0

I noticed a strange behaviour in my code. With specific size assigned to the array used for storing the initials, there are some extra characters stored in the initials array.

Below is the code that uses an array ("initials") of size 5:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    string name = get_string("Please input your name: ");
    char initials[5]; //initializes an array for storing initials extracted
    initials[0] = toupper(name[0]); //saves first letter in the "initials" array
    for (int i = 1, n = strlen(name)-1, count = 1; i < n; i++)
    {
        eprintf("i: %d, %s, \n", i, initials);
        if (name[i] == ' ')
        {
            initials[count] = toupper(name[i+1]);
            count++;
        }
    }
    printf("%s\n",initials); //prints the final answer
}

This code gives the correct output as seen below:

Please input your name: Willy Wonka
initials.c:13: i: 1, W, 
initials.c:13: i: 2, W, 
initials.c:13: i: 3, W, 
initials.c:13: i: 4, W, 
initials.c:13: i: 5, W, 
initials.c:13: i: 6, WW, 
initials.c:13: i: 7, WW, 
initials.c:13: i: 8, WW, 
initials.c:13: i: 9, WW, 
WW

However, it displays some strange behaviour when the array size is changed to value between 7 and 10, with no changes made to other parts of the code.

Output with array size 7:

Please input your name: Willy Wonka
initials.c:13: i: 1, WA, 
initials.c:13: i: 2, WA, 
initials.c:13: i: 3, WA, 
initials.c:13: i: 4, WA, 
initials.c:13: i: 5, WA, 
initials.c:13: i: 6, WW, 
initials.c:13: i: 7, WW, 
initials.c:13: i: 8, WW, 
initials.c:13: i: 9, WW, 
WW

Output with array size 8:

Please input your name: Willy Wonka
initials.c:13: i: 1, W]A, 
initials.c:13: i: 2, W]A, 
initials.c:13: i: 3, W]A, 
initials.c:13: i: 4, W]A, 
initials.c:13: i: 5, W]A, 
initials.c:13: i: 6, WWA, 
initials.c:13: i: 7, WWA, 
initials.c:13: i: 8, WWA, 
initials.c:13: i: 9, WWA, 
WWA

Output with array size 9:

Please input your name: Willy Wonka
initials.c:13: i: 1, W]A, 
initials.c:13: i: 2, W]A, 
initials.c:13: i: 3, W]A, 
initials.c:13: i: 4, W]A, 
initials.c:13: i: 5, W]A, 
initials.c:13: i: 6, WW]A, 
initials.c:13: i: 7, WW]A, 
initials.c:13: i: 8, WW]A, 
initials.c:13: i: 9, WW]A, 
WW]A

Output with array size 10:

Please input your name: Willy Wonka
initials.c:13: i: 1, W, 
initials.c:13: i: 2, W, 
initials.c:13: i: 3, W, 
initials.c:13: i: 4, W, 
initials.c:13: i: 5, W, 
initials.c:13: i: 6, WW]A, 
initials.c:13: i: 7, WW]A, 
initials.c:13: i: 8, WW]A, 
initials.c:13: i: 9, WW]A, 
WW]A

When the array size reaches 11, the output becomes normal again:

Please input your name: Willy Wonka
initials.c:13: i: 1, W, 
initials.c:13: i: 2, W, 
initials.c:13: i: 3, W, 
initials.c:13: i: 4, W, 
initials.c:13: i: 5, W, 
initials.c:13: i: 6, WW, 
initials.c:13: i: 7, WW, 
initials.c:13: i: 8, WW, 
initials.c:13: i: 9, WW, 
WW

What is causing this phenomenon? Is it a problem in my code or a bug in the IDE?

1

Simple explanation. The initials array was not initialized. Next, when printing, it is printing the entire 5 element array. So, when the valid data is output, the garbage data in the later elements is also printed. Lastly, the code does not set an end of string marker, \0, in the array at the end of the valid data. The printf() call depends on finding that marker. It will print out everything it finds until it finds that marker, whether intentionally placed, or found in random garbage data.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • Doesn't the line "char initials[5]" declare the initials array? Also where does the 'garbage' data come from? I am confused on where it's coming from, as my code is supposed to only add data when there is a blank space. This means there is no data to be inputted other than the initials.
    – blankcheck
    Oct 8 '17 at 9:39
  • Declaring an array only creates it. Initializing an array actually sets the contents to some starting value. Some vars are automatically initialized, others are not. For instance, an int will automatically be set to 0. Those that are not automatically initialized will contain garbage data - whatever happened to be in that physical memory the last time it was used. Best practice is to initialize all vars and arrays before using them. I'll leave it to you to review class material for the end of string marker.
    – Cliff B
    Oct 8 '17 at 18:53
  • Sorry for the late acknowledgement! I comprehend your explanation now
    – blankcheck
    Jan 15 '18 at 13:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .