I have a string I composed using memcpy() that (when expanded) looks like this:
char* str = "AAAA\x00\x00\x00...\x11\x11\x11\x11\x00\x00...";
I would like to print every character in the string, and if the character is null, print out "(null)"
as a substitute for '\0'.
If I use a function like puts() or printf() it will just end at the first null and print out
AAAA
So how can I get it to print out the actual word "(null)" without it interpreting it as the end of the string?
You have to do that mapping yourself. If you want to, that is. In C, strings are null-terminated. So, if you use a formatted output function such as
printf
orputs
and ask it to print a string (via the format specifier%s
) it'd stop printingstr
as soon as it hits the first null. There is nonull
word in C. If you know exactly how many characters you have instr
you might as well loop over them and print the characters out individually, substituting the0
by your chosen mnemonic.The draft says 7.21.6.1/8:
However, the following:
produces:
on both gcc 4.6 and clang 3.2.
However, on digging deeper:
does indeed produce the desired output:
on both gcc and clang.
Note that the standard does not mandate this:
Relying on this behavior may lead to surprises!