Posted on 19th September 2009No Responses
Expression in C- Definition, Types and Example

Expressions
Operators, constants, and variables are the constituents of expressions. An expression in C/C++ is any valid combination of these elements. Because most expressions tend to follow the general rules of algebra, they are often taken for granted. However, a few aspects of expressions relate specifically to C and C++.

Order of Evaluation
Neither C nor C++ specifies the order in which the subexpressions of an expression are evaluated. This leaves the compiler free to rearrange an expression to produce more optimal code. However, it also means that your code should never rely upon the order in which subexpressions are evaluated. For example, the expression
x = f1() + f2();
does not ensure that f1() will be called before f2().

Type Conversion in Expressions
When constants and variables of different types are mixed in an expression, they are all converted to the same type. The compiler converts all operands up to the type of the largest operand, which is called type promotion. First, all char and short int values are automatically elevated to int. (This process is called integral promotion.) Once this step has been completed, all other conversions are done operation by operation, as described in the following type conversion algorithm:
IF an operand is a long double
THEN the second is converted to long double
ELSE IF an operand is a double
THEN the second is converted to double
ELSE IF an operand is a float
THEN the second is converted to float
ELSE IF an operand is an unsigned long
THEN the second is converted to unsigned long
ELSE IF an operand is long
THEN the second is converted to long
ELSE IF an operand is unsigned int
THEN the second is converted to unsigned int

char ch;
int i;
float f;
double d;
result=(ch/i) + (f*d) – (f+i);
int double float
int double float
double
A type conversion example

There is one additional special case: If one operand is long and the other is unsigned int, and if the value of the unsigned int cannot be represented by a long, both operands are converted to unsigned long.
Once these conversion rules have been applied, each pair of operands is of the same type and the result of each operation is the same as the type of both operands.

Casts
You can force an expression to be of a specific type by using a cast. The general form of a cast is
(type) expression
where type is a valid data type. For example, to make sure that the expression x/2 evaluates to type float, write (float) x/2
Casts are technically operators. As an operator, a cast is unary and has the same precedence as any other unary operator.
Although casts are not usually used a great deal in programming, they can be very useful when needed. For example, suppose you wish to use an integer for loop control, yet to perform computation on it requires a fractional part, as in the following program:
#include
int main(void) /* print i and i/2 with fractions */
{
int i;
for(i=1; i<=100; ++i)
printf(”%d / 2 is: %f\n”, i, (float) i /2);
return 0;
}
Without the cast (float), only an integer division would have been performed. The cast ensures that the fractional part of the answer is displayed.

Spacing and Parentheses
You can add tabs and spaces to expressions to make them easier to read. For example, the following two expressions are the same:
x=10/y~(127/x);
x = 10 / y ~(127/x);
Redundant or additional parentheses do not cause errors or slow down the execution of an expression. You should use parentheses to clarify the exact order of evaluation, both for yourself and for others. For example, which of the following two expressions
is easier to read?
x = y/3-34*temp+127;
x = (y/3) – (34*temp) + 127;

Shorthand Assignments
There is a variation on the assignment statement, sometimes referred to as a shorthand assignment, that simplifies the coding of a certain type of assignment operation. For example,
x = x+10;
can be written as
x += 10;
The operator
+=
tells the compiler to assign to x the value of x plus 10.
This shorthand works for all the binary operators (those that require two operands). In general, statements like:
var = var operator expression
can be rewritten as
var operator = expression
For another example,
x = x-100;
is the same as
x -= 100;
Shorthand notation is widely used in professionally written C/C++ programs; you should become familiar with it.

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • blogmarks
  • BlogMemes Fr
  • BlogMemes Sp
  • Blogosphere News
  • blogtercimlap
  • co.mments
  • connotea
  • Current
  • Design Float
  • Diigo
  • DotNetKicks
  • DZone
  • eKudos
  • email
Comments
Leave a Response