tags:

views:

179

answers:

4
char label[8] = "abcdefgh";
char arr[7] = "abcdefg";

printf("%s\n",label);
printf("%s",arr);

====output==========

abcdefgh

abcdefgÅ

Why Å is appended at the end of the string arr? I am running C code in Turbo C ++.

+14  A: 

printf expects NUL-terminated strings. Increase the size of your char arrays by one to make space for the terminating NUL character (it is added automatically by the = "..." initializer).

If you don't NUL-terminate your strings, printf will keep reading until it finds a NUL character, so you will get a more or less random result.

Heinzi
Here's a tutorial on the theory behind this: http://www.cplusplus.com/doc/tutorial/ntcs/
Heinzi
Or, you could use empty brackets, e.g. 'char label[]', to allow the compiler to correctly size the arrays for you.
Paul Baker
Or drop the explicit sizes and let the compiler do the counting; computers are good at counting!
Jonathan Leffler
But why it is showing output "abcdefgh" when giving array length 8 and assigning "abcdefgh" to the char array label? the difference between arr and label is that I have set the length 7 and assigned "abcdefg" to arr and the length 8 and assigned "abcdefgh" to label. both are similar respect to length(7,8) and no. of characters(7,8)?
Himadri
It's a conincidence that it works. Apparently, in memory right behind array label, there is a NUL character, so printf stops. See Amits answer for what it could look like on a different machine.
Heinzi
+4  A: 

Your string is not null terminated, so printf is running into junk data. You need to use the '\0' at the end of the string.

Myles
+2  A: 

Using GCC (on Linux), it prints more garbage:

abcdefgh°ÃÕÄÕ¿UTÞÄÕ¿UTÞ·
abcdefgabcdefgh°ÃÕÄÕ¿UTÞÄÕ¿UTÞ·

This is because, you are printing two character arrays as strings (using %s).

This works fine:

char label[9] = "abcdefgh\0"; char arr[8] = "abcdefg\0";

printf("%s\n",label); printf("%s",arr);

However, you need not mention the "\0" explicitly. Just make sure the array size is large enough, i.e 1 more than the number of characters in your strings.

Amit
No need to write the "\0" explicitly: Double-quoted strings are implicitly NUL-terminated. You just need to make sure the array is large enough.
Heinzi
Yes. Updated my answer. Thanks
Amit
+5  A: 

Your variables label and arr are not strings. They are arrays of characters.

To be strings (and for you to be able to pass them to functions declared in <string.h>) they need a NUL terminator in the space reserved for them.

Definition of "string" from the Standard

    7.1.1 Definitions of terms
1   A string is a contiguous sequence of characters terminated by and including
    the first null character. The term multibyte string is sometimes used
    instead to emphasize special processing given to multibyte characters
    contained in the string or to avoid confusion with a wide string. A pointer
    to a string is a pointer to its initial (lowest addressed) character. The
    length of a string is the number of bytes preceding the null character and
    the value of a string is the sequence of the values of the contained
    characters, in order.
pmg
+1 for the formal answer!
Heinzi