-1

According to this question, sizeof(true) or sizeof(false) is 4 bytes because true and false are macros and defined in #include <stdbool.h> header file.

But, here interesting output of program.

#include <stdio.h>
#include <stdbool.h>

int main() 
{
    bool a = true;
    printf("%zu\n", sizeof(a ? true : false, a));
                             /* ^^ -> expresion2 */
    return 0;
}

Output(Live demo):

1

C11 6.5.15 Conditional operator(P4) :

The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below.110)

Here, a is a bool type and assigned value true, then inside the sizeof operator, the conditional operator executed expression2 because a is a true.

So, comma operator part(expression3) not evaluated according to the standard and expression2 is a macro. So, according to that question, the output of macro is 4 bytes but here, the output of program is 1 byte.

So here, Why sizeof(a ? true : false, a) printed 1 byte only?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
msc
  • 33,420
  • 29
  • 119
  • 214

4 Answers4

10

a ? true : false, a is two expressions, separated by the comma operator. It discards the result of the ternary operator and evaluates simply to a.

Since a is a bool, which in stdbool.h is a macro for _Bool, you print the size of the _Bool data type. It's more than likely that _Bool is just one byte, since all it needs to hold is the values 1 and 0.

To contrast it with the linked question, there you printed true and false, which stdbool.h defines as preprocessor macros for 1 and 0. The two are int constants, and therefore their size is the size of the int data type.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
7

For starters consider the following simple demonstrative program

#include <stdio.h>
#include <stdbool.h>

int main(void) 
{
    printf( "sizeof( _Bool ) = %zu\n", sizeof( _Bool ) );
    printf( "sizeof( bool )  = %zu\n", sizeof( bool ) );

    return 0;
}

Its output is

sizeof( _Bool ) = 1
sizeof( bool ) = 1

According to the C Standard (7.18 Boolean type and values )

2 The macro

bool

expands to _Bool.

Now let's consider the expression used in the sizeof operator in this call

printf("%zu\n", sizeof(a ? true : false, a));

According to the C Standard (6.5.15 Conditional operator)

Syntax

1 conditional-expression:
    logical-OR-expression
    logical-OR-expression ? expression : conditional-expression

That is the conditional operator has higher precedence than the comma operator.

So this expression

a ? true : false, a

is an expression with the comma operator and can be equivalently rewritten like

( a ? true : false ) , ( a )

The result of the expression is the second operand of the comma operator that is it is the expression ( a ).

As it was shown above this expression has the type _Bool because the variable a is declared like

bool a = true;

where the macro bool expands to _Bool. So sizeof( _Bool ) is equal to 1.

If you will rewrite the conditional operator the following way as it is shown in this call

printf( "%zu\n", sizeof( a ? true : a ) );

then the output will be equal to the value returned by sizeof( int ) (usually equal to 4) because the type _Bool has less rank than the type int (the type of the constant integer literal of the expanded macro true) and as result the expression a will be implicitfly converted to the type int due to the integer promotions.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Does the sizeof operator evaluate the expression inside the parentheses? – surendra nath Nov 02 '17 at 09:27
  • @surendranath No, the expression itself is not evaluated. For example, int a = 1; printf( "%zu\n", sizeof( ++a ) ); printf( "%d\n", a ); the output for a will be 1. – Vlad from Moscow Nov 02 '17 at 09:29
  • Then, how is the sizeof operator printing 1? Is it because of the sizeof _Bool/bool or anything else? – surendra nath Nov 02 '17 at 09:31
  • @surendranath Because the expression has the type _Bool that has the size 1 that was shown in the demonstrative program. – Vlad from Moscow Nov 02 '17 at 09:35
  • @surendranath The result of the comma operator is the second operand. So the sizeof operator determinates the type of the second operand. If there is used the conditional operator then the sizeof operator determinates the common type of the second and third operands. – Vlad from Moscow Nov 02 '17 at 09:37
  • 1
    @surendranath - the `sizeof` operator does not evaluate the expression (and the `()` are part of the expression). However, it does determine the type of the result of that expression, even though it doesn't actually evaluate it. Once it knows the type of the result, it can determine the size of that type. – Peter Nov 02 '17 at 09:41
1

You are getting 1 because of comma operator used in the expression:

(a ? true : false, a));

Comma operator:

In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).

So, in expression (a ? true : false, a)); because of the comma operator, the result of ternary operator is getting discarded and then it evaluates the second operand and return a. Since a is of type bool so, your program is giving output as sizeof(bool) which is 1.

Try putting brackets around false,a, like this:

printf("%zu\n", sizeof(a ? true : (false, a)));

When you put the expression false, a in brackets (), expression (false, a) will get evaluated first and the comma operator returns a but the ternary operator will evaluate the expression and return true, which is a macro expands to the integer constant 1, and the output of the program will be sizeof(1) i.e. 4.

H.S.
  • 11,654
  • 2
  • 15
  • 32
0

first statement in Wiki

"In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result"

so essentially

printf("%zu\n", sizeof(a ? true : false, a));

evaluates to

printf("%zu\n", sizeof(a));

a is of type bool and rest is what you get as output.

asio_guy
  • 3,667
  • 2
  • 19
  • 35
  • The quote (from the Wiki) is crippled, incomplete in a way that is does not make sense in the context of the question. – alk Nov 04 '17 at 10:25