What are operators and expressions? Explain different types of operators and expressions with examples.

1 year ago
C Programming

One of the most important features of C is that it has a very rich set of built in operators including arithmetic, relational, logical, and bitwise operators.

  • An operator can be any symbol like + - * / that specifies what operation need to be performed on the

For ex: + indicates addition operation

* indicates multiplication operation

  • An operand can be a constant or a
  • An expression is combination of operands and operators that reduces to a single

For ex: Consider the following expression a + b here a and b are operands, while + is an operator.

Types of Operators and Expressions are:

  1. Arithmetic Operators and Expressions
  2. Assignment and Compound Assignment Operators
  3. Increment and Decrement Operators
  4. Relational Operators and Expressions
  5. Logical Operators and Expressions
  6. Conditional Operators and Expressions
  7. Bitwise Operators and Expressions
  8. Special Operators

1)     Arithmetic Operators and Expressions:

Arithmetic operators are      + - *  /  and %

Arithmetic expressions are expressions which contains only arithmetic operators in it.

+ performs addition               - performs subtraction            * performs multiplication

/ performs division operation

% modulus or reminder operation For Example:-

 

int a = 5, b = 2, x ;

float c = 5.0, d = 2.0, f ;

x = a / b ; // integer division(5/2), therefore, x = 2.

f = c / d ; // floating point division(5.0/2.0), f = 2.5. x = 5 % 2 ; //  remainder operator, x = 1.

x = 7 + 3 * 6 / 2 - 1 ;    // x=15,* and / evaluated ahead of + and -. x = 7 + ( 3 * 6 / 2 ) - 1 ; // x = 15

x = ( 7 + 3 ) * 6 / ( 2 - 1 ) ; // changes order of evaluation, x = 60 now.

 

2)      Assignment Operators: Used to initialize the variables with value. Assignment operators is =

Compound assignment operators are +=,-=,/=,*=,%=. These are also called as shorthand operators.

Many C operators can be combined with the assignment operator as shorthand notation For Example :- x = x + 10 ; can be replaced by          x += 10 ;

Similarly for -=, *=, /=, %=, etc.

These shorthand operators improve the speed of execution as they require the expression, the variable x in the above example, to be evaluated once rather than twice.

 

3)     Increment and Decrement Operators

There are two special unary operators in C, Increment ++, and Decrement -- , which cause the variable they act on to be incremented or decremented by 1 respectively.

For Example :-            x++ ;      /* equivalent to  x = x + 1 ;    */

x-- ;     /* equivalent to x = x-1; */

++ and -- can be used in Preincrement/Predecrement Notation( Prefix Notation) or Postincrement/Postdecrement Notation(Postfix notation).

in Preincrement/Predecrement Notation

  1. The value of the variable is either incremented or decremented first
  2. Then, will use updated value of the variable But in Postincrement/Postdecrement Notation

 

  1. The value of the variable is used first
  2. Then the value of the variable is incremented or decremented For Example :-

int i, j = 2 ;

i = ++ j ; /* preincrement: therefore i has value 3, j has value 3 */ int i, j = 2 ;

i = j++ ; /* postincrement: therefore i has value 2, j has value 3  */

 

4)   Relational Operators: The operators which are to compare or to check the relation between two or more

Relational Expressions are expressions that contains only relational operators. The full set of relational operators are provided in shorthand notation

is greater than

>=

is greater than or equal to

is less than

<=

is less than or equal to

 

==        is equal to                   !=        is not equal to

The result of any relational expression is always either TRUE or FALSE. Example 1:-

int x=2;

if ( x == 2 )     /* x ==2 is TRUE since x value is 2 */

printf( “x is equal to 2\n” ) ; // prints x is equal to 2

 

 

 

Example 2:-

 

else

 

 

int x=5;

 

printf( “x is not equal to 2\n” ) ;

 

if ( x == 2 )     /* x ==2 is False since x value is 5 */

printf( “x is equal to 2\n” ) ;

 

else

 

printf( “x is not equal to 2\n” ) ;       // prints x is not equal to 2

 

5)       Logical Operators: The operators which acts on only two logical values i.e. true and

Logical expressions are expressions that contains only logical operators. Usually to combine one or more relations these logical operators are used. Hence such expressions are called as relational logical expressions.

&&     Logical  AND                                     | | Logical OR

!           Logical NOT

NOTE: Since C has no boolean datatype so to use these operators one must need to remember that

  • ZERO(0) is always treated as FALSE and vice versa
  • NON-ZERO (>0 or <0) value is always treated as TRUE is always represented as 1.

For Example :-

if ( x >= 0 && x < 10  )

printf( “ x is greater than or equal to zero and less than ten.\n” ) ;

For Example :- 2 > 1 -- TRUE so expression has value 1 2 > 3 -- FALSE so expression has value 0

i = 2 > 1 ; -- relation is TRUE -- has value 1, i is assigned value 1

NOTE: Every C expression has a value. Typically we regard expressions like 2 + 3 as the only expressions with actual numeric values. However the relation 2 > 1 is an expression which evaluates to TRUE so it has a value 1 in C. Likewise if we have an expression x = 10 this has a value which in this case is 10 the value actually assigned.

6)       Conditional Operators: the operators ?: is called as conditional operator and it is also called as ternary operator since it operates on three

Syntax:           expression1?expression2:expression3;

In the above conditional expression, if expression1 is TRUE then returns the value of expression2 otherwise returns the value of expression3.

For example:-

5>12 ? 11: 12;             // returns 12, since 5 not greater than 12.

 

10!=5 ? 4 : 3;              // returns 4, since 10 is not equal to 5.

12>8 ? a : b;                // returns the value of a, since 12 is greater than 8.

7)    Bitwise Operators: These operators performs operations on each individual bits of the data value rather than on usual data Hence the name bitwise.

These are special operators that act on char or int variables only. & Bitwise AND         | Bitwise OR

^ Bitwise XOR                                    ~ Ones Complement

>> Shift Right                                     << Shift left

Recall that type char is one byte in size. This means it is made up of 8 distinct bits or binary digits normally designated as illustrated below with Bit0 being the Least Significant Bit (LSB) and Bit 7 being the Most Significant Bit (MSB).

The value represented below is 13 in decimal.

 

Bit7(MSB)

Bit6

Bit5

Bit4

Bit3

Bit2

Bit1

Bit0(LSB)

0

0

0

0

1

1

0

1

An integer on a 16 bit OS is two bytes in size and so Bit15 will be the MSB while on a 32 bit system the integer is four bytes in size with Bit31 as the MSB.

Bitwise AND( & )

RULE : If any two bits in the same bit position are 1 then the resultant bit in that position is 1 otherwise it is zero.

For Example :-         1011 0010 (178) & 0011 1111 (63) = 0011 0010 (50)

Bitwise OR ( | )

RULE : If any two bits in the same bit position are 0 then the resultant bit in that position is 0 otherwise it is 1.

For Example :-            1011 0010 (178) | 0000 1000      (8) = 1011 1010 (186)

Bitwise XOR ( ^ )

RULE : If the bits in corresponding positions are different then the resultant bit is 1 otherwise it is 0.

For Example :-     1011 0010     (178)            ^       0011 1100 (60) =     1000 1110                             (142)

Shift Operators, << and >>

RULE : These move all bits in the operand left or right side by a specified number of places.

Syntax :            variable << numberOfPlaces

variable >> numberOfPlaces

For Example :- 2 << 2 = 8 i.e.  0000 0010 becomes  0000 1000

NB :    shift left variable by numberOfPlaces multiplies variable by 2numberOfPlaces

i.e. resultant value = variable *  2numberOfPlaces

shift left variable by numberOfPlaces divides variable by 2numberOfPlaces

i.e. resultant value = variable / 2numberOfPlaces

Ones Complement ( ~ )

RULE : bit 1 is set to 0 and bit 0 is set to 1. i.e. Each bit is negated. For Example :-   1101 0011 becomes 0010 1100

Note: With all of the above bitwise operators we must work with decimal, octal, or hexadecimal values as binary is not supported directly in C.

The bitwise operators are most commonly used in system level programming where individual bits of an integer will represent certain real life entities which are either on or off,

 

one or zero. The programmer will need to be able to manipulate individual bits directly in these situations. A mask variable which allows us to ignore certain bit positions and concentrate the operation only on those of specific interest to us is almost always used in these situations. The value given to the mask variable depends on the operator being used and the result required.

For Example :- To clear bit 7 of a char variable. char ch = 89 ; // any value

char mask = 127 ; // 0111 1111

ch = ch & mask ; // clears bit7 of a variable ch ; For Example :- To set bit 1 of an integer variable.

int i = 234 ; // any value

int mask = 2 ;  // a 1 in bit position 2

i = i | mask ; // sets the bit 1 of variable i

 

8)     Special Operators: the following are called as special operators

 a)  sizeof operator: used to determine the size, in bytes, of the variable or The sizeof operator returns the  amount of memory, in bytes, associated with a variable or a type.

Syntax:           sizeof ( expression ) For Example:-

int x , size ;

size = sizeof(x) ;         // sizeof returns size of integer variable x

printf(“The integer x requires %d bytes on this machine”, size);

printf( “Doubles take up %d bytes on this machine”, sizeof (double) ) ;

// prints the size of double data type in bytes

b) Type Cast operator:

Type cast operator is used to convert one type of data to specific type of data in the expression which has the following syntax.

Syntax:           ( type ) expression For Example,

if we have an integer x, and we wish to use floating point division in the expression x/2 we might do the following

( float ) x / 2

which causes x to be temporarily converted to a floating point value and then implicit casting causes the whole operation to be floating point division.

The same results could be achieved by stating the operation as x / 2.0

which essentially does the same thing but the former is more obvious and descriptive of what is happening.

c) Comma operator – used to separate the arguments in function header, variables in declaration, combine more than one statement in a single line, etc. For Example,

int a,b,c;          // comma separates three variables a=b,c=10,d=100;  // comma separates three statements

Sanisha Maharjan
Jan 20, 2022
More related questions

Questions Bank

View all Questions

Top