Friday, July 27, 2012

Precedence and associativity in c

Precedence: Precedence is used to determine the order in which different operators in complex expression are evaluated.

Assiciativity: Associativity is used to determine the order in which  operators with the same precedence are evaluated in a complex expression. It can be a left to right or right to left depends on the expression.

Left to right associativity: 3*8/4%4*5
Right to left associativity: Several operators have right to left associativity. for example when more than one assignment operator occur in an expression e.g a+=b*=c-=5 evaluated as (a+=(b*=(c-=5))).


difference between signed int and unsigned int (signed int vs unsigned int)

Signed integer can store positive value as well as negative value while unsigned integer can store only positive value.
For signed integer the range is -32768 to 32767 while for unsigned integer the range is: 0 to 65535.

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;

  

Saturday, July 21, 2012

difference between short int and int (int vs short int) ?

Actually the size of int is fully dependent on machine and compiler. But c requires to maintains the following relationship:  sizeof(short int)<=sizeof(int)<=sizeof(long int)<sizeof(long long int).
For e.g. in 32 bits operating system sizeof(short int)=2 bytes, sizeof(int)=2bytes, sizeof(long int)=4bytes, sizeof(long long int)=4bytes.
but in case of 64 bits operating system sizeof(long long int)=8bytes.
So the bottom line is that size of integer is totally dependent on the machines and compilers.