CSC115 C Programming

C ProgrammingTU Board 2081Unit 4

List different types of operators and explain any four of them.

10

Answer

An operator is a symbol that tells the compiler to perform an operation on values (operands). C has these types of operators:

  1. Arithmetic operators: + - * / %
  2. Relational operators: < <= > >= == !=
  3. Logical operators: && || !
  4. Assignment operators: = += -= *= /= %=
  5. Increment and decrement operators: ++ --
  6. Conditional (ternary) operator: ? :
  7. Bitwise operators: & | ^ ~ << >>
  8. Special operators: sizeof, comma ,, address &, pointer *, member . and ->

1. Arithmetic operators

Used for calculations. / between two integers gives an integer (the fraction is dropped), and % gives the remainder.

int a = 17, b = 5;
printf("%d %d %d", a + b, a / b, a % b);   /* 22 3 2 */

2. Relational operators

Compare two values. The result is 1 (true) or 0 (false).

int x = 10, y = 20;
printf("%d %d", x < y, x == y);            /* 1 0 */

3. Logical operators

Combine conditions. && is true only if both sides are true, || if at least one side is true, and ! reverses a condition.

int age = 20, marks = 65;
if (age >= 18 && marks >= 60)
    printf("Eligible");

4. Increment and decrement operators

++ adds 1 and -- subtracts 1. In prefix form (++a) the value changes before it is used; in postfix form (a++) it is used first and changed afterwards.

int a = 5, b, c;
b = ++a;   /* a = 6, b = 6 */
c = a++;   /* c = 6, a = 7 */

(The conditional operator is another common choice: max = (a > b) ? a : b; picks the larger of two numbers.)

Discussion

Loading…

All C Programming old questions