2

I'm trying to print a 2 character string. This is the part of that code.

char arraytwo[3]; 
// 2 characters 
for (i = 'a'; i <= 'z'; i++) 
{
    arraytwo[0] = i; 
    for (j = 'a'; j <= 'z'; j++) 
    {
        arraytwo[1] = j; 
        printf("%s\n", arraytwo);
    }
}

The output I am getting is this. For some reason it keeps adding "AZ" at the end of each iteration. What am I missing?

aaAZ
abAZ
acAZ
adAZ
aeAZ
afAZ
agAZ
ahAZ
aiAZ
ajAZ
akAZ
4
  • 9
    arraytwo[2] = '\0' Commented May 2, 2017 at 5:59
  • 2
    %s means to print a null-terminated string ... not just two characters Commented May 2, 2017 at 6:01
  • 1
    What if you just print the characters (without the array)? Commented May 2, 2017 at 6:01
  • How are you expecting printf to know that you want it to print two characters? How do you think this is going to work? Commented May 2, 2017 at 6:02

2 Answers 2

5

What you're missing is the definition of a string, it has to be null-terminated, by definition.

Quoting C11, chapter §7.1.1, (emphasis mine)

A string is a contiguous sequence of characters terminated by and including the first null character. [....]

In your case, for arraytwo,

  • it's automatic storage, and not initialized explicitly.
  • you did not null-terminate it manually.

So, technically, arraytwo is not a string.

In this usage, as an argument to %s format specifier, out of bound access happens in search of the null-terminator, which causes undefined behavior.

Also quoting chapter §7.21.6.1

s

If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type.280) Characters from the array are written up to (but not including) the terminating null character. [....]

Solution:

  • Either initialize the array elements to 0, something like char arraytwo[3] = {0};
  • Or, manually null terminate your array, like arraytwo[2] = '\0';

before using the array as string.

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

5 Comments

Thank you for pointing to the right direction. Terminated the string with arraytwo[2] = '\0'; and it now printed the characters correctly.
@zohan Well, that's good, however, I've just added one more alternative, please refer to the updated answer. :)
@Sourabh Thanks. Which book these chapters are from? I need to read it detail. :)
@zohan well, they are not from any book, the quotes are from C standard, C11.
@Sourabh Thanks :)
2

You were on a good way making the array with a length of 3. But you didn't initialize it to be all 0s. So at the beginning of your snipped, the array contains some random garbage and after the array also comes some random stuff.

When you pass a pointer to a printf("%s") function call, printf will output the memory starting with the pointed-to value and incrementing the pointer until it hits a '\0'.

In your case this already happend after 2 random characters. But it does not have to. And if the bell-ringing character (it was '\b' I believe) is there too in the random part of your printf call, your computer might even start to beep.

1 Comment

Thank you Mike. Understood.

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.