3-Elements of C-31-07-2023
3-Elements of C-31-07-2023
1
7. EXPRESSION
(VII) Expression
It is a combination of operators, constants and variables.
Types of expression
ExpressionType Description with Example
Constant expression Expression with only constant values.
Ex: 10 + 5 / 6.0
Floating expression Produce floating point results
Ex: x + y, 10.75
NOTE: x, y are the floating point variables
Relational expression Yield results of type boolean type (0 or 1)
Ex: x <= y, x + y > 2
Logical expression Combine two or more relational expressions and produces
boolean type results.
Ex: x > y && x == 10, x == 10 || y == 5
Bitwise expression Manipulate data at bit level.
Ex: x << 3, y >> 1
OPERATOR
PRECEDENCE
Operator Precedence
Tells the order of evaluating an expression.
Priority (precedence) & associativity.
Note: operators with the highest priority (precedence) appear at the top
of the table & lowest priority appear at the bottom.
Operator Precedence
Example - 1
int var = 15 - 4 * 3;
Example - 2
Quiz - ?
Evaluate the following:
(10 - 4) + (20 / (2 * 5)) * 3
7 + 2 * 4 - 3;
1 == 3 != 5;
++a * (3 + 8) % 35 - 28 / 7, assume a = 5
3<<1+2*3/7+x++,assume x=2
Example
// OperPrecAsso.c
#include<stdio.h> OUTPUT:
void main() x=12
{ y=12
z=1
int x=(10 - 4) + (20 / (2 * 5)) * 3; b=27
int y=7 + 2 * 4 - 3;
int z=1 == 3 != 5;
int a=5;
int b = ++a * (3 + 8) % 35 - 28 / 7;
printf("x=%d\n",x);
printf("y=%d\n",y);
printf("z=%d\n",z);
printf("b=%d\n",b);
}
TYPE CONVERSION
Type Conversion
Takes place when two or more data type present in an
expression.
Process of converting one data type to another.
Performed by a compiler.
NOTE:The destination data type can‟t be smaller than the
source data type.
Types:
Implicit type conversion
Explicit type conversion
(a) Implicit Type conversion
(or) automatic type conversion
Done by the compiler on its own, without any external
trigger from the user.
All the data types of the variables are upgraded to the data
type of the variable with the largest data type.