C – Operators

Operators

  • arithmetic : +, -, *, /, %
  • assignment : =
  • augmented assignment : +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
  • bitwise logic : ~, &, |, ^
  • bitwise shifts : <<, >>
  • boolean logic : !, &&, ||
  • conditional evaluation : ? :
  • equality testing : ==, !=
  • calling functions : ( )
  • increment and decrement : ++, —
  • member selection : ., ->
  • object size : sizeof
  • order relations : <, <=, >, >=
  • reference and dereference : &, *, [ ]
  • sequencing : ,
  • subexpression grouping : ( )
  • type conversion : (typename)

Address of Operator (Address of variable

  • & // Address of Operator – It cannot be used on constants.It cannot be used on variable which are declared using register storage class.

main() -function

  • main() – main is a function. Every function has a pair of parentheses ()
  • Can a program be compiled without main() function?
  • Yes, it can be but cannot be executed, as the execution requires main() function definition.

function

  • () - function -Every function has a pair of parentheses ()

semicolon

  • ; // Any C statement always ends with a ;

Comma Operator

  • , Comma Operator – can be used to separate two or more expressions
  • Eg: printf(hi) , printf(Hello);

scope or block

  • A local block is any portion of a C program that is enclosed by the left brace { and the right brace } .
  • anything between the two braces is contained in a local block.
  • {} // scope or block
    • Increment Operator
      • ++x (pre increments)
      • x++ (post increments)
      • Eg:
        • S++; // ++, as it is single machine instruction (INC) internally.(recommended)
        • S = S+1; // ++, as it wil take two machine cycle internally.(not recommended)
    • Decrements Operator
      • --x (pre Decrements)
      • x-- (post Decrements)

Boolean Operator

  • == Equal
  • != Not equal
  • > Greater than
  • >= Greater than or equal
  • < Less than
  • <= Less than or equal
  • && Logical AND
  • || Logical OR
  • ! Logical NOT

 

Operators

  1. Arithmetic Operators
    • Unary Operators
      • Eg: +5 , -8 .
    • Binary Operators
      • Eg
        • X = 5+6; (Addition + Operator)
        • X = 20-10; (Subraction - Operator
        • X = 5*3; (Multiplication * Operator)
        • X = 5/3; (Division / Operator)
        • X = 5%3; (Modular % Operator) -remainder
  2. Relational Operators
    • A>B; (Greater than)
    • A<B; (lesser than)
    • A>=B; (Greater than equal to)
    • A<=B; (Lesser than equal to)
    • A==B; (equal to)
    • A!=B; (not equal to)
  3. Logical Operator
    • A && B; (AND)
    • A || B; (OR)
    • !A=A; (NOT)
  4. Increment and Decrements Operator
    • Increment Operator
      • ++x; (pre increments)
      • x++; (post increments)
        • Eg:
        • S++; // ++, as it is single machine instruction (INC) internally.(recommended)
        • S = S+1; // ++, as it wil take two machine cycle internally.(not recommended)
    • Decrements Operator
      • --x; (pre Decrements)
      • x--; (post Decrements)
  5. Short hand assignment Operators
  6. conditional operators (Ternary Operator)

syntax : expression 1 ? expression 2 : expression 3;

  • Rule
    • if expression 1 is true then expression 2 is executed
    • else expression 1 is false then expression 3 is executed
  • Advantage : Using ?: reduce the number of line codes and improve the performance of application.
  • Example Invalid:In this below example this is an error in this line i>45? return(*P): return (*q); We cannot use return keyword in the terenary operators.

     
  • Example Valid : In this below example a is lesser than b . so the b value
  • More examples :
    • Find largest number among 3 numbers using ternary operator

Associativity

Associativity is only needed when the operators in an expression have the same precedence. Usually + and – have the same precedence.

Consider the expression 7 – 4 + 2. The result could be either (7 – 4) + 2 = 5 or 7 – (4 + 2) = 1. The former result corresponds to the case when + and – are left-associative, the latter to when + and – are right-associative.

Usually the addition, subtraction, multiplication, and division operators are left-associative, while the exponentiation, assignment and conditional operators are right-associative. To prevent cases where operands would be associated with two operators, or no operator at all, operators with the same precedence must have the same associativity.

 

 


 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

if you have any queries call us +918300026060 or WhatsApp Dismiss

0

Your Cart

%d bloggers like this: