Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
Weird output of simple expression in C, why?

I am using TurboC++. I write the following expression which is not resulting in proper evaluation, am I missing some concept behind it?

int c=300*300/300;
printf("%d",c);

The output is

81

Why?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

300*300 is 90000.

Assuming int is 16bit, you have overflowed.

The overflow wraps around, giving you: 24464.

24465/300 = 81.55

Do not rely on this. It is undefined behavior.

over 4 years ago · Santiago Trujillo Report

0

The evaluation of 300 * 300 / 300 happens from left to right.

300 * 300 overflows a 16 bit signed integral type (an int in Turbo C++ is 16 bit). As the computation will take place in signed arithmetic, the result is undefined.

What's happening is 300 * 300 is wrapping round to give you 24464. (24464 + 32768 + 32768 = 90000).

24464 / 300 is 81 in integer division.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!