Tuesday 12 December 2017

Operators in C Program

  • Operators are the symbols used between two operands to perform a particular operation.
  • Operators are used to perform logical and mathematical operations in C Programming. 
  • The operators join individual constants and variables to form expression. 
  • Operators, Functions, Constants and Variables combined together to form expressions.
  • Let's take an example- a * b + c
    • Here * and + are the Operators.
    • 'a', 'b' and 'c' are the variables and we could take some constants.
    • Here the variable and constants are called as Operands.
    • All are combined to form an expression.
  • In general, we could divide the operator into three types as;
    1. Unary Operator
    2. Binary Operator
    3. Ternary Operator
  • But as per our use, we could divide the operators into the following types;
    1. Arithmetic Operator
    2. Assignment Operator
    3. Relational Operator
    4. Logical Operator
    5. Conditional Operator
    6. Bitwise Operator
    7. Increment/Decrement Operator 
    8. Special Operator

Unary Operator

  • The operator which act on single operand is known as Unary Operator.
  • It takes only one operand.
  • It is used to define the +ve and -ve values with the signs like + or-.
    • Ex: -a, -b, -3, +5, etc. 
    • We won't use the + symbol before any positive number, so we usually write the value only.
  • Increment/Decrement operators are Unary operators.
    • Ex: --a, ++b, etc;

Binary Operator

  • The operators which act on two operands is known as Binary Operator.
  • It takes two operands. 
  • All the 2nd type of operators list described above are considered as binary type except Increment/Decrement operator.

Ternary Operator

  • The operators which act on three operands is known as Ternary Operator.
    • Ex: a?b:c
    • The expression in the example is called as Conditional Operator.
    • Here 'a' is the expression and 'b' is the true statement and 'c' is the false statement.
    • Syntax- (expression)?true statement:false statement;
      • Ex: (a>18)?printf("Eligible to caste for vote."):printf("No eligible for caste.")

1. Arithmetic Operator

  • These operators are used for the basic and common mathematical operations like addition, subtraction, multiplication, division etc.
  • Operators are:
    • + : Addition Operator
    • - : Subtraction Operator
    • * : Multiplication Operator
    • / : Division Operator
    • % : Modulus Operator
      • The modulus operator is always applicable for the integer.
      • In the modulus operator, the operator sign will depend always on the sign of the 1st operand.
    • ^ : Caret or Power
      • Ex: 3^2 = 32

2. Assignment Operator

  • Assignment operators are used to for assigning a variable to a value.
  • Mainly the '=' symbol is used for assigning.
  • x=2;
    • Here 2 is assigned to the variable x.
  • x+=2;
    • It means x=x+2;
  •  We could use x-=2; for x=x-2; and so on for others.

3. Relational Operator

  • Relational operators are used to compare the relationship between two operands.
  • < : Less than
    > : Greater than
    <= : Less than or equals to
    >= : Greater than or equals to
    == : Equals to
    != : Not equals
  • The relational operator returns true of false value.

4. Logical Operator

  • Logical operators are used to perform the logical operations.
  • && : Logical AND
    || : Logical OR
    ! : Logical NOT
    !(&&) : Logical NAND
    !(||) : Logical NOR

5. Conditional Operator

  • The conditional operators are used to check whether a given condition is true or false and generate the statement accordingly.
  • The conditional operator is the Ternary operator.
  • The syntax is:
    • (condition)?true statement:false statement;
  • If the given condition would true, then the true statement would generate and if not, then the false statement would generate.

6. Bitwise Operator

  • C has a distinction of supporting special operators known as Bitwise operator.
  • These operators are used for manipulation of data at the bit level.
  • A bitwise operator operates on each bit of data.
  • & : Bitwise AND
    | : Bitwise OR
    ^ : Bitwise Exclusive
    << : Shift Left
    >> : Shift Right

7. Increment/Decrement Operator

  • These operators are used to increment or decrement the value by 1.
  • These are the unary operators.
  • Syntas: Increment/Decrement_operator Variable_name;
  • Ex:
    • ++a;
    • ++b;
    • --a;
    • --b;
  • The above syntax and example are know as pre-increment or pre-decrement.
  • Varibale_name Increment/Decrement_operator;
  • Ex:
    • a++;
    • b++;
    • a--:
    • b--;
  • These are called post-increment or post-decrement.

8. Special Operator

  • C Programming Language supports some special operatos like Comma (,), Address (&), Pointer (*), sizeof().
  • The comma operator is used to link related expression together. It is evaluated from left to right.
  • The address operator is used to display the location value of a variable.
  • The pointer operator is used to display the vaulue stored in a given address.
  • The sizeof() operator is used to display the size of a datatype.

1 comment:

  1. Leovegas - Play Live Casino Games for Free at Gold Casino
    Enjoy our fantastic leovegas casino games 1XBET with a wide range of unique online casino games available, including slots, roulette, blackjack and more! Rating: 4.2 · ‎12 bk8 votes

    ReplyDelete