Lecture Slide of Expression in C Program
Lecture Slide of Expression in C Program
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.
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
#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
1 1 0 0 1 1
~11010011
------------
00101100
Bitwise Operators
•Examples: int a = 33333, b = -77777;
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
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;
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