top of page

Why does 5/2 give 2 in C? Integer division explained

Computer Science · Class 12 · College | 20 Oct 2026 | 2 min read | By Ayan Mandal

In C, dividing two integers throws away the decimal part. Learn why it happens, how to get 2.5 instead, and the exam mistake to avoid.

The surprise

Many students write printf("%d", 5/2); and expect 2.5. The program prints 2. Nothing is broken. C is doing exactly what the rules say.


Key idea

When both numbers are integers, C does integer division. The decimal part is thrown away. It is not rounded.


Why it happens

C looks at the types of the two operands before it divides:


  • int / int gives an int, so 5/2 is 2
  • float / int or int / float gives a float, so 5.0/2 is 2.5


In maths terms, C gives you the quotient only:


5 = 2 × 2 + 1


Here the quotient is 2 and the remainder is 1. The remainder is what the % operator gives you.


How to get 2.5

Make at least one side a real number:


#include <stdio.h>

int main() {
    int a = 5, b = 2;
    printf("%d\n", a / b);          /* 2   */
    printf("%d\n", a % b);          /* 1   */
    printf("%.1f\n", (float)a / b); /* 2.5 */
    return 0;
}


The (float) before a is called type casting. It turns a into a float before the division happens.


Common mistake

Writing (float)(a / b) does not work. The division is done first, so you get 2 and then it becomes 2.0. Cast one operand, not the answer.


Quick practice

  1. What does 7/2 print? What does -7/2 print?
  2. What does 7 % 2 print?
  3. Fix this so it prints 3.5: printf("%f", 7/2);


Exam tip

Board exams love this trap. If a program uses int variables for an average, say clearly in your answer that the decimal part is lost.


Remember: the types decide the answer, not the numbers.

[picture caption 1]

[post text 2]

[picture caption 2]

[post text 3]

[picture caption 3]

[post text 4]

[picture caption 4]

[post text 5]

[picture caption 5]

[post text 6]

[picture caption 6]

[post text 7]

PDF files for this post

Open in Drive
Open in Drive
Preview here
Preview here

WRITTEN BY

Ayan Mandal

B.Sc. Honours in Computer Science · 5+ years of teaching

bottom of page