Friday, July 29, 2016

C Basics - Expression in C

An expression is nothing but a valid combination of constants, variables and operators. Thus, 3, 3 + 2, c and a + b * c – d all are valid expressions.

An expression is a sequence of operators and operands that specifies computation of a value. An expression may consist of single entity or some combination of such entities interconnected by one or more operators. All expression represents a logical connection that's either true or false. Thus logical type expression actually represents numerical quantities.

In C every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable. Some examples of expressions are shown in the table given below.

a+b
3.14*r*r
a*a+2*a*b+b*b

Consider the following expression: i+++j

The compiler first makes the longest possible operator (++) from the three plus signs, then processes the remaining plus sign as an addition operator (+).Thus, the expression is interpreted as (i++) + (j), not (i) + (++j).

Lvalues and Rvalues:
Every C expression is either an lvalue or an rvalue. An lvalue refers to an object that persists beyond a single expression. You can think of an lvalue as an object that has a name. All variables, including nonmodifiable (const) variables, are lvalues. An rvalue is a temporary value that does not persist beyond the expression that uses it. To better understand the difference between lvalues and rvalues, consider the following example:

#include <stdio.h>

int main()
{
   int x = 3 + 4;
   printf ("%d", x);
   
   return 0;
}
In this example, x is an lvalue because it persists beyond the expression that defines it. The expression 3 + 4 is an rvalue because it evaluates to a temporary value that does not persist beyond the expression that defines it.



Related topics:
Overview of Storage Class in C   |   Overview of Operators in C   |   Type Conversion in C   |   Overview of Instruction in C   |   Overview of Statements in C

List of topics: C Programming

No comments:

Post a Comment