What does an asterisk in a scanf format specifier mean? [duplicate]
*c means, that a char will be read but won’t be assigned, for example for the input “30a” it will assign 30 to word_count, but ‘a’ will be ignored.
*c means, that a char will be read but won’t be assigned, for example for the input “30a” it will assign 30 to word_count, but ‘a’ will be ignored.
You should use the style of inttypes.h but define the symbols yourself. For example: #define PRIu8 “hhu” #define PRId8 “hhd” #define PRIx8 “hhx” #define PRIu16 “hu” #define PRId16 “hd” #define PRIx16 “hx” #define PRIu32 “u” #define PRId32 “d” #define PRIx32 “x” #define PRIu64 “llu” // or possibly “lu” #define PRId64 “lld” // or possibly “ld” … Read more
%02x means print at least 2 digits, prepend it with 0‘s if there’s less. In your case it’s 7 digits, so you get no extra 0 in front. Also, %x is for int, but you have a long. Try %08lx instead.
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
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
Your array needs to be able to hold four chars, since it must also contain the 0-terminator. With that fixed, specifying a maximal length in the format, scanf(“%3s”, string); ensures that scanf reads no more than 3 characters.
It’s used to specify, in a dynamic way, what the width of the field is: The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. so “indent” specifies how much space to allocate for the string that follows it in the … Read more
One possible reason: for symmetry with the use of those modifiers in the formatted input functions? I know it wouldn’t be strictly necessary, but maybe there was value seen for that? Although they don’t mention the importance of symmetry for the “h” and “hh” modifiers in the C99 Rationale document, the committee does mention it … Read more
Yes: use the z length modifier: size_t size = sizeof(char); printf(“the size is %zu\n”, size); // decimal size_t (“u” for unsigned) printf(“the size is %zx\n”, size); // hex size_t The other length modifiers that are available are hh (for char), h (for short), l (for long), ll (for long long), j (for intmax_t), t (for … Read more
printf(“Hello World!”); is IMHO not vulnerable but consider this: const char *str; … printf(str); If str happens to point to a string containing %s format specifiers, your program will exhibit undefined behaviour (mostly a crash), whereas puts(str) will just display the string as is. Example: printf(“%s”); //undefined behaviour (mostly crash) puts(“%s”); // displays “%s\n”