Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf conversion specifier for _Bool?

With printf(), I can use %hhu for unsigned char, %hi for a short int, %zu for a size_t, %tx for a ptrdiff_t, etc.

What conversion format specifier do I use for a _Bool? Does one exist in the standard?

Or do I have to cast it like this:

_Bool foo = 1;
printf("foo: %i\n", (int)foo);
like image 782
Richard Hansen Avatar asked Jun 28 '12 17:06

Richard Hansen


People also ask

What is %d %s %F in C?

%d is print as an int %s is print as a string %f is print as floating point.

How do you print a boolean value?

The println(boolean) method of PrintStream Class in Java is used to print the specified boolean value on the stream and then break the line. This boolean value is taken as a parameter. Parameters: This method accepts a mandatory parameter booleanValue which is the boolean value to be written on the stream.

What is %P in C printf?

In C we have seen different format specifiers. Here we will see another format specifier called %p. This is used to print the pointer type data.


1 Answers

There is no specific conversion length modifier for _Bool type.

_Bool is an unsigned integer type large enough to store the values 0 and 1. You can print a _Bool this way:

_Bool b = 1;
printf("%d\n", b);

Because of the integer promotions rules, _Bool is guaranteed to promote to int.

like image 79
ouah Avatar answered Oct 02 '22 11:10

ouah