Correct printf format specifier for size_t: %zu or %Iu?

MS Visual Studio didn’t support %zu printf specifier before VS2013. Starting from VS2013 (e.g. _MSC_VER >= 1800) %zu is available. As an alternative, for previous versions of Visual Studio if you are printing small values (like number of elements from std containers) you can simply cast to an int and use %d: printf(“count: %d\n”, (int)str.size()); … Read more

What’s the meaning of the %m formatting specifier?

m conversion specifier is not C but is a GNU extension to printf: From GNU documentation: http://www.gnu.org/software/libc/manual/html_node/Other-Output-Conversions.html The ‘%m’ conversion prints the string corresponding to the error code in errno. See Error Messages. Thus: fprintf (stderr, “can’t open `%s’: %m\n”, filename); is equivalent to: fprintf (stderr, “can’t open `%s’: %s\n”, filename, strerror (errno)); The ‘%m’ … Read more

How to use C++ std::ostream with printf-like formatting?

In C++20 you can use std::format for safe printf-like formatting: std::cout << std::format(“The answer is {}.\n”, 42); In addition to that the {fmt} library, std::format is based on, provides the print function that combines formatting and output: fmt::print(“The answer is {}.\n”, 42); Disclaimer: I’m the author of {fmt} and C++20 std::format.

Can I display the value of an enum with printf()?

As a string, no. As an integer, %d. Unless you count: static char* enumStrings[] = { /* filler 0’s to get to the first value, */ “enum0”, “enum1”, /* filler for hole in the middle: ,0 */ “enum2”, “enum3”, …. }; … printf(“The value is %s\n”, enumStrings[thevalue]); This won’t work for something like an enum … Read more

Does C have a string type? [closed]

C does not and never has had a native string type. By convention, the language uses arrays of char terminated with a null char, i.e., with ‘\0′. Functions and macros in the language’s standard libraries provide support for the null-terminated character arrays, e.g., strlen iterates over an array of char until it encounters a ‘\0’ … Read more