0
void main(){
char s[10];

s[1]='a';
s[6]='4';
for(int i=0;i<10;i++)
    printf("%c",s[i]);
}

I have this simple program. It gives the following output:

@aO   4

If I change the above code to:

void main(){
char s[10];

s[1]='a';
s[6]='4';
for(int i=0;i<10;i++)
    printf("%c",s[i]);

printf("\n");

for(int i=0;i<10;i++)
    printf("%c",s[i]);

}

Output changes to:

@a@ 4
@a@ 4

There are actually 2 cubes containing 4 numbers (1 number in each quadrant of the cube) between 'a' and the '@' after a but for some reason they are not showing.Please try the above codes in code blocks if I do not make sense to you.

I was expecting the output to be a 4 by the first code.Why is it not so? Also, why did the output change when I added more code? I was expecting the output to be:

a    4
a    4
9
  • 1
    Since the other elements of the array are not initialized you are invoking undefined behavior. Commented Dec 25, 2018 at 18:58
  • @Osiris “If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.”. It is not really UB Commented Dec 25, 2018 at 19:00
  • @Ctx Yes, but i think accessing it is UB. Commented Dec 25, 2018 at 19:04
  • @Osiris No, it isn't, that's what I tried to tell you Commented Dec 25, 2018 at 19:05
  • @Ctx In the standard C11 – ISO/IEC 9899:2011 in section J.2 Undefined Behavior it is written the value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.9, 6.8). Commented Dec 25, 2018 at 19:07

3 Answers 3

1

You got to understand behavior behind your program. So when you load your program it is given N bytes in memory and that N bytes gets reused many times and not erased. So in first instance your program have loaded some data into spot where s[0] would later reside, where as in second something was loaded where s[2] as well. That is why you are getting different output in these 2 cases.

So to sum it up: Your array is not initiated to 0 or unless you do it your self, it is given memory that have been previously used by the same program. To do it as it was pointed before you have to do this:

char s[10] = "          ";
....

One more thing I see is you were not expecting space before a, in C/C++/Java array indexes start at 0. So if you do:

char s[4];

s[1] = 'a';
s[2] = 'b';
s[3] = '\0';

print ("%s", s);

you would probably get:

@ab

@ comes up because there was nothing written by you as programmer in memory spot where s[0] resides.

Note that every time you have string in C you have to terminate it with '\0' character.

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

Comments

1

The problem is that you didn't initialise your array. In order to get that output you can do it in this way:

char s[10];

for(int i=0;i<10;i++)
{
    s[i] = ' ';
}
s[1]='a';
s[6]='4';
for(int i=0;i<10;i++)
    printf("%c",s[i]);

Comments

1

Here

char s[10]; /* uninitialized array */ 

character array s is not initialized & default s elements contain garbage data as it has automatic storage duration and you have assigned values to only s[1] and s[6].

Hence array elements except s[1] and s[6] contains garbage data & printing their value is not guaranteed to be same Every time as you have shown.

To avoid this you can initialize array like this

char s[10] = ""; /* initialize all array elements  with 0 */

while declaring itself.

2 Comments

Understood. Also,can you explain why did the output change when I modified the first code?
@Aman you are trying to print uninitialized array elements in both cases, it causes UB. Also you can't assume every time you it produces same intermediate values.

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.