0

I am quite new to C programming so feel free to correct me, I insist. My basic understanding of strings in C is when we initialize a string a null character is automatically assigned at the end of the string and the null character cannot be read read or written, but is used internally only.

So when I create a string of size 4 as char str[3] and assign a word to it say "RED" and print it using puts function or printf("%s",str), I get an unusual output printed as RED(SMIILEY FACE)

I then again reduce the size of string to char str[2] and assign RED to it and then compile it and the again receive a output stating RE(Smiley face)

If someone can explain it to me I will be thankful . Posting the C code below

    int main()
{

 char s1[3]="RED";
 char s2[]="RED";
 puts(s1);
 puts(s2);
 printf("%s",s1);
 return 0;
}
1
  • 2
    "when we initialize a string a null character is automatically assigned" -- only if you provide enough space for it. Commented Nov 21, 2015 at 11:00

2 Answers 2

5
char s1[3] = "RED";

Is a valid statement. It copies 3 characters from the constant string literal "RED" (which is 4 characters long) into the character array s1. There is no terminating '\0' in s1, because there is no room for it.

Note the copy, because s1 is mutable, while "RED" is not. This makes the statement different from e.g. const char *s1 = "RED";, where the string is not copied.

The result of both puts(s1) and printf("%s", s1) are undefined. There is no terminating '\0' in s1. Treating it as a string with one can lead to arbitrary behavior.

char s2[] = "RED";

Here, sizeof(s2) == 4, because "RED" has four characters, you need to count the trailing '\0' when calculating space.

Sign up to request clarification or add additional context in comments.

4 Comments

char s3[2] = "RED"; is ill-formed
And just a quibble over "if you define", should really be if you declare, 'define' is quite different -- but I know what you meant, thus the word quibble.
@m-m: Can you pinpoint that in the standard? Looks like the assignment "minus 1" is defined for C++ as an exception, but I cannot for the sake of it find the case when you really have too many elements.
@DavidC.Rankin Thanks. Trivially solved by dropping of the last example until it is clarified what the required behavior is. C99 has an example but only for leaving out the terminating zero.
4

The null character takes one exra character(byte). So you need to use an extra space in addition to the number of characters in the word you are initializing.

 char s1[4]="RED"; //3 for RED and 1 for the null character

On the other hand

char s2[3]="RED";

there is no space for null character. "RED" is in there but you would encounter I/O problems when printing it as there is no null character stored at the end. Your data is stored fine but it can't be recognized properly by the printf as there is no null character.

 char s2[]="RED";

This would work as memory of 4 (bytes) is automatically assigned which includes space for the terminating null character.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.