Tuesday, January 17, 2012

Precedence & Associativity Questions

#include<stdio.h>
int main()
{
  char *s[] = { "knowledge","is","power"};
  char **p;
  p = s;
  printf("%s ", ++*p);
  printf("%s ", *p++);
  printf("%s ", ++*p);
  getchar();
  return 0;
}
Output: nowledge nowledge s

Let us consider the expression ++*p in first printf(). Since precedence of prefix ++ and * is same, associativity comes into picture. *p is evaluated first because both prefix ++ and * are right to left associative. When we increment *p by 1, it starts pointing to second character of “knowledge”. Therefore, the first printf statement prints “nowledge”.
Let us consider the expression *p++ in second printf() . Since precedence of postfix ++ is higher than *, p++ is evaluated first. And since it’s a psotfix ++, old value of is used in this expression. Therefore, second printf statement prints “nowledge”.
In third printf statement, the new value of (updated by second printf) is used, and third printf() prints“s”.

Question 2:
int main()
{
  int x, y = 5, z = 5;
  x = y==z;
  printf("%d", x);
  getchar();
  return 0;
}
The crux of the question lies in the statement x = y==z. The operator == is executed before = because precedence of comparison operators (<=, >= and ==) is higher than assignment operator =.
The result of a comparison operator is either 0 or 1 based on the comparison result. Since y is equal to z, value of the expression y == z becomes 1 and the value is assigned to x via the assignment operator.

Question 3:
int main()
{
  char arr[]  = "geeksforgeeks";
  char *ptr  = arr;
  while(*ptr != '\0')
      ++*ptr++;
  printf("%s %s", arr, ptr);
  getchar();
  return 0;
}
Output:  hffltgpshfflt
Explanation:
The crust of this question lies in expression ++*ptr++.
If one knows the precedence and associativity of the operators then there is nothing much left. Below is the precedence of operators.
Postfixx ++            left-to-right
   Prefix  ++             right-to-left
   Dereference *          right-to-left
Therefore the expression ++*ptr++ has following effect
Value of *ptr is incremented
Value of ptr is incremented

Question 4:
int main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
return 0;
}
Output:0 0 1 3 1
Explanation:Logical operations always give a result of 1 or 0 . And also the logical
AND  (&&)  operator  has  higher  priority over   the  logical  OR  (||)  operator.  So  the
expression  ‘i++ && j++ && k++’ is executed first. The result of this expression is 0
(-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR
operator always gives 1 except for ‘0 || 0’ combination- for which it gives 0). So the
value of m is 1. The values of other variables are also incremented by 1.
Question 5:
int main()
{
int i=10;
i=!i>14;
Printf ("i=%d",i);
}
Output:i=0
Explanation:In the expression !i>14 , NOT (!) operator has more precedence than ‘
>’ symbol.   !  is a unary logical operator. !i (!10) is 0 (not of true is false).   0>14 is
false (zero).

No comments:

Post a Comment