Saturday, October 3, 2020

Different type of operators and type casting in C programming

  •  Airthmetic Operations
                + , - , *, / , %
  • Relation Operations
                <. <=, >, >=, ==, !=
  • Logical Operations
                && logical AND
                ||   logical OR
                !    logical NOT
  • Assignment operations
                a = a+ 1  is called a += 1
  • Increment/Decrement operators
            ++  and --  with prefix and postfix

  • Bitwise Operations
            &     bitwise AND
            |     bitwise OR
            ^     bitwise exclusive OR    
            <<    bitswise left shift
            >>   bitwise right shift

  • Special operators
            sizeof operators
            pointer operators & and *
            member selection operator  . and  ->
            comma operator (used in for and while loop to seperate several condition)


Implicit Type cast

When we have different kind of constants or variable of different types in an expression, C automatically convert lower type of operand to highter type operand and perform operation. Result will be in higher type operand.

Explicit Type Cast

In some case, we need to ask C to first perform type cast and perform operation.

float ratio;
int female_numbers
int male numbers

ration =  female_numbers / male_numbers 

Here we will loose fractional part of the answer. so we need to solve this as below

ration = (float) female_numbers/male_numbers

Here first female_numbers and male_numbers will be converted to float and then answer will be assigned to ration. No fractional value will be lost. 

Operator Precedence and Associativity in C


No comments:

Post a Comment