0

Apparently C doesn't like the declaration:

char some_array[n] = "Text here"; <== where n is an int of suitable size....

Well, actually, I guess it likes it just fine, but the output surprises me! Here's an example.

    char two[4] = "What";
    printf("2: %s \n", two);

Output is:

2: WhatWhat 

WhatWhat the heck is WhatWhat doing there?!

So I try adding:

    char test[4] = "abcd";

Output is:

2: WhatWhatabcd
3: abcd

Yeah, I get it, my syntax isn't favored. But why?! And what is going on with the actual variables?

4
  • 2
    undefined behavior because of buffer overrun Commented Mar 1, 2013 at 5:19
  • Yeah, I changed the array size but I forgot to save the result before I recompiled. Embarassing! Commented Mar 1, 2013 at 5:21
  • how did you get 3: abcd? Commented Mar 1, 2013 at 5:22
  • Not sure. Must have had a null in the stack spot afterwards....? Commented Mar 1, 2013 at 5:23

4 Answers 4

3

String literals have an extra character - the nul terminator.

So you need to have it be length 5:

char two[5] = "What";

Or this if possible:

char two[] = "What";

(you're getting a buffer overrun or something otherwise)

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

2 Comments

Wow, nevermind. Didn't save the changes when I recompiled. Lol what a sad question now.
@d0rmLife it doesn't happen when I test it: stacked-crooked.com/view?id=8a6206afbc36664379851d10dbe0944b
1

While dealing with strings you should take the null terminator into consideration. Here you are trying assign a string literal of 4 characters to an array of 4 characters, thereby not providing an extra character space for the null terminator.

change the code as below:

char two[5] = "What";
char test[4] = "abcd";

As to why for "What" you are having the problem and not having it for "abcd", the behavior will be undefined for such cases, as the program will try to search the memory till a null terminator is found. In your case, it is printing incorrect, it can lead to a crash, hang or god knows what.

Comments

1

A string is terminated by only a NULL character, in your case since you have allocated a space of 4 bytes and you are storing "what" in it exactly 4 characters so there is no space left for NULL termination.

While printing printf starts to read the data from the starting address that you mention and goes till NULL character... So its possible that the adjacent memory where the buffer is allocated have some junk characters(since memory is not flushed automatically, in all cases ,when the buffer is freed) so you are seeing whatwhat or it might be whatjkefwhksdjfsdfjsf some other day... or possibly a segmentation fault if it goes to read till an unauthorized location of memory.

Comments

1

because your max array index is 4, you are facing this problem.

make the max array index to 5, e.g. char test[5] so that it can terminate the string with NULL. your problem will be sorted.

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.