Friday, July 27, 2012

Expressions in c

An expression is a sequence of operands and operators that reduces to a single value. An expression can be simple or complex. A simple expression contains only one operator e.g 2+5; A complex expression contains more than one operator e.g 2+5*3-9.

Expression can be categorized into 6 parts.

Primary expressions: A primary expression consists of only one operand with no operator. In c, the operand in the primary expression can be a name, a constant, or a parenthesized expression.

names: a, b12, price, calc, etc....
constants: 6,123.98, 'a'.

Parenthetical expression: (2+4*6-4).

Postfix expression: The postfix expression consists of one operand followed by one operator.
example postfix increment/ decrement e.g a++, a--;

Prefix expression: In prefix expressions, the operator comes before the operand. example: prefix increment/decrement e.g ++a, --a;

Unary Expressions: A unary expression, like a prefix expression consists of one operator and one operand.. Also like the prefix expression, the operator comes before operand. but the difference between unary and prefix expression is that prefix expression needs  a variable as an operand, while unary expression can have an expression or a variable as the operand. ex- sizeof(int). +a,-a, float(x).

Binary Expressions: Binary expressions are formed by an operand operator operand combination.
example: 10*5, (2+38*i)*(1+3+i).

Assignement expression: ex- a=5; b=90;


No comments:

Post a Comment