Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

291
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda