Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

286
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda