An expression is a combination of constants and variables interconnected by one or more operators. An expression consists of one or more operands and one or more operators.
Operands are values and operators are symbols that represent particular actions.
For example statements like a = b + 5, ++x and 300 > (8 * b) are all expressions. Strictly speaking, even a single variable or constant can be considered an expression.
Expressions can also represent logical conditions that are either true or false. In C, the conditions true and false are represented by the integer values 1 and 0 respectively.
a-b;
In the above expression, minus character (-) is an operator, and a, and b are the two operands.
Below are few more examples and their explanation.
Expressions | Description |
---|---|
num1 + num2 | variables num1 and num2 are operands and + is the operator used. |
x = y | the assignment operator (=) is used to assign the value stored in y to x. |
a = b + c | the value of the expression (b + c) is assigned to a. |
x <= y | the lesser-than-equalTo (<=) the relational operator compares the values of x and y and returns either 1 (true) or 0 (false). |
a++ | the value of variable a is incremented by 1 i.e, this expression is equivalent to a = a + 1. |
Expressions can also represent logical conditions that are either true or false. In C, the conditions true and false are represented by the integer values 1 and 0 respectively.
Consider an expression below.
a = 3 + 4 * 3;
What will be the value of a? 21 or 15?. To avoid the ambiguity during the evaluation of expressions involving 2 or more operators in a C program, C language has given precedence to each and every operator supported by it. Therefore, in C the operators at higher level of precedence are evaluated first. Below table shows the various precedence levels (Rank 1 being the highest level)
Rank | Operator Type | Associativity |
---|---|---|
1 | () [] member operators (. ->) expr++ expr-- | left-to-right |
2 | pointer operators (* &) unary plus (+) unary minus (-) ! ~ ++expr --expr (typecast) sizeof | right-to-left |
3 | * / % | left-to-right |
4 | + - | left-to-right |
5 | >> << | left-to-right |
6 | < > <= >= | left-to-right |
7 | == != | left-to-right |
8 | & | left-to-right |
9 | ^ | left-to-right |
10 | | | left-to-right |
11 | && | left-to-right |
12 | || | left-to-right |
13 | Ternary Operator | right-to-left |
14 | Assignment Operators | right-to-left |
15 | Comma | left-to-right |
Below are the examples which lists some expressions and their evaluation in C programming language.
Expression | C Evaluation |
---|---|
1+2*2 | 1+(2*2) |
1+2*2*4 | 1+((2*2)*4) |
(1+2)*2*4 | ((1+2)*2)*4 |
1+4,c=2|3+5 | (1+4),(c=(2|(3+5))) |
1 + 5&4 == 3 | (1 + 5) & (4 == 3) |
c=1,99 | (c=1),99 |
!a++ + ~f | (!(a++)) + (~f) |
s == ‘w’ || i < 9 | (s == ‘w’) || (i < 9) |
r = s == ‘w’ | r = (s == ‘w’) |
C expressions may contain many variables and constants which may be When they are evaluated they should be converted to common type.
Sometimes implicit/ automatic type conversions are done by C compiler. A narrower operand is converted into a wider one without losing information if expression involves a wider variable or constant. Consider the example below
int x = 10; float y = 5.5; double z; z = x + y;
In this example, integer variable x will be first converted to float (temporarily) and then the contents will added to real variable y. The result will be raised to double that will be assigned to z. However, x will still be an integer and y will still be a floating point. It should be noted that if z would have be integer type, then the result would have been grounded to integer.
Sometimes, due the complexity of an expression and nature of computation required, explicit type conversions are needed. Consider an example below
int x = 10, y = 3; float z; z = x / y;
Because both x and y are integers, the fractional part will be dropped during division. However, because z is floating point variable, the result (3) will be raised to float (3.000). In explicit type conversions, any variable or constant can be forced for a type conversion that is different from automatic conversion.
In order to achieve this, C language provides type-casting operator which takes first argument as the data type to which the second argument should be type casted temporarily.
The general syntax of type casting is as follows
(data-type) Expression;
So using explicit type casting, we can solve the above mentioned problem as shown below.
int x = 10, y = 3; float z; z = (float) x / y;
In this case, x is type-casted to float. Thus, to evaluate the expression y is also raised to float. Hence, the result is accurate and is assigned to z.