C Expressions

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.

Example #1

In the above expression, minus character (-) is an operator, and a, and b are the two operands.

Example #2

Below are few more examples and their explanation.

num1 + num2variables num1 and num2 are operands and + is the operator used.
x = ythe assignment operator (=) is used to assign the value stored in y to x.
a = b + cthe value of the expression (b + c) is assigned to a.
x <= ythe 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.

Precedence and Associativity

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)

RankOperator TypeAssociativity

() []

member operators (. ->)

expr++ expr--


pointer operators (* &)

unary plus (+)

unary minus (-)

! ~

++expr --expr

(typecast) sizeof

3* / %left-to-right
4+ -left-to-right
5>> <<left-to-right
6< > <= >=left-to-right
7== !=left-to-right
13Ternary Operatorright-to-left
14Assignment Operatorsright-to-left
Expressions – Evaluation

Below are the examples which lists some expressions and their evaluation in C programming language.

ExpressionC Evaluation
1 + 5&4 == 3(1 + 5) & (4 == 3)
!a++ + ~f(!(a++)) + (~f)
s == ‘w’ || i < 9(s == ‘w’) || (i < 9)
r = s == ‘w’r = (s == ‘w’)
Expressions – Type Conversions

C expressions may contain many variables and constants which may be When they are evaluated they should be converted to common type.

Implicit type conversion

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.

Explicit type conversion

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.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +