1

I know it is a very simple code, but when printing the elements of the array the 4th element is printed twice like showed bellow.

void printWeekDays(){
char days[7][9] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
for(int i=0; i < 7; i++){
    printf("%s \n",days[i]); 
}

Monday
Tuesday
WednesdayThursday
Thursday
Friday
Saturday
Sunday

Here is my environment: Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0

5
  • Compile with warnings enabled. -Wall Commented Nov 29, 2018 at 13:18
  • And the correct fix is to have static const char *days[7] = { ... Commented Nov 29, 2018 at 13:19
  • xing If I change the days[7][9] to days[7][10] it works, but why we have this strange behaviour? The C compilers seems to concatenate the 3th and 4th element. Commented Nov 29, 2018 at 13:20
  • @RodrigoCamargos printf will check for \0 char to stop printing but your thursday does not have it. Commented Nov 29, 2018 at 13:21
  • @RodrigoCamargos strings are NULL terminated. But Wednesday\0 requires 10 characters. Unfortunately no warning is generated by clang because you're squeezing it to fit in 9. Commented Nov 29, 2018 at 13:22

2 Answers 2

3

"Wednesday" needs char[10] to hold the \0 char.

You get strange behavior because printf will search for \0 in the input string to stop printing but your "wednesday" does not have \0 char appended.

Hence printf goes on printing until it gets \0 that is after printing "thursday".

Change this

char days[7][9]

to

char days[7][10]

or

const char *days[7] //Compiler automatically adjust the size needed to store string literals.
Sign up to request clarification or add additional context in comments.

2 Comments

Got it. Thanks kiran.
Is there any difference between const char* and char* ? Why use "const"?
0

This is because "Wednesday" doesn't fit in an array of 9 characters. Sure, there's just 9 letters in Wednesday, but with the null terminator that is put at the end of each string, that's 10 characters. What happens is that the null terinator goes where the T from "Thursday" goes, and when "Thursday" is written to its corresponding place, that null terminator is overwritten. Hence why both are printed after eachother when you try to print Wednesday. It doesn't stop at the end of Wednesday because the next null terminator is at the end of Thursday.

To fix it, change this:

char days[7][9] = {

To this:

char days[7][10] = {

If you don't know about how null terminators work and would like to learn more, you can read about it here.

2 Comments

My compiler didn't issue any warning, but I understood the problem. Thanks.
@RodrigoCamargos C compilers don't warn for this bug: it is a defect in the C language. See this: stackoverflow.com/questions/52385351/c-string-at-the-end-of-0

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.