Tony Tannous Tony Tannous - 1 year ago 56
C Question

Printing the value of a 0-initialized array element prints nothing, why?

I have to initialize a char array to 0's. I did it like

char array[256] = {0};

I wanted to check if it worked so I tried testing it

#include <stdio.h>

int main()
char s[256] = {0};
printf("%c\n", s[10]);
return 0;

After I compile and run it, the command line output shows nothing.

What am I missing ? Perhaps I initialized the array in a wrong manner ?

Answer Source

TL;DR -- %c is the character representation. Use %d to see the decimal 0 value.

Related , from C11, chapter ยง7.21.6.1, (emphasis mine)

c          If no l length modifier is present, the int argument is converted to an unsigned char, and the resulting character is written.

FYI, see the list of printable values.

That said, for a hosted environment, int main() should be int main(void), at least to conform to the standard.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download