jimboweb jimboweb - 1 year ago 90
C Question

can't read first character of array in c

I'm trying to read the characters of a character array by number in c. I've stripped the program down to isolate the problem. I know this is about my misunderstanding of how arrays and memory works and I am ready to be called clueless but I would like to know what I am misunderstanding here. Here is what I have:

#include <stdio.h>

int main(int argc, char **argv) {
char buffer[] = "stuff";
printf("buffer is %s\n", buffer);
printf("first character of buffer is %s", (char)buffer[0]);

return 0;

Answer Source

You have to write the correct format specifier. Now you used %s ...what happens? It looks for an string which is null terminated. But it doesn't find one. So it simply cant put anything in the output . That's it. Use %c instead.

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