Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views

Lecture Slide of Expression in C Program

1. An expression in C programming combines operands and operators to evaluate to a single value. Common expressions include arithmetic operations, assignments, comparisons, and logical operations. 2. Expressions have an l-value, which must be a variable that can hold data, and an r-value that returns a value such as a constant. 3. C provides various operators to manipulate data in expressions, including arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, and special operators. The precedence of operators determines the order of operations in an expression.

Uploaded by

Min Yoonti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Lecture Slide of Expression in C Program

1. An expression in C programming combines operands and operators to evaluate to a single value. Common expressions include arithmetic operations, assignments, comparisons, and logical operations. 2. Expressions have an l-value, which must be a variable that can hold data, and an r-value that returns a value such as a constant. 3. C provides various operators to manipulate data in expressions, including arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, and special operators. The precedence of operators determines the order of operations in an expression.

Uploaded by

Min Yoonti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Expressions

Expression
1. In programming, an expression is any legal
combination of symbols that represents a value.
2. C Programming Provides its own rules of
Expression, whether it is legal expression or
illegal expression. For example, in the C
language x+5 is a legal expression.
3. Every expression consists of at least one operand
and can have one or more operators.
4. Operands are values and Operators are symbols
that represent particular actions.
Certain Examples of Expression.
Now we will be looking into some of the C
Programming Expressions, Expression can be
created by combining the operators and
operands
Each of the expression results into the some
resultant output value. Consider few expressions
in below table
Expression Examples, Explanation
n1 + n2,This is an expression which is going to
add two numbers and we can assign the result of
addition to another variable.
Continue . . . . . . .
x = y, This is an expression which assigns the
value of right hand side operand to left side
variable
v = u + a * t, We are multiplying two numbers and
result is added to ‘u’ and total result is assigned
to v
x <= y, This expression will return Boolean value
because comparison operator will give us output
either true or false ++j, This is expression having
pre increment operator\, it is used to increment
the value of j before using it in expression [/table]
More explanation with
 An expression is any valid set of literals, variables,
operators, operands and expressions that evaluates to a
single value.

 This value can be a number, a string or a logical value.

 For instance a = b + c; denotes an expression in which


there are 3 operands a, b, c and two operator + and =.

 The number of operands of an operator is called its arity.

 Based on arity, operators are classified as unary (1


operand), binary (2 operands), ternary (3 operands).
What is L-Value of Expression?
1. L-Value stands for left value
2. L-Value of Expressions refer to a memory
locations
3. In any assignment statement L-Value of
Expression must be a container (i.e. must have
ability to hold the data)
4. Variable is the only container in C programming
thus L Value must be any Variable
5. L Value Cannot be Constant, Function or any of
the available data type in C
int main()
{
int num;
num = 5;
return(0);
}
What is R-Value of Expression?
1. R Value stands for Right value of the
expression.
2. In any Assignment statement R-Value of
Expression must be anything which is
capable of returning Constant Expression or
Constant Value.
int main()
{
int num;
num = 5;
return(0);
}
based on operators
Operator
 An operator is a symbol that tells the compiler to
perform specific mathematical or logical manipulations.
 They are used in programs to manipulate data and
variables.
 C language is rich in built-in operators and
provides the following 8 types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
continue . . . . . . . . . .
- Bitwise Operators
- Assignment Operators
- Increment & Decrement Operator
- Conditional Operator
- Special Operator
Arithmetic Operators
Operation Operator Example Value of
(a=5, b=3) expression

Addition + a+6 11
Subtraction - a-6 -1
Multiplication * a*b 15

Division / a/b 1
Modulus % a%b 2
Arithmetic Operators
• If both of the operands are int types, then the
result of the expression is an int too. If you want the
result to be any other type, you have to use type
casting.
int a = 3, b = 5;
double c;
c = 1/5; /* value of c is 0 */
c = 1.00/5; /* value of c is 0.2 */
c = a/b; /* value of c is 0 */
c = (double)a/b; //value of c is 0.6

• You can use modulus (%) operation only on int


variables.
Unary Operators
C Operation Operator Example
Positive + a = +3;
Negative - b = -a;

• The first assigns positive 3 to a.


• The second assigns the negative value of a to b.
Operator(Increment &
Decrement Operators)
C supports 2 useful operators namely
1. Increment ++
2. Decrement – operators
The ++ operator adds a value 1 to the operand
The – operator subtracts 1 from the operand
++a or a++
--a or a--
Operator(Increment &
Decrement Operators)
Rules for ++ & -- operators
1. These require variables as their operands
2. When postfix either ++ or – is used with the variable in a
given expression, the expression is evaluated first and then
it is incremented or decremented by one.
That is, i++ will increment the value of i, but return
the original value that i held before being incremented.

3. When prefix either ++ or – is used with the variable


in a given expression, it is incremented or decremented
by one first and then the expression is evaluated with
the new value. That is, ++i will increment the
value of i, and then return the incremented value.
Operator(Increment &
Decrement Operators)
Examples for ++ & -- operators
Let the value of a =5 and b=++a then
a = b =6
Let the value of a = 5 and b=a++ then
b = 5 but a = 6
i.e.:
1. a prefix operator first adds 1 to the operand
and then the result is assigned to the variable on the left
2. a postfix operator first assigns the value to the
variable on left and then increments the operand.
Operator( Increment & Decrement Operators) (continued)
Increment & Decrement Operators (continued)
Conditional Operator
Syntax:
exp1 ? exp2 : exp3
Where exp1,exp2 and exp3 are expressions
Working of the ? Operator:

Exp1 is evaluated first, if it is nonzero(1/true) then the


expression2 is evaluated and this becomes
the value of the expression,

If exp1 is false (0/zero) exp3 is evaluated and its value


becomes the value of the expression
Ex: m=2;
n=3
r=(m>n) ? m : n;
Conditional Operator(continued)

#include<stdio.h>
int main()
{
int m, n, r;
scanf("%d %d", &m, &n);
(m>=n) ? printf("%d is greater than or equal to %d", m, n):
printf("%d is less than %d", m, n);
return 0;
}
Conditional Operator(continued)
Bitwise Operators
•Truth table

& bitwise AND b a ~a a^ a|b a&


b b
| bitwise OR
^ bitwise XOR 0 0 1 0 0 0
~ 1’s compliment
1 0 1 1 1 0
<< Shift left
>> Shift right 0 1 0 1 1 0

1 1 0 0 1 1

All these operators (except ~) can be suffixed with =


For instance a &= b; is the same as a = a & b;
Bitwise Operators
•Examples

11010011 11010011 11010011


& | ^
10001100 10001100 10001100
------------ ------------ ------------
10000000 11011111 01011111

~11010011
------------
00101100
Bitwise Operators
•Examples: int a = 33333, b = -77777;

Expression Representation Value


a 00000000 00000000 10000010 00110101 33333
b 11111111 11111110 11010000 00101111 -77777
a&b 00000000 00000000 10000000 00100101 32805
a^b 11111111 11111110 01010010 00011010 -110054
a|b 11111111 11111110 11010010 00111111 -77249
~(a|b) 00000000 00000001 00101101 11000000 77248
~a&~b 00000000 00000001 00101101 11000000 77248
Bitwise Operators
•Examples
Right shift Left shift
11010011>>3 11010011<<3
------------ ------------
00011010 10011000
Bitwise Operators
char c = 130;
int a = 1 << 31; /* shift 1 to the MSB */
unsigned int b = 1 << 31;

Expression Representation Action

c 10111100 unshifted
c << 4 11000000 left shifted 4
c >> 4 00001011 right shifted 4
a 10000000 00000000 00000000 00000000 unshifted
a >> 3 11110000 00000000 00000000 00000000 right shifted 3
b 10000000 00000000 00000000 00000000 unshifted
b >> 3 00010000 00000000 00000000 00000000 right shifted 3
Bitwise Operator (continued)
Bitwise Operator (continued)
Special Operator

1. Comma operator ( ,)
2. sizeof operator – sizeof( )
3. Pointer operators – ( & and *)
4. Member selection operators – ( . and ->)
Special Operator(continued)
sizeof() operator:
- The sizeof() operator returns the size of its operand in bytes
- The size of operator always preceeds its operand.
- The operand may be an expression or it may be a cast.
Uses:
- To determine the lengths of array structures.
- To allocate memory space dynamically to variables during exe
Operator Precedence
• When an expression contains more than one
operator, the meaning of the expression may not
be immediately clear:
Does i + j * k mean (i + j) * k or
i + (j * k) ?
• In C, this potential ambiguity is resolved by
operator precedence.
• Precedence of the arithmetic operators:
Operator precedence determines the
grouping of terms in an expression and
decides how an expression is evaluated.
Certain operators have higher precedence
than others.
For example, the multiplication operator has a
higher precedence than the addition operator.
For example, x = 7 + 3 * 2
Here, x is assigned 13, not 20 because
operator * has a higher precedence than +, so
it first gets multiplied with 3*2 and then adds
into 7.
Here, operators with the highest
precedence appear at the top of the
table, those with the lowest appear at the
bottom.
Within an expression, higher precedence
operators will be evaluated first.

Go to next page
Operator Precedence
More Examples:

i + j * k means i + (j * k)
-i * -j means (-i) * (-j)
+i + j / k means (+i) + (j / k)
Associativity
• Operator precedence rules alone aren’t enough when an
expression contains two or more operators at the same
level of precedence. The associativity of the operators
now comes into play.
• The binary arithmetic operators are all left associative
(they group from left to right); the unary operators are
right associative.
Examples of associativity:
i - j - k means (i - j) - k
i * j / k means (i * j) / k
i - j * i + k means (i - (j * i)) + k
Assignment Operators
• Assignment operators are used to combine the '='
operator with one of the binary arithmetic operators
• In the following table, assume that c = 9

Operator Example Equivalent Value of c is


Statement
+= c += 7 c = c + 7 16
-= c -= 8 c = c – 8 1
*= c *= 10 c = c * 10 90
/= c /= 5 c = c / 5 1
%= c %= 5 c = c % 5 4
There are two variants of
increment/decrement operator

- Prefix (pre-increment and pre-decrement)


- Postfix (post-increment and post-decrement)
Prefix Operator.
The increment operator ++ if used as prefix
on a variable, the value of variable gets
incremented by 1.
After that the value is returned unlike Postfix
operator. It is called Prefix increment operator.
In the same way the prefix decrement operator
works but it decrements by 1.
Postfix Operator.
The increment operator ++ if used as postfix
on a variable, the value of variable is first
returned and then gets incremented by 1. It is
called Postfix increment operator.
In the same way the decrement operator
works but it decrements by 1.
Increment and Decrement Operators
• The ++ and -- operators increment and decrement variables.
• Both operators have the same precedence as negation.
• Either operator can be prefix or postfix:
++i (same as i = i + 1)
i++ (same as i = i + 1)
--i (same as i = i - 1)
i-- (same as i = i - 1)
• When used as a prefix operator, ++ increments the variable
before its value is fetched:
i = 1;
printf("i is %d\n", ++i); /* prints "i is 2" */
Increment and Decrement Operators

• When used as a postfix operator, ++ increments the variable


after its value is fetched:
i = 1;
printf("i is %d\n", i++); /* prints "i is 1" */
printf("i is %d\n", i); /* prints "i is 2" */

• The -- operator has similar properties:


i = 1;
printf("i is %d\n", --i); /* prints "i is 0" */

i = 1;
printf("i is %d\n", i--); /* prints "i is 1" */
printf("i is %d\n", i); /* prints "i is 0" */
Increment and Decrement Operators
int R = 10, count=10;

Statement Equivalent R Count


Statements
R = count++; R = count; 10 11
count = count + 1
R = ++count; count = count + 1; 11 11
R = count;
R = count --; R = count; 10 9
count = count – 1;
R = --count; Count = count – 1; 9 9
R = count;
Partial List of C Operators
Precedence Operator Associativity

3 * Left
/
%
4 + Left
-
5 = *= /= %= += -= right
•#include <stdio.h>
int main()
{
int x = 3;
printf("%d\n", 3 -2 / 4);//3
printf("%f\n", 3 -2.0 / 4);//2.5
printf("%d\n", -27 / -5 + 4 / 3);//6
printf("%d\n", 16 % -5 + 7 * 6);//43
printf("%d\n", -12*3%2*-23/+6-5*2);//-10

printf("%d\n", x-- * 2 + 5);//11


printf("%d\n", x);//2
printf("%d\n", --x * 2 + 5);//7
printf("%d\n",x);//1
printf("%d\n", 3 % 5 / (5 % 3));//1
return 0;
}

You might also like