Monday, October 22, 2012

Operators In 'C' :

Assignment (=)
Case :- Lvalue Error


main()

{
int a = 10 , b = 20;
printf("a=%d b=%d\n",a,b);
b= ( a*b/a=b );
printf("a=%d b=%d\n",a,b);
}


O/p:- Invalid Lvalue In Assignment


The Above Error Is A Result Of Assignment a*b/a=b . a*b/a is a Constant and not a value which could hold an address , but we were trying to assign a variable to it .
b=(a*b/(a=b)); Grouping a=b Using parentheses Will Solve The Issue And Makes It More Readable .

Arithmetic ( + , - , * , / , % )

Note :- % Operator Is Applicable Only On Integer Data Type

Case :- Type Casting

main()
{
int a=5,b=2;
float c;
c=a/b;
printf("%f",c);
}

O/p: - 2.000000

Note :- Observe The Output Is Not 2.500000 as Expected . Its 
because , both a and b are integers ,any operation on them results in an integer but not a higher data type .

The Solution To Above Problem Is "Explicit Type Casting"


main()
{
int a=5,b=2;
float c;
c= (float)a / b;
printf("%f",c);
}

O/p :- 2.500000

Relational ( < , <= , > , >= , == , != )

eg:- int a=1,b=1;
        c = a!=b;  
       Implies c = 0

Case :-  Explicit Type Casting

main()
{
unsigned int a=1000000;
int b = -6000;
int c;
c= a>b;
printf("c=%d\n");
}

O/p:- c=0

Note :- Despite the Condition a>b being true , the code results in c=0 .

In The above case , implicit type casting of 'b' (signed int) which is a lower range happens to 'a'(unsigned int) which is higher range.

Then the value of  -6000 = [(2^32)-6000] =4294961296

So , now b=4294961296 and a=1000000 .then a>b is not true ,thus c=a>b results in c=0.

The Solution For Above Problem Is Again Explicit Type Casting 
main()
{
unsigned int a=1000000;
int b = -6000;
int c;
c= (signed)a>b;
printf("c=%d\n");
}

O/p:- c=1


Logical ( && , || , !

eg 1:- x = 5 && -6 implies x = 1 (Since Both 5 and -6 are non -zero)
eg 2 :- x = 5 && 0 implies x = 0 (Since One Of The Values In 5 and 0 is zero)

Case :- Skipping Obvious Results



Note :Associativity for Logical Operators is From Left To Right


eg:- 1 :
let a=10 ,b=0,c=-5
d= (a=b)&&(b=c)||(c=a) ;
Now , a=0 , b=0 , c=0 , d=0  , Note That The Assignments b=c and c=a have been skipped . Otherwise The Result Could have Been a=0 , b=-5 , c=10 , d=1

Bit wise ( & , | , ^ , ~ , << , >> )

Note : Bit Wise Operators Are Applicable Only On Integers , Its A Syntax Error Otherwise . 

Associativity Is From Right To Left .

Eg :- 
main()
{
int a=20,b=12;
printf("a= %d    b= %d \n",a,b);
a^=b^=a^=b;
printf("a= %d    b= %d \n",a,b);
}

O/p :- a= 12   b= 20 


Sign Copy For Right Shifting Negative Numbers :


behavior of operators in C


Sizeof( ) Operator :
Eg:-
main()
{
char a='a',c=99;
int b;
printf("%d \n",sizeof(c));
printf("%d \n",sizeof(a));
printf("%d \n",sizeof(b));
printf("%d \n",sizeof('a'));
printf("%d \n",sizeof(int));
printf("%d \n",sizeof(1.0));
printf("%d \n",sizeof("1.0"));
printf("%d \n",sizeof("double"));
}

O/p :-        1
             1
             4
             4 ( Because 'a' is Equivalent to ASCII Integer Value 97 )
             4
             8 ( Because Default Real Type Is Double )  
             4 ( Because "1.0" is A String(Character Array) of 4 characters i.e; '1' , '.' ,  '0' and '\0' )        
             7 ( Because "double" is String (Character Array) of characters i.e; 'd' , 'o' , 'u' , 'b' , 'l' , 'e' , '\0' )

Increment(++) And Decrement(--) :

Note : Associativity is From Right To Left

main()
{
 int a=1, b=5 ,c=3;
 a = b++ + c++ ;          // b=5  , c=3
printf("a=%d b=%d c=%d\n",a,b,c);
b= ++c + --a + --b ;    // c=5 , a=7 , b=5
printf("a=%d b=%d c=%d\n"a,b,c);
c= ++a + b++ + c++; // a=8 , b=17 , c=5
printf("a=%d b=%d c=%d\n"a,b,c);
}

O/p:- 8 6 4
        7 17 5
        8 18 29

Conditional Operator ((Condition)?(If True) : (If False))


Eg :-

main()
{
int a=10,b=20,c;
(a==b)?(c=1):(c=0);
printf("c=%d\n",c);
a=20;
(a==b)?(c=1):(c=0);
printf("c=%d\n",c);
}

O/p:- c=0
       c=1

The Other Operators like &,* will be discussed in future posts.


Example To Ease Your Understanding Of Operators In 'C' :

Eg:- Program To Test A Given Bit In A Given Location
main()
{
int num,bit;
printf("Enter An Integer :");
scanf("%d",&num);
printf("Enter The Bit Position To Test:");
scanf("%d",&bit);
((num>>bit)&1) ? printf("Bit is Set") : printf("Bit is Clear") ;
}


O/p :- Enter An Integer : 5
       Enter The Bit Position To Test: 2
       Bit is Set
       Enter An Integer : 5
       Enter The Bit Position To Test: 1
       Bit is Clear



Case :- Comma Operator ( , )

main()
{
int a=1,b=10,c;
C=(a= -1 , ++b , a = a+b);
printf("%d     %d     %d\n",a,b,c);
}

O/p:- 10    11     10

The Right Most Expression Is Evaluated And Finally The Result a assigned To c