Question

I've seen two ways to terminate a string.

// char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\n'}; //ignore this one, /n does not terminate

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}

and

char greeting[6] = {'H', 'e', 'l', 'l', 'o', 0};

Out of curiosity, which one is more standard?


Another question I have is with the below code

char greeting[] = {'H', 'e', 'l', 'l', 'o'};
printf("Greeting message: %s\n", greeting );
//output message is Greeting message: Hello? Question mark is upside down in Xcode

What happened here? I expected compiler to detect the size of the string itself and add a terminating character after the last character. Then in that printf statement, it would stop after the 'o'. Why is there a ? there? Also how did it know to stop after the '?' ? Was it possible for it to keep going and split out random characters until it crashes or miraculously find a random null terminating character in memory?

Was it helpful?

Solution

If you put a \0 in a string that is a way to tell the compiler that this is something special. Specifically it tells the compiler that it is a null. Also these are generally tucked inside of strings.

To answer your question I don't think that either is more standard. The more standard way to do this is:

char greeting[] = "Hello";

or more generally:

const char *greeting = "Hello";

This code:

char greeting[] = {'H', 'e', 'l', 'l', 'o'};
printf("Greeting message: %s\n", greeting );

prints out a strange character because the string is not explicitly terminated.

OTHER TIPS

"Which one is more standard?"

There's noting "more" or "less" standard about either approach. Both 0 and '\0' stand for constant zero of type int in C, meaning that both are absolutely equivalent and can be used interchangeably.

"I expected compiler to detect the size of the string and add a terminating character"

The compiler has no such concept as "string" and knows noting about any "strings". "String" in C is a purely run-time concept - an array of characters terminated by a zero character. The only exception from this are string literals, which are treated differently by the compiler (e.g. a zero terminator is added implicitly). What you have in your examples are just abstract char arrays. They are not "strings" to the compiler. The compiler does not see these arrays as "strings" and does not add anything to them. It only does what you tell it to do. And you explicitly asked it to create a char array without any zeros at the end. This char array is not a string and cannot be used as a string. If you attempt to use it as a string, the behavior will be undefined and it will be entirely your fault.

An addendum to the other answer. The difference between the two is very subtle. '\0' is a character constant that is zero (one byte.) '0' is an integer constant that is zero (usually four bytes.) When you assign an integer constant to a character, it is implicitly converted. So in your second example, 0 is implicitly converted to '\0' before assigning to the array.

The effect is identical, so it really doesn't matter, but you could argue that '\0' is slightly better as it does not have any implicit conversion.

But as the other answer says, 'char greeting[] = "Hello"' is much better than either.

The only reason to use the explicit array of characters is if you wanted to do something non-string-like with the character data, perhaps something like:

char greeting[12] = {'H', 'e', 'l', 'l', 'o', '\0', 'w', 'o', 'r', 'l', 'd', '\0'}

Of course, you wouldn't use standard C functions to deal with that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top