Co to jest specyfikacja formatu printf dla bool?
Od ANSI C99 istnieje _Bool
lub bool
poprzez stdbool.h
. Ale czy istnieje również specyfikacja formatu printf
dla bool?
Mam na myśli coś takiego jak w tym pseudo kodzie:
bool x = true;
printf("%B\n", x);
Który wydrukuje:
true
8 answers
Nie ma. ale ponieważ każdy typ całki krótszy od {[4] } jest promowany do int
, gdy przekazywany jest do printf()
s argumentów wariacyjnych, możesz użyć %d
:
bool x = true;
printf("%d\n", x); // prints 1
Ale dlaczego nie
printf(x ? "true" : "false");
Lub lepiej
printf("%s", x ? "true" : "false");
Or even better
fputs(x ? "true" : "false", stdout);
Zamiast tego?Nie ma specyfikacji formatu dla bool. Można go wydrukować przy użyciu niektórych istniejących specyfikatorów do drukowania typów integralnych lub zrobić coś bardziej fantazyjnego:
printf("%s", x?"true":"false");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-25 20:59:14
ANSI C99 / C11 nie zawierają dodatkowego specyfikatora konwersji printf dla bool
.
Ale biblioteka GNU C zapewnia API do dodawania niestandardowych specyfikacji .
Przykład:
#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;
}
Ponieważ jest to rozszerzenie glibc, GCC ostrzega o tym niestandardowym specyfikatorze:
$ gcc -Wall -g main.c -o main main.c: In function ‘main’: main.c:34:3: warning: unknown conversion type character ‘B’ in format [-Wformat=] r = printf("The result is: %B\n", b); ^ main.c:34:3: warning: too many arguments for format [-Wformat-extra-args]
Wyjście:
$ ./main The result is: false (written 21 characters) $ ./main 1 The result is: true (written 20 characters)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-01 12:30:30
W tradycji itoa()
:
#define btoa(x) ((x)?"true":"false")
bool x = true;
printf("%s\n", btoa(x));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-25 21:00:48
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:18:16
Wolę odpowiedź z najlepszy sposób, aby wydrukować wynik bool jako 'false' lub 'true' w c?, tak jak
printf("%s\n", "false\0true"+6*x);
- x = = 0, "false\0true" + 0 "oznacza " false";
- x = = 1, "false\0true" + 6 "oznacza " prawda";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:54:50
Jeśli lubisz C++ lepiej niż C, możesz spróbować tego:
#include <ios>
#include <iostream>
bool b = IsSomethingTrue();
std::cout << std::boolalpha << b;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-19 19:48:49
Aby po prostu wydrukować 1 lub 0 Na podstawie wartości logicznej, której właśnie użyłem:
printf("%d\n", !!(42));
Szczególnie przydatne z flagami:
#define MY_FLAG (1 << 4)
int flags = MY_FLAG;
printf("%d\n", !!(flags & MY_FLAG));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-09 13:26:18