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

285
Views
Typecast to unsigned char and multiply the variables
printf("%d\n", (unsigned char)255 * (unsigned char)255);

The output of the code above is 65025 but not 1 (255 * 255 (mod 256)). Why?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The multiplication operator * applies the usual arithmetic conversions to its operands. These conversions convert the unsigned char operands to int. See pmg's answer for the standard reference.

Apparently on your system type int is wide enough to hold the result value 65025. On a system with 16-bit int, where INT_MAX == 32767, the multiplication would result in an overflow, causing undefined behavior (typically the high-order bits of the result would be discarded). Most modern systems have 32-bit int.

There is no multiplication operator for integer types narrower than int or unsigned int.

(Strictly speaking, it's possible that the operands could be converted to unsigned int rather than to int. That would happen only if unsigned int can represent the entire range of unsigned char, but int cannot -- and that cannot happen on a system with 8-bit char.)

over 4 years ago · Santiago Trujillo Report

0

The operands to * undergo the "usual arithmetic conversions" (C11 Standard p6.3.1.8).

Also see p6.5.5.2: Multiplicative operators

Semantics
The usual arithmetic conversions are performed on the operands.

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!