问题描述:

(Preface: I am not a programmer, at all, but I'm studying it so I can build some understanding of how it works, so please bear with my ignorance!)

I've been working away on the C language tutorials over at c.learncodethehardway.org. Made it about 8 exercises in, when I got stuck on the 9th. Here's the link:

http://c.learncodethehardway.org/book/ex9.html

I pretty much have a grasp on all of this, except when it gets to the author's breakdown in the "What You Should See" section. Here's a copy/paste of the code in question:

#include <stdio.h>

int main(int argc, char *argv[])

{

int numbers[4] = {0};

char name[4] = {'a'};

// first, print them out raw

printf("numbers: %d %d %d %d\n",

numbers[0], numbers[1],

numbers[2], numbers[3]);

printf("name each: %c %c %c %c\n",

name[0], name[1],

name[2], name[3]);

printf("name: %s\n", name);

// setup the numbers

numbers[0] = 1;

numbers[1] = 2;

numbers[2] = 3;

numbers[3] = 4;

// setup the name

name[0] = 'Z';

name[1] = 'e';

name[2] = 'd';

name[3] = '\0';

// then print them out initialized

printf("numbers: %d %d %d %d\n",

numbers[0], numbers[1],

numbers[2], numbers[3]);

printf("name each: %c %c %c %c\n",

name[0], name[1],

name[2], name[3]);

// print the name like a string

printf("name: %s\n", name);

// another way to use name

char *another = "Zed";

printf("another: %s\n", another);

printf("another each: %c %c %c %c\n",

another[0], another[1],

another[2], another[3]);

return 0;

}

What the program will print on screen looks like this:

numbers: 0 0 0 0

name each: a

name: a

numbers: 1 2 3 4

name each: Z e d

name: Zed

another: Zed

another each: Z e d

$

Now what confuses me in this tutorial is this statement by the author:

"When each element of name is printed, only the first element 'a' shows up because the '\0' character is special and won't display."

I find that strange, since I define name as 'a' up top, but I never call out a '\0' character until after I print the name variable for a second time.

Why didn't it print with just 'a a a a', the same way numbers printed out '0 0 0 0'? Is it something to do with the ' ' around the a?

网友答案:

Why didn't it print with just 'a a a a', the same way numbers printed out '0 0 0 0'?

Because any array values (in an initialized array) that are not given explicit initializers get default-initialized to zero. ('\0' is just another way of specifying the NUL/zero value as a byte)

Is it something to do with the ' ' around the a?

No, it's just that

char name[4] = {'a'};

is logically equivalent to:

char name[4] = {'a', 0, 0, 0};

which is also logically equivalent to:

char name[4] = {'a', '\0', '\0', '\0'};

Note that any C-string compatible function (including printf()) will recognize a zero-byte as indicating the end of the string and will stop reading the string when it encounters the zero-byte. That is why only "a" is printed.

网友答案:

Unspecified array values are automatically initialized with integer 0 by most C compilers. This not only occurs for integer arrays, but for character arrays as well.

Now, the character literal \0 has the value 0. And in C, character values are stored as their ASCII values. So when the compiler appends 0s to the rest of the character array, they're treated as \0 literals or null, which is also used to mark the end of the string.

As proof, In your example, if you replace the print statement for individual character array elements with,

printf("name each: %c %d %d %d\n",name[0], name[1], name[2], name[3]);

You will find that it prints:

name each: a 0 0 0

This is because we have used the %d format specifier which is for integer values instead of the %c format specifier which expects a character value. And since the integer value zero is same as \0, the string terminates.

网友答案:

Because when you use something like:

char name[4] = {'a'};

name[0] = a and the rest has nothing (as '\0' only), and by the rest I mean name[1], name[2], ...

网友答案:

Why learn C the "hard way" when you can learn it the easy way by reading the book known as K&R C, 2nd Edition.

相关阅读:
Top