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

310
Visualizações
(Where) does the C standard define the result of adding/subtracting two booleans?

The C11 standard defines the _Bool type (6.2.5.2) as a standard unsigned integer type (6.2.5.6) and as I read the standard, _Bool is then also an arithmetic type (6.2.5.18 via 6.2.5.7 and 6.2.5.17).

Furthermore, it is specified that for + and - "both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type" (6.5.6.2).

However, about the result I can only see "The result of the binary + operator is the sum of the operands" (6.5.6.5) and "The result of the binary - operator is the difference resulting from the subtraction of the second operand from the first" (6.5.6.6). For two booleans, "sum" may be interpreted as logical OR, but I do not think "subtraction" has a well-defined meaning.

So the question is: is the result of a+b and a-b (where a and b have type _Bool) undefined behavior in C11 or does the standard clearly define the result of these operations (if so, where?)?

Note: maybe the standard just sees _Bool as an integer with very small range. In that case, I would expect true+true to be 0 (1 + 1 modulo 2). However, GCC says 1.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

From the C Standard (6.5.6 Additive operators)

4 If both operands have arithmetic type, the usual arithmetic conversions are performed on them

And (6.3.1.8 Usual arithmetic conversions)

1 Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way ...

Otherwise, the integer promotions are performed on both operands.

And (6.3.1.1 Boolean, characters, and integers)

  1. ...If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. 58) All other types are unchanged by the integer promotions.

So the result of an additive operation has the type int when the both operands have the type _Bool that are integer promoted to the type int before performing the operation.

Pay attention to that in C there is no boolean type as in C++. The boolean type _Bool is a standard unsigned integer type in C that can store either 1 or 0.

So if you will write for example

_Bool a = 1;
_Bool b = 1;
_Bool c = a + b;

then the value of the variable c will be 1 because any non-zero value (and the result of the operation is the value 2 of the type int) is converted to 1.

From the C Standard

6.3.1.2 Boolean type

1 When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

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