Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 1

Results of all the operators

CPractice sessionsJNTUH R22 · CS108ES

Aim

To write a C program that reads two integers and prints the result of every operator in C: arithmetic, relational, logical, bitwise, assignment, increment and decrement, conditional and sizeof.

Algorithm

  1. Read two integers a and b.
  2. If b is 0, print a message and stop, because / and % cannot divide by zero.
  3. Print the results of the arithmetic operators + - * / %.
  4. Print the results of the relational operators < > <= >= == != (1 means true, 0 means false).
  5. Print the results of the logical operators && || !.
  6. Print the results of the bitwise operators & | ^ ~ << >>.
  7. Copy a into x and apply the assignment operators += -= *= /= %= one after another, printing x each time.
  8. Show pre-increment, post-increment, pre-decrement and post-decrement by printing both the value used and the new value.
  9. Print the larger number using the conditional operator ?: and the size of an int using sizeof.

Program

C
#include <stdio.h>

int main(void) {
    int a, b;
    printf("Enter two integers a and b: ");
    if (scanf("%d %d", &a, &b) != 2) {
        printf("Invalid input\n");
        return 1;
    }
    if (b == 0) {
        printf("b cannot be 0, because / and %% divide by b\n");
        return 1;
    }

    printf("\nArithmetic operators\n");
    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d\n", a / b);
    printf("a %% b = %d\n", a % b);

    printf("\nRelational operators (1 = true, 0 = false)\n");
    printf("a < b  is %d\n", a < b);
    printf("a > b  is %d\n", a > b);
    printf("a <= b is %d\n", a <= b);
    printf("a >= b is %d\n", a >= b);
    printf("a == b is %d\n", a == b);
    printf("a != b is %d\n", a != b);

    printf("\nLogical operators\n");
    printf("a && b is %d\n", a && b);
    printf("a || b is %d\n", a || b);
    printf("!a     is %d\n", !a);

    printf("\nBitwise operators\n");
    printf("a & b  = %d\n", a & b);
    printf("a | b  = %d\n", a | b);
    printf("a ^ b  = %d\n", a ^ b);
    printf("~a     = %d\n", ~a);
    printf("a << 1 = %d\n", a << 1);
    printf("a >> 1 = %d\n", a >> 1);

    printf("\nAssignment operators (x starts as a)\n");
    int x = a;
    x += b;
    printf("x += b gives x = %d\n", x);
    x -= b;
    printf("x -= b gives x = %d\n", x);
    x *= b;
    printf("x *= b gives x = %d\n", x);
    x /= b;
    printf("x /= b gives x = %d\n", x);
    x %= b;
    printf("x %%= b gives x = %d\n", x);

    printf("\nIncrement and decrement operators (x starts as a)\n");
    int y;
    x = a;
    y = ++x;
    printf("y = ++x: y = %d, x = %d\n", y, x);
    x = a;
    y = x++;
    printf("y = x++: y = %d, x = %d\n", y, x);
    x = a;
    y = --x;
    printf("y = --x: y = %d, x = %d\n", y, x);
    x = a;
    y = x--;
    printf("y = x--: y = %d, x = %d\n", y, x);

    printf("\nOther operators\n");
    int larger = (a > b) ? a : b;
    printf("(a > b) ? a : b = %d\n", larger);
    printf("sizeof(int) = %zu bytes\n", sizeof(int));
    return 0;
}

Output

Output
Enter two integers a and b: 12 5

Arithmetic operators
a + b = 17
a - b = 7
a * b = 60
a / b = 2
a % b = 2

Relational operators (1 = true, 0 = false)
a < b  is 0
a > b  is 1
a <= b is 0
a >= b is 1
a == b is 0
a != b is 1

Logical operators
a && b is 1
a || b is 1
!a     is 0

Bitwise operators
a & b  = 4
a | b  = 13
a ^ b  = 9
~a     = -13
a << 1 = 24
a >> 1 = 6

Assignment operators (x starts as a)
x += b gives x = 17
x -= b gives x = 12
x *= b gives x = 60
x /= b gives x = 12
x %= b gives x = 2

Increment and decrement operators (x starts as a)
y = ++x: y = 13, x = 13
y = x++: y = 12, x = 13
y = --x: y = 11, x = 11
y = x--: y = 12, x = 11

Other operators
(a > b) ? a : b = 12
sizeof(int) = 4 bytes

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

With a = 12 (binary 1100) and b = 5 (binary 0101): a & b keeps only the bits set in both (0100 = 4), a | b keeps bits set in either (1101 = 13), and a ^ b keeps bits set in exactly one (1001 = 9). ~a flips every bit, which for a signed int gives −(a + 1) = −13.

a << 1 doubles a and a >> 1 halves it (dropping the remainder). Use non-negative numbers for the shifts: shifting a negative number is not well defined in C.

To print a % sign with printf, write %%. sizeof gives a value of type size_t, which is printed with %zu.

Common mistakes

  • Thinking x++ and ++x are always the same. On their own line they are, but in y = x++ y gets the old value, while in y = ++x y gets the new value.

  • Mixing up & with && and | with ||. & and | work on each bit, while && and || treat the whole number as true (non-zero) or false (zero) and give 1 or 0.

  • Writing a % b inside printf as "a % b". A single % starts a format specifier, so write %% to print the sign.

  • Not checking b before a / b and a % b. Integer division by zero crashes the program.

Viva questions

Try to answer each one aloud before you open it.

  • What is the difference between pre-increment and post-increment?

    Pre-increment (++x) adds 1 first and then gives the new value. Post-increment (x++) gives the old value and then adds 1.

  • What does the % operator do, and can it be used with float values?

    % gives the remainder of an integer division, for example 12 % 5 = 2. It works only on integers; for floating-point numbers use fmod from math.h.

  • What is the difference between & and &&?

    & is bitwise AND: it compares the two numbers bit by bit. && is logical AND: it gives 1 if both operands are non-zero, otherwise 0.

  • Why is ~12 equal to -13?

    ~ flips every bit. Signed integers are stored in two's complement form, where flipping all the bits of n gives −(n + 1).

  • What is the conditional (ternary) operator?

    It is condition ? value1 : value2. If the condition is true the result is value1, otherwise value2. It is the only operator in C that takes three operands.

  • Is sizeof a function?

    No. sizeof is an operator that gives the size in bytes of a type or variable. It is worked out by the compiler.

Learn the concepts

All Programming for Problem Solving Lab experiments →