Tuesday, 31 March 2015

Operators C tid bits


1 :
    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

    2 :
     int i = 1, 2, 3;
     printf("%d", i);
     Compile time error : Comma acts as a separator here. The compiler creates an integer variable and initializes it with 1. The compiler fails to create integer variable 2 because 2 is not a valid identifier.
   
     3:
     int i = (1, 2, 3);
     printf("%d", i);
     Output : 3
     The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right but it is always the result of the last expression which gets assigned.
   
     4:
      int i;
    i = 1, 2, 3;
    printf("%d", i);  Output : 1
    Comma acts as an operator. The assignment operator has higher precedence than comma operator. So, the expression is considered as (i = 1), 2, 3 and 1 gets assigned to variable i.
  
    5 The control in the logical OR goes to the second expression only if the first expression results in FALSE
    6: . An expression doesn't get evaluated inside sizeof operator.
  
    7 :
        This is an another way to convert an alphabet to upper case by resetting its bit positioned at value 32 i.e. 5th bit from the LSB(assuming LSB bit at position 0).
      
        eg : a &= ~32 ; Outputs A  ;
    8:
        We can swap two variables without any extra variable using bitwise XOR operator '^'. Let X and Y be two variables to be swapped. Following steps swap X and Y.

          X = X ^ Y;
          Y = X ^ Y;
          X = X ^ Y ;
           
   

No comments:

Post a Comment