Pages

Operators and Expression

Assignment Operator:

Assignment operators are used to assign a value or an expression or a value of a variable to another variable.

Syntax                      variable=expression (or) value ;

Description               Variable is any valid 'c' variable, assigned value can be anything.

Example                x=10;
                             x=a+b;
                             x=y;
Example 
Program to demonstrate assignment operator.

/*  Demonstration of assignment operator * /
#include<stdio.h>
#include<conio.h>
void main()
{
       int i,j,k; /*  Local definitions * /
        /*  Statements * /
       clrscr();
       k =  5;  /*  = is a assignment operator * /
       printf("k = %d",k);
       getch();
}/*  main * /

Output:
            k=5

Arithmetic Operator:

'C' allows us to carryout basic arithmetic operations like addition, subtraction, multiplication, and division. The following table shows the Arithmetic Operators and their meaning.

Operator          Meaning                 Examples                                 

+                     Addition                   2+9= 11

-                     Subtraction               9 - 2= 7

*                     Multiplication            2*9= 18

/                      Division                     9/3= 3

%                 Modulo division         9 % 2= 1

Example 1
Program to illustrate the usage of arithmetic operator.
/*  Usage of arithmetic operators * /
#include<stdio.h>
#include<conio.h>
void main()
{                                                                                                    
     int i, j,k;     /*  Local definitions * /
     /*  Statements * /
     clrscr();
     i = 10;                                                                                                
     j = 20;                                                                                                 
     k = i +  j;                                                                                        
     printf ("Value of k is %d\n ", k);
     getch();
}/*  main * /

Out put:
             Value of k =30

Increment and Decrement Operator:

'C' has two way useful operators not generally found in other languages, these are the increment (++) and decrement ( -- ) operators. The'++' dds one to the variables and '--' subtracts one from the variable. These operators are called unary operators. Because they acts upon only one variable.

Operator                        Meaning

++x                                Pre increment

--x                                  Pre decrement

x++                                Post increment

x--                                 Post decrement

Example:
 Program using Increment and Decrement operators.

/*  Program using increment and decrement operators * /
#include<stdio.h> 
main () 
int a=10; /*  Local definition * / 
 /*  Statements * /
printf("a++= %d\n", a++);   /*  ++is a post increment * / 
printf("++b = %d\n", ++b);/*  ++is a pre-increment * / 
printf("--a = %d\n", --a);/*  -- is a pre-decrement * / 
printf("b-- = %d\n", b--);/*  -- is a post-decrement * /

} /*  main * /

Out put:
            a++ = 10

Relational Operator

Relational operators are used to compare two or more operands. Operands may be variables, constants or expression. For example we may compare the age of two persons, or the price of two items and so on. These comparisons can be done with the help of relational operators.

The following table shows the Relational Operators.

Operator        Meaning                                            Example                 Return value

  <                  is less than                                             2<9                           1

  <=                is less than or equal to                           2 <= 2                       1

   >                 is greater than                                        2 > 9                         0

   >=               is greater than or equal to                      3 >= 2                       1

   ==               is equal to                                             2 == 3                       0

   !=                is not equal to                                       2! = 2                        0

Example :
Program to use various relational operators and display their return values
/* Program to use various relational operators */

#include <stdio.h>
#include <conio.h> 
void main() 
{                                                                                                     
/* Statements */ 
clrscr(); 
printf("\nCondition  : Return values \n");
printf("\n5!=5       : %5d",5!=5); /* != is assignment operator */ 
printf("\n5==5      : %5d",5==5); /* == is assignment operator */ 
printf("\n5>=50    : %5d",5>=5); /* >= is assignment operator */ 
printf("\n5<=50    : %5d",5<=50); /* <= is assignment operator*/
printf("\n5!=3       : %5d",5!=3); 
}/* main */



Logical Operator:

Logical operators are used to combine the results of two or more conditions. 'C' has the following logical operators.

Operator            Meaning            Example                              Return value

&&                    Logical AND       (9>2)&&(17>2)                            1

||                       Logical OR           (9>2)||(17==7                            1

!                        Logical NOT           29!=29                                    0

Example 
                                                                                       
Program to demonstrate logical operator.
/* Program on demonstration of logical operator */
#include<stdio.h>
#include<conio.h>
void main( )
{
int c1,c2,c3;                                                                                    /* Local definitions */ 
/* Statements */ 
clrscr();
printf("ENTER VALUES OF c1, c2 AND c3 : ");
scanf("%d%d%d",&c1,&c2,&c3);
if((c1 < c2)&&(c1<c3))                                                      /* && is a logical operator */
printf("\n c1 is less than c2 and c3");                                              
if (!(c1< c2))
printf("\n c1 is greater than c2");
if ((c1 < c2)||(c1 < c3))                                                                    /* || is a logical operator */
printf("\n c1 is less than c2 or c3 or both");
getch();
}/* main */

Bitwise Operator:

Bitwise operators are used to manipulate the data at bit level. It operates on integers only. It may not be applied to float or real. The operators and its meaning are given below.

Operator                                    Meaning

&                                               Bitwise AND

|                                                 Bitwise OR

^                                                Bitwise XOR

<<                                             Shift left

>>                                             Shift right

~                                               One's complement

Conditional Operator:

Conditional operator itself checks the condition and executes the statement depending on the condition.
Example                                                                                        
main()

{

 int a=5,b=3,big;
big=a>b?a:b;
printf("Big is...%d",big);

}

Output:
           Big is ...5

Sizeof() Operator:

The Special Operator

Along with these operators, the C language supports some of the special operators given below.

Operators                                          Meaning

,                                                       comma operators

sizeof                                               size of operators

& *                                                   pointer operators

 -->                                                 Member selection operators

    (a)    Comma operator(,) : Usually the comma operator is used to separate the statement elements such as variables, constants or expression etc, and this operator is used to link the related expressions together, such expressions can be evaluated from left to right and the value of right most expressions is the value of combined expression.

     (b)   The sizeof() operator : The sizeof() is a unary operator, that returns the length in bytes of the specified variable, and it is very useful to find the bytes occupied by the specified variable in the memory.

      (c)    Pointer operators :

& : This symbol specifies the address of the variable.

                   * : This symbol specifies the value of the variable.

      (d)    Member selection operators : 
        --> : These symbols used to access the elements from a structure.
  
Example
Program to illustrate sizeof operator. 
/* Program to illustrate sizeof operator */
#include<stdio.h>
#include<conio.h> 
struct person {char name[50]; int age; float height; }; /* structure */ 
void main()
/* Local definitions */
int num=1234567890;
float dec= 0.123456;
double ext=0.123456789;
char ltr='A';
char str[]="Something to write home about...";
struct person boy;
/* Statements */
clrscr();
printf("Size of num int is %d bytes\n",sizeof num);
printf("Size of dec float is %d bytes\n", sizeof dec);
printf("Size of ext double is %d bytes\n",sizeof ext);
printf("Size of ltr char is %d bytes\n",sizeof ltr);
printf("Size of str string is %d byte\n", sizeof str);
printf("Size of boy struct is %d bytes\n",sizeof boy);
getch();
} /* main */

Expression:
An expression represents data item such as variables, constants and are interconnected with operators as per the syntax of the language. An expression is evaluated using assignment operator.

Example :
                 y=(a/b)+c;
                 z=(a*b)-d;


No comments:

Post a Comment