Know your operators

You know basic operators of any programming language. You know very well that x is not multiplication, but * is. You also know that % does not give percentage, instead gives reminder. You also know about && || and !

But that is just basic stuff or as the cliche goes tip of the iceberg. C has a very rich set of operators. And these allow you to write very small and cryptic programs.

a++ or ++a? 

You know about increment operators - pre-increment and post increment. You can increment your number a using ++a or a++. Which is better and why?

The answer is - it does not matter. Both increment the number - an integer. Please remember that you can not increment a float or a double. But you can increment a char - because char is basically an integer. 

So you use a++; or ++a; 

But it matters when this iincrement operation is used in an expression.

Let us look at an example.
 int m = 2, n=3;  
 m = 2 + ++n; 


Here m will be 6 and n will be 4.

 But if we use post increment operator

 int m = 2, n=3;  
 m = 2 + n++;
 Here m will be 5 because 2+n is assigned to m and after that n is incremented.

Same rules apply to decrement operators --.

What is sum+=n?

You use this type of expression quite often

sum = sum+n;

So, C has a short hand operator for this called +=

sum+=n;

is same as sum = sum+n;

You have such operators for all arithmetic and bitwise operators.  You have -=, *=, /=, %=, <<=,>>=, &=,|=,^=

What is the relation?   

Of course, you know about less than, greater than, less than or equal to, greater than or equal to ( <, >, <=, >=). But confusing operators for newbies are equal to and not equal to.

Equal to operator is not =. 

= is assignment operator. Equal to is ==. And not equal to is !=.  
 
 int a = n%2;  
 if(a==0)  
   printf("%d is even",n);  
 else  
   printf("%d is odd",n);   

Three is a crowd

 C has only one ternary operator?: . Ternary operator is an operator which takes 3 operands. This operator works like if - else statement.

  int m,n,p; m = 3;n=2;  
  p = m>n?m:n;  
  


The second statement is equivalent to

 if(m>n)  
   p = m;  
 else  
   p = n;  
You can write complete code to print even-ness of a number by tweaking this operator.

 int m;
 scanf("%d",&m);
  (m%2)?printf("Odd"):printf("Even);

Where is the address?     

 You can get the address of a variable using & operator. Please note that, & is also used for bitwise  and operation and &&  is logical and. 

int a =0;
 int *ptr = &a;//& is address operator
 &a gives "address of" a. 

 ptr = &(a+10)
produces error because a+10 is not lvalue and has no address.

Once we have address, we can get value at that address using * operator  - called indirection operator .

 int b = *ptr;//indirection operator

b is assigned to the value at address in ptr.

Again the onus is on you to ensure that ptr points to correct location. If ptr is uninitialized or has 0 value or any other invalid value - you get the infamous segmentation fault.  

Just a dot 

Operator . is used to access member of a structure or a union. -> arrow operator is used to access member of a structure using a pointer.

 struct student {int id,char name[10]};  
 struct student s;  
 s.id = 10;  
 strcpy(s.name,"FooBar"}; 
 struct student *sptr = &s; sptr->id=11; 

 

And the rest

  There are bitwise operators &,|,!, ^ and complement. And there are left and right shift operators. Read about bitwise operators here



Comments

Popular Posts