Sinds ANSI C99 Er is _Bool
of bool
VIA stdbool.h
. Maar is er ook een printf
Formatteerspecificator voor BOOL?
Ik bedoel iets als in die pseudo-code:
bool x = true;
printf("%B\n", x);
die afdrukken:
true
Antwoord 1, Autoriteit 100%
Er is geen formaatspecificator voor bool
typen. Sinds elk integraal type korter dan int
wordt gepromoot naar int
wanneer u wordt doorgegeven aan printf()
‘S VARIADIC-argumenten, kunt u %d
:
bool x = true;
printf("%d\n", x); // prints 1
Maar waarom niet:
printf(x ? "true" : "false");
of, beter:
printf("%s", x ? "true" : "false");
of, nog beter:
fputs(x ? "true" : "false", stdout);
in plaats daarvan?
Antwoord 2, Autoriteit 6%
Er is geen formaatspecificator voor bool. U kunt het afdrukken met een aantal van de bestaande specifieke specificaties voor het afdrukken van integrale typen of iets meer fantasandert:
printf("%s", x ? "true" : "false");
Antwoord 3, Autoriteit 4%
ANSI C99 / C11 Neem geen extra printf-conversiespecificator op voor bool
.
Maar de GNU C-bibliotheek biedt een API voor het toevoegen van aangepaste specificaties.
Een voorbeeld:
#include <stdio.h>
#include <printf.h>
#include <stdbool.h>
static int bool_arginfo(const struct printf_info *info, size_t n,
int *argtypes, int *size)
{
if (n) {
argtypes[0] = PA_INT;
*size = sizeof(bool);
}
return 1;
}
static int bool_printf(FILE *stream, const struct printf_info *info,
const void *const *args)
{
bool b = *(const bool*)(args[0]);
int r = fputs(b ? "true" : "false", stream);
return r == EOF ? -1 : (b ? 4 : 5);
}
static int setup_bool_specifier()
{
int r = register_printf_specifier('B', bool_printf, bool_arginfo);
return r;
}
int main(int argc, char **argv)
{
int r = setup_bool_specifier();
if (r) return 1;
bool b = argc > 1;
r = printf("The result is: %B\n", b);
printf("(written %d characters)\n", r);
return 0;
}
Omdat het een glibc-extensie is, waarschuwt de GCC voor die aangepaste specificatie:
$ gcc -Wall -g main.c -o main main.c: In functie ‘main’: main.c:34:3: waarschuwing: onbekend conversietype teken 'B' in formaat [-Wformat=] r = printf("Het resultaat is: %B\n", b); ^ main.c:34:3: waarschuwing: te veel argumenten voor formaat [-Wformat-extra-args]
Uitvoer:
$ ./main Het resultaat is: false (geschreven 21 tekens) $ ./hoofd 1 Het resultaat is: waar (geschreven 20 karakters)
Antwoord 4
In de traditie van itoa()
:
#define btoa(x) ((x)?"true":"false")
bool x = true;
printf("%s\n", btoa(x));
Antwoord 5
Dat kan niet, maar u kunt wel 0 of 1 afdrukken
_Bool b = 1;
printf("%d\n", b);
Antwoord 6
Om gewoon 1 of 0 af te drukken op basis van de booleaanse waarde die ik zojuist heb gebruikt:
printf("%d\n", !!(42));
Vooral handig met vlaggen:
#define MY_FLAG (1 << 4)
int flags = MY_FLAG;
printf("%d\n", !!(flags & MY_FLAG));
Antwoord 7
Als je C++ beter vindt dan C, kun je dit proberen:
#include <ios>
#include <iostream>
bool b = IsSomethingTrue();
std::cout << std::boolalpha << b;
Antwoord 8
Ik geef de voorkeur aan een antwoord van De beste manier om het resultaat van een bool af te drukken als ‘false’ of ‘true’ in c?, net als
printf("%s\n", "false\0true"+6*x);
- x == 0, “false\0true”+ 0” betekent “false”;
- x == 1, “false\0true”+ 6” betekent “true”;