Friday, October 25, 2013

Unary operators


Binary operators are the operators which works on two operands at a time.
E.g. z=x+y;
Here, + is binary operator.
X and y are operands.
So, “+” operator works on two operands (Here x and y) at a time. Unlike binary operators, unary operator works on only one operand at a time.
Note that, unary operators have higher precedence than arithmetic operators. Another main difference between unary and binary operator is the associativity. The associativity of unary operator is from right to left, whereas the associativity of binary operator is from left to right. We will see the concept of associativity later on. In C you can use the following unary operators.
1) Unary minus (-): Unary minus change the sign of operand it preceding.
E.g. -786,    -temp,         5E-5  etc.
2)Unary not or Logical not or Negation: It is used to negates the condition. The following examples should better clarify this concept.
E.g. if(!TRUE) is same as
       if(FALSE)
Similarly,
       if(!FALSE) is same as
       if(TRUE)
Where TRUE is a symbolic constant having nonzero value (E.g. 100,-100, 1 etc.), and FALSE is a symbolic constant having value 0. Remember that, any nonzero value is treated as true value in C and 0 is treated as falsity.
Consider an example,
          !(ZZ>=5) is same as      
            ZZ<5
3)Increment and decrement operator (++ and --): Increment operator increment the value of the variable by 1, and the decrement operator decrease the value of the variable by 1.
E.g. i=5;
       i++;
Increments the value of I by 1. So, after evaluation of the statement i++, I contains a value of 6. instead of i++, you can write ++I also. But there is slight difference between the two. We will see it later on.
Similarly, we can decrement the value of the variable by 1 using:
E.g. i=5;
       i--;
Decrement the value of I by 1. So, after evaluation of this statement i contain a value of 4. Here also you can write --i instead of i--.
4)Address of operator (&): The address of operator is used to find the address of the variable it precedes.
E.g. printf(“The address of the variable x=%u”,&x);
Note: You can use %p or %u format specifier for printing the addresses of the variables.
Consider the following example,
Program 6:
#include <stdio.h>
#include <conio.h>
void main()
{
     int x;
     clrscr();
     printf(“\nThe address of the variable x is=%u”,&x);     
     printf(“\nThe address of the variable x is=%p”,&x);
     getch();
}
Run the program, you will get the following output (in your case it may be different, because the compiler maqy select any available address as well.)
Output:
#-------------------------------------------------------------------------------#
The address of the variable x is=65524
The address of the variable x is=FFF4
#-------------------------------------------------------------------------------#        

What? Two different outputs for same variable? Absolutely nooooo. Every variable can have only one unique address assigned by the compiler at run time. Then what is the mystery behind this output? Actually, the address we have printed using %p format specifier is hexadecimal equivalent of its address 65524.So, both 65524 and FFF4 represents same location in the memory. Now, it is totally up to you to decide whether you want to print your variable addresses using decimal notation or hexadecimal notation. Try using %x or %X format specifier to print the address of the variable x. You will get the same output as produced by %p but %X will print the output in upper case letter whereas %x will print the output in lowercase letters.
4) sizeof(): sizeof() operator returns the size of its operands in bytes.
E.g. sizeof(x) ;
Gives the number of bytes allocated to the data type of x. Consider the following example.
Program 6:
#include <stdio.h>
#include <conio.h>
void main()
{
     char a;
     int b;
     float c;   
     clrscr();
     printf(“\nThe number of bytes reserved for character is=%d”,sizeof(a));
     printf(“\nThe number of bytes reserved for integer is=%d”,sizeof(b));
     printf(“\nThe number of bytes reserved for float is=%d”,sizeof(c));
     getch();
}

Run the program and you will get following output.

#-------------------------------------------------------------------------------#
The number of bytes reserved for character is=1
The number of bytes reserved for integer is=2
The number of bytes reserved for float is=4
#-------------------------------------------------------------------------------#        

In above program we use %d to print the size of the variables as the sizeof() returns the size of operands in an integer format.
Now, try to run the following code.
Program 6:
#include <stdio.h>
#include <conio.h>
void main()
{
     clrscr();
     printf(“\nThe number of bytes reserved for float is=%d”,sizeof(3.14));
     getch();
}
Run it, and observe the unexpected output.
#-------------------------------------------------------------------------------#
The number of bytes reserved for float is=8
#-------------------------------------------------------------------------------#        

In the previous program the size reported by the compiler for float is 4 bytes and now it report 8 bytes! How?

Note: A floating point number is specified by the float keyword in the C language. Floating point numbers can be suffixed with the letter f or F to specify float explicitly. A floating point number without suffix is double by default.

Therefore the correct version of the above program is:

#include <stdio.h>
#include <conio.h>
void main()
{
     clrscr();
     printf(“\nThe number of bytes reserved for float is=%d”,sizeof(3.14F));
     getch();
}
Run it, and you will get the expected output.
#-------------------------------------------------------------------------------#
The number of bytes reserved for float is=4
#-------------------------------------------------------------------------------#        

Note: if sizeof() is being applied to the type then parenthesis () are required, otherwise they are optional.
So, the following code is valid one.
Program 9:
#include <stdio.h>
#include <conio.h>
void main()
{
     clrscr();
     printf(“\n%d”,sizeof 3);
     getch();
}
But, this is invalid.

#include <stdio.h>
#include <conio.h>
void main()
{
     clrscr();
     printf(“\n%d”,sizeof int);
     getch();
}

Now, consider the another example using sizeof which will produce the unexpected output.

#include <stdio.h>
#include <conio.h>
void main()
{
     clrscr();
     printf(“\n%d”,sizeof (‘a’));
     getch();
}

Run the program now.
Output:
#-------------------------------------------------------------------------------#
  2
#-------------------------------------------------------------------------------#        
Remember that the character constants in C are of type int, so sizeof(‘a’) is equal to sizeof(int).
6) Cast unary operator: This operator is used to change the type of the variable, constant or expression explicitly.
E.g. (int)3.14159;

Change the value of operand to 3.

No comments:

Post a Comment