LearnFrenzy
Sign in Take a free test

C Programming · Topic 20 of 23

Operators and Expressions

  • 4 exercises
  • 40 questions
  • ~60 min
  • Answers explained

Introduction & Syntax

Operators and Expressions · Study material

Operators in C combine values into expressions: a + b, i++, x > 5 && y < 10, n += 3. Every C program is built from them, and placement tests love the details – integer division, the sign of %, pre- vs post-increment, precedence, the comma operator and expressions such as i = i++ that have no defined result at all. This page explains each group of operators and the rules that decide how an expression is evaluated, then gives you 40 practice questions checked on a real compiler.

On this page

  • Arithmetic operators, integer division and %
  • Increment and decrement: prefix vs postfix
  • Relational, logical, assignment, comma, conditional and sizeof
  • Precedence, associativity – and undefined unsequenced expressions

Arithmetic operators

+ - * / % work as expected, with two C rules to remember: dividing two integers truncates toward zero (17 / 5 is 3, -7 / 2 is −3), and a % b has the sign of a. % works only on integers; use fmod() for doubles.

#include <stdio.h>

int main(void) {
    int a = 17, b = 5;
    printf("%d %d %d %d\n", a + b, a - b, a * b, a / b);        /* int / int truncates */
    printf("%d %d %.2f\n", a % b, -a % b, (double) a / b);      /* % keeps the sign of a */
    return 0;
}

Output

22 12 85 3
2 -2 3.40

Increment and decrement

++ and -- add or subtract 1. The prefix form (++i) changes the variable and gives the new value; the postfix form (i++) gives the old value and changes the variable afterwards.

#include <stdio.h>

int main(void) {
    int i = 5;
    int x = i++;              /* use the old value (5), then increment: i = 6 */
    int y = ++i;              /* increment first (i = 7), then use it */
    printf("x=%d y=%d i=%d\n", x, y, i);
    return 0;
}

Output

x=5 y=7 i=7

Precedence and associativity

Precedence decides which operator binds first (* before +); associativity decides the order among operators of the same level – left to right for most binary operators, right to left for assignment, unary operators and ?:.

#include <stdio.h>

int main(void) {
    int r1 = 2 + 3 * 4;           /* * before +             */
    int r2 = (2 + 3) * 4;         /* brackets first         */
    int r3 = 20 / 4 / 2;          /* left to right: (20/4)/2 */
    int a, b, c;
    a = b = c = 7;                /* right to left: a = (b = (c = 7)) */
    printf("%d %d %d %d\n", r1, r2, r3, a + b + c);
    return 0;
}

Output

14 20 2 21
Level (high → low)OperatorsAssociativity
Postfix() [] -> . x++ x--left to right
Unary++x --x + - ! ~ * & (type) sizeofright to left
Multiplicative* / %left to right
Additive+ -left to right
Shift<< >>left to right
Relational< <= > >=left to right
Equality== !=left to right
Bitwise& then ^ then |left to right
Logical&& then ||left to right
Conditional?:right to left
Assignment= += -= *= /= %= …right to left
Comma,left to right

Relational and logical operators

Comparisons (< > == != …) and logical operators (&& || !) produce the int 1 or 0. && and || evaluate left to right and stop as soon as the result is known (short-circuit).

#include <stdio.h>

int main(void) {
    int x = 7;
    /* comparisons and logical operators give the int 1 (true) or 0 (false) */
    printf("%d %d %d %d\n", x > 5, x == 5, !x, (x > 5) + (x < 10));
    return 0;
}

Output

1 0 0 2

Assignment, compound assignment and the comma operator

n += 5 means n = n + 5 (with n evaluated once). The comma operator evaluates its left operand, discards it, and yields its right operand – mostly seen in for loops. Not every comma is this operator: the commas between function arguments and in declarations are just separators.

#include <stdio.h>

int main(void) {
    int n = 10;
    n += 5;                   /* 15 */
    n *= 2;                   /* 30 */
    n -= 4;                   /* 26 */
    n /= 3;                   /* 8  */
    n %= 5;                   /* 3  */
    int k = (n++, n * 10);    /* comma: do n++, then the value is n * 10 */
    printf("%d %d\n", n, k);
    return 0;
}

Output

4 40

Unsequenced side effects: undefined behaviour

If one expression modifies a variable twice, or modifies it and reads it elsewhere without a sequence point in between, the behaviour is undefined: i = i++, a[i] = i++, printf("%d %d", i++, i++). Different compilers print different results, and the optimiser may do anything. Split such expressions into separate statements.

i = i++;                    /* undefined: i modified twice                */
a[i] = i++;                 /* undefined: i read and modified, unsequenced */
printf("%d %d\n", i++, i++);/* undefined: two unsequenced modifications   */
x = i++ + i++;              /* undefined                                    */

i++;                        /* fine: one change per statement */
a[i] = i;

Key points to remember

  • int / int truncates toward zero; % takes the sign of the left operand.
  • i++ yields the old value, ++i the new one.
  • When in doubt, add brackets – they cost nothing and remove all doubt.
  • Relational and logical operators return the int 0 or 1.
  • Never modify a variable twice in one expression.

Frequently asked questions

What is the difference between ++i and i++?

Both add 1 to i. As an expression, ++i has the new value and i++ the old value. On its own line (i++;) there is no difference.

Why does 5 / 2 give 2 in C?

Both operands are integers, so the division is integer division and the fraction is discarded. Write 5 / 2.0 or (double) 5 / 2 for 2.5.

What is a sequence point?

A point where all earlier side effects are complete – for example at the end of a full expression, after the left operand of &&, ||, ?: and the comma operator, and before a function is called.

Where can I learn more?

Read Operators in C and Type Conversion in C, then practise with the exercises below.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C Programming

C programming is the language most computer science courses start with and the foundation of operating systems, embedded devices and compilers. This section helps you learn C properly – data types, operators, control statements, functions, arrays, strings, pointers, structures, memory allocation and files – and then prove it with hundreds of practice questions explained line by line.

Why it matters

  • C teaches how a computer really works: memory, addresses and how data is stored
  • Pointers and memory questions are a favourite in written tests and technical interviews
  • Once you know C, learning C++, Java and Python becomes much easier
  • Embedded systems, operating systems and many companies' coding rounds still use C

Who it is for

  • School students (Class 11–12 computer science) building a strong programming base
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, aptitude-cum-coding tests and technical interviews
  • Anyone preparing for GATE CS or government IT exams

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro tests
  • Technical interviews
  • GATE CS
  • University exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links