Operators in C

 

 Operators in C

Following are the different types of operators used in c programming

Arithmetic operators

Relational operators

Logical  operators 

Increment and Decrement operators 

Assignment operators 

Bitwise Operator 

Conditional operators 

   Arithmetic operators:  (  +  - * / % )

These operators are used to perform various arithmetic operations such as addition, subtraction, multiplication, division, and modulus.

 Ex.       float a=8.0,b=4.0, c;

              c=a/b;     /* gives output 2.because quotient is 2.0*/

              c=a%b;             /* gives output 0 because remainder is zero */

Example on Operator in C

 Relational Operators: ( < >  <= >=   == != )

These operators are used to check the relation between the values the above table shows various relational operators.

 Sometimes confusion may occur in equal to operator (==) and assignment operator(=) both having different meanings in c programming.

Equal to operator just check equality while the assignment operator assigns the value which is at the right of = sign to the variable at the left side of = sign.

Logical Operators: ( && ||^)

Thlogical  AN operator  (&&  evaluates thtruth or falseness opairs of expressions. if both expressions are true, the logical AND operator returns 1. Otherwise, the operator returns 0.

However, the logical OR operator (||) returns 1 if at least one of the expressions is true. Th|| operator returns 0 if both expressions are false.

Only one operand (or expressioncan be taken bthe logical negation operator (!). If the operand is true, the (!) operator returns 0; otherwise, the operator returns 1

Increment  and  Decrement  operators :  ( ++ -- )

Th incremen an decremen operators  are  ++  an - respectivel we use these increments or decrement the value of an associated variable by 1 only.

The increment/decrement operators may be of the prefix (before variable) or postfix form (after variable)

 Prefix form:           Ex. Assuming i as variable

 ++i;             is equivalento            i=i+1;

 --i;              is equivalento            i=i-1;

Postfix form:

  i++;             is equivalento            i=i+1;

  i--;              is equivalento            i=i-1;

The difference between prefix anpostfix forms observed when these operators are used as part of a larger expression or in the value assignment case.

If (++i) is used in an expression, it means the value of  i is incremented before the expression is evaluated.

If (i++) is used in an expression, that means the value of  i is incremented after the expression is evaluated.

Ex. Assume that the integer variables i is initialized to a value 2

Then in the following statement

                  x=++i /* Gives output i=3 and x=3*/

                    x=i++; /* Gives output i=3 but x=2

because here we are applying the increment operators after the variable name ‘i’ */

Assignment Operator: ( = )

The assignment operator is a simple (=) sign. It is used to assign valueto a variable,

 The expression may be a variable, a value, literals, and operators combination.

The assignment operation always takes from the righto the left.

    Ex.  a=b+c;          /* value of b added with ‘c’ the result assigned to ‘a’ */

 The shortcucan be possible with assignment operators like,

           a=a+b;   /* this can be written as a+=b;    

Bitwise Operator:(  & | ^  ~ )

Using C you can access and manipulate the bits in the variables you can perform bitwise AND, OR, XOR, Complement operation as well right shifting and left shifting of bits. Ex. 

the following table shows the truth table for AND, OR, XOR.

  

 Shift Operators: (<<  >>)   

Right Shift (>>) - It will shifthe bits of operand towards the right; the blanks are created on the left side which is filled bzero(s). In general, declared as

Variable >> number of bits

Ex. if the variable a contains the bit pattern 11010011, then, a >> 1 would give

01101001 and a>> 2 would give 00110100.

Left Shift (<<) - It will shift the bits of operand towards the left; the blanks are created on the right side which is filled bzero(s). In general, declared as:

Variable << number of bits

if the variable a contains the bit pattern 11010011, then, a << 1 would give

10100110and a<< 2 would give 01001100

 

Conditional Operators: ( ? :)   

In the conditional operator, we are writinthree expressionsthe first expression1 is always a condition that is to be checked, if this condition is true then expressionwill be selected as output, if the condition is false then expression3 will be selected as the output.

Ex.        int a=15,b=5;

(a>b)?

printf(a is larger) : printf(“b is larger”);

  In this case output will be

is larger

 

 

Previous
Next Post »