Skip to main content

2. Operators

 

Arithmetic Operators

  1. What is the output of the following C code?
  2. #include <stdio.h>
    int main()
    {
        int x = -3, y = +4;
        printf("%d %d", x, y);
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>
    int main()
    {
        int a = 17, b = 4;
        printf("%d\n, a + b);
        printf("%d\n", a - b);
        printf("%d\n", a * b);
        printf("%d\n", a / b);
        printf("%d", a % b);
    }
  5. What is the output of the following C code?
  6. #include <stdio.h>
    int main()
    {
        float a = 12.4, b = 3.8;
        printf("%.2f\n", a + b);
        printf("%.2f\n", a - b);
        printf("%.2f\n", a * b);
        printf("%.2f", a / b);
    }
  7. What is the output of the following C code?
  8. #include <stdio.h>
    int main()
    {
        int x = 9, y = 2;
        float z = 2.0;
        printf("9/2: %f\n", x / y);
        printf("9/2: %d\n", x / y);
        printf("9/2.0: %f", x / z);
    }

Assignment Operators

  1. What is the output of the following C code?
  2. #include <stdio.h>
    int main()
    {
        int x, y, z, t;
        x = 5, y = 6, z = x + y * 2, t = x;
        printf("x: %d, y: %d, z: %d, t: %d\n", x, y, z, t);

        x, y, z = 10; // x and y values will not change, only z will be assign by 10
        x = y = z = 10;

        x = x + 5;
        x += 5;

        y *= 5;
        z /= 2;
        t %= 5;

        printf("x: %d, y: %d, z: %d, t: %d", x, y, z, t);
        return 0;
    }

Increment and Decrement Operators

  1. What is the output of the following C code?
  2. #include <stdio.h>
    int main()
    {
        int x = 8;
        printf("%d ", x);  
        printf("%d ", ++x);
        printf("%d ", x);  
        printf("%d ", --x);
        printf("%d", x);
        return 0;
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>
    int main()
    {
        int x = 8;
        printf("%d ", x);  
        printf("%d ", x++);
        printf("%d ", x);  
        printf("%d ", x--);
        printf("%d", x);
        return 0;
    }
  5. What is the output of the following C code?
  6. #include <stdio.h>
    int main()
    {
        int a = 10;
        int b, c;
        b = ++a; // b will be assigned the incremented value i.e., 11.
        c = a++; // c will be assigned the value of a before incrementing it i.e., 11.
        b--;    
        --c;    
        b -= c;
        printf("%d", b);
        return 0;
    }
  7. What is the output of the following C code?
  8. #include <stdio.h>
    int main()
    {
        int a = 10;
        int y = (++a) + (--a); // brackets will be evaluated before arithmetic addition
        printf("%d", y);
        return 0;
    }
  9. What is the output of the following C code?
  10. #include <stdio.h>
    int main()
    {
        printf("%d", printf("DCSharma"));
        return 0;
    }
  11. What is the output of the following C code?
  12. #include <stdio.h>
    int f1()
    {
        printf("Jetha");
        return 1;
    }
    int f2()
    {
        printf("Babita");
        return 1;
    }
    int main()
    {
        int p = f1() + f2();
        printf("%d", p);
        return 0;
    }

NOTE: Among n++ and n = n+1, n++ will be executed faster in old compilers but in new compilers n = n+1 is converted as n++ while compiling, so both will take same execution time.
Among n++ and ++n, ++n is more efficient.

Relational Operators

  1. What is the output of the following C code for the following inputs:
    1. 1 3
    2. 0 3
    3. 3 0
    #include <stdio.h>
    int main()
    {
        int a, b;
        scanf("%d%d", &a, &b);
        if (a = b)
        {
            printf("Assignment operator ");
            printf("%d", a = b);
        }
        else
            printf("Else executed");
        return 0;
    }
  2. What is the output of the following C code?
  3. #include <stdio.h>
    int main()
    {
        int a = 10, b = 20, c = 30;
        if (c > b > a)
            printf("TRUE");
        else
            printf("FALSE");
        return 0;
    }

Logical or Boolean Operator

  1. What is the output of the following C code?
  2. #include <stdio.h>
    int main()
    {
        int a = 2;
        int p = (2 < 10) && (a = 4);
        printf("a=%d ", a);

        int b = 2;
        p = (2 > 10) && (b = 4);
        printf("b=%d ", b);

        int c = 2, d = 2;
        p = (2 > 10) && (c = 4) && (d = 4);
        printf("c=%d d=%d", a);
        return 0;
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>
    int main()
    {
        int a = 2;
        int p = (2 < 10) || (a = 4);
        printf("a=%d ", a);

        int b = 2;
        p = (2 > 10) || (b = 4);
        printf("b=%d ", b);

        int c = 2, d = 2;
        p = (2 > 10) || (c = 4) || (d = 4);
        printf("c=%d d=%d", a);
        return 0;
    }

Comma Operator

  1. What is the output of the following C code?
  2. #include <stdio.h>
    int main()
    {
        int a, b, c, res;
        res = (a = 8, b = 7, c = 9, a + b + c);
        printf("Output:%d", res);
        return 0;
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>
    int main()
    {
        int a = 8, b = 7, temp, x;
        printf("a=%d, b=%d ", a, b);
        x = temp = a, a = b, b = temp;
        printf("x=%d ", x);
        printf("a=%d, b=%d", a, b);
        return 0;
    }

Conditional or Ternary Operator

  1. Which combination of integer variables x, y and z makes the variable a get the value 4 in the following expression?
  2. a = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z);
    1. x = 3, y = 4, z = 2
    2. x = 6, y = 5, z = 3
    3. x = 6, y = 3, z = 5
    4. x = 5, y = 4, z = 5

sizeof() Operator

  • sizeof() is an operator and not a function.
  • sizeof() tells the number of bytes required to store an datatype/variable/constant etc.
  • sizeof() is a unary operator as it requires only single parameter as input.
  • sizeof() gives machine and compiler dependent results.
  • Return type of sizeof() is long unsigned int (%lu).
  • Parameter given to sizeof() operator is never evaluated.
  1. What is the output of the following C code?
  2. #include <stdio.h>
    int main()
    {
        int i = 12;
        int j = sizeof(++i);
        printf("%d %d", i, j);
        return 0;
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>
    int main()
    {
        printf("%d", sizeof(printf("Accenture")));
        return 0;
    }

Bitwise Operators

  1. What is the output of the following C code?
  2. #include <stdio.h>
    int main()
    {
        int a = 20;
        int b = 10;
        int c = a & b;
        int d = a | b;
        int e = a ^ b;
        printf("c=%d d=%d e=%d", c, d, e);
    }

Shift Operators

  1. What is the output of the following C code?
  2. #include <stdio.h>
    int main()
    {
        int a = 8 >> 1;
        int b = 8 >> 3;
        int c = 3 << 1;
        int d = 3 << 5;
        int e = 11 >> 2;
        int f = 1 << 5 << 4;
        printf("a=%d b=%d c=%d d=%d e=%d f=%d", a, b, c, d, e, f);
    }

Precedence and Associativity

  1. What is the output of the following C code?
  2. #include <stdio.h>
    int main()
    {
        int a, b, c, d, e, f, g, h, k;
        a = 8, b = 4, c = 2, d = 1, e = 5, f = 20;
        printf("%d\n", a + b - (c + d) * 3 % e + f / 9);

        a = 17, b = 5, c = 6, d = 3, e = 5;
        printf("%d\n", a % 6 - b / 2 + (c * d - 5) / e);

        a = 4, b = 5, c = 6, d = 3, e = 5, f = 10;
        printf("%d\n", a * b - c / d < e + f); // 0

        a = 8, b = 5, c = 8, d = 3, e = 65, f = 10, g = 2, h = 5, k = 2;
        printf("%d\n", a - b + c / d == e / f - g + h % k);

        a = 8, b = 3, c = 2, d = 3, e = 2, f = 11;
        printf("%d", a - b || (a - b * c) + d && e - f % 3);
    }
Prev Next

Comments