5

Possible Duplicate:
initial value of int array in C

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(){
    char name[10];
    printf("%s\n", name);
    return 0;
}

What value does an uninitialized string in C hold ? Does the compiler automatically allocate a storage of size 10 and fill it with garbage values ? What basically happens on writing the above code ?

Community
  • 1
  • 1
Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
  • @Yuushi the link is really helpful. But is there a direct correspondence between C strings and C int arrays ? Can we also initialize all the characters of the string to a specific value by say writing: char name[10] = {'a'} ; ?(we can do this for C int arrays as written in the 2nd answer to the question which you pointed) . – Nikunj Banka Feb 05 '13 at 08:06
  • The initialization of `int` and `char` arrays is different, yes, and you cannot write `char c[10] = {'a'}` to initialize a `char` array with all `a`'s (nor can you do `int x[10] = {1}` to initialize an int array with all ones, though). However, that isn't what your question was asking. The question as stated is exactly the same, and the answers exactly the same, regardless of type. – Yuushi Feb 05 '13 at 08:15
  • 1
    @Nikunj Banka No you can not initiate char array in this way `char name[10] = {'a'}`. this could be done only if you want to initiate with 0 like this `char name[10] = {0}` – MOHAMED Feb 05 '13 at 08:15
  • you can do it with memset `memset(name,'a',9); a[10]='\0';` – MOHAMED Feb 05 '13 at 08:18
  • 1
    @Yuushi, in what initialization of `char` and `int` is different? `char` is just one integer type in C like others. – Jens Gustedt Feb 05 '13 at 08:23
  • 1
    An "uninitialised string" sounds invalid. How can you be sure that a sequence ends in '\0' without putting the '\0' there? @MohamedKALLEL `char name[10] = {'a'};` is a valid initialisation. It'll set the first byte to 'a', and the following nine bytes to 0. – autistic Feb 05 '13 at 08:41

6 Answers6

9

10 bytes are allocated on the stack, that's all. Their value is left as is, that means it is what ever had been written to such 10 bytes before having been allocated.

alk
  • 69,737
  • 10
  • 105
  • 255
3

As the string is uninitialized, the value is not defined - it may be anything. I would also say it is unsafe to print uninitialized string as it does not have a terminating zero character, so in theory you may end up printing way more than 10 chars.

And another thing - C does not fill the storage with anything. It just leaves it the way it is.

EDIT: Please note I am not saying that as long as you have a 0 terminating character it is safe to access the uninitialized string. Invoking an undefined behavior is never safe as it is undefined - you never know what will happen.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • +1 for "it may print more than 10 characters" .Also, you said that it will not fill in anything .But what if I declare the string static ? I think that the compiler will not fill in anything only if it is declared automatic . Please correct me if I am wrong . – Nikunj Banka Feb 05 '13 at 08:09
  • As such, this answer borders very closely to being incorrect/misleading. Even if there's a terminating 0 within those 10 bytes, it'd still invoke undefined behaviour to access an uninitialized variable (a c-string is different). – P.P Feb 05 '13 at 08:12
  • @KingsIndian I have only said that you can not be sure that only 10 chars will be printed, never said it is OK to use the variable if there is a terminating 0 character. – Ivaylo Strandjev Feb 05 '13 at 08:15
  • 1
    IMO, it gives the impression that if there's a terminating 0 within the allocated memory (10 bytes), it's ok to print/access it, which is wrong. – P.P Feb 05 '13 at 08:16
  • 1
    @KingsIndian added an edit to address your concern. – Ivaylo Strandjev Feb 05 '13 at 08:20
  • @NikunjBanka take a look [here](http://stackoverflow.com/questions/1294772/does-gcc-automatically-initialize-static-variables-to-zero) yes static variables are zero initialized. – Ivaylo Strandjev Feb 05 '13 at 08:22
1

The contents of uninitialized variables is - other than e.g. in Java - undefined. In other words: The contents consists of values pushed on the stack lately for other method invocations.

junix
  • 3,161
  • 13
  • 27
1

In your particular example, it's probably going to be zeroes. But it doesn't matter.

The key point is that it's undefined. If you can't trust it to always be the same, it's of no use to you. You can't make any assumptions. No other part of your code can depend on it. It's like it didn't exist.

If you're curious as to where the actual contents come from, they are remainders of previous execution contexts stored in the stack. If you run a few function calls, you're going to leave garbage lying around that your program will feel free to overwrite. Those only-good-for-overwriting bytes may end up in your string.

salezica
  • 74,081
  • 25
  • 105
  • 166
  • "*In your particular example, it's probably going to be zeroes.*" I doubt this. Please explain. – alk Oct 15 '16 at 07:42
1

The C standard uses the term "unspecified", i. e. it can be anything. In real life, it will most likely be filled with random garbage, and if you're unlucky, it won't have a terminating zero byte, so you invoke undefined behavior and probably the call to printf() will crash (segmentation fault, anyone?).

  • So it means different compilers can decide on different values, but that won't be portable? – Ghasan غسان Feb 05 '13 at 07:50
  • 1
    @Noor This is not portable even in the slightest manner. It can even vary from one running of the program to another. Really, expecting a reasonable explanation for undefined behavior and unspecified values just makes no sense. –  Feb 05 '13 at 07:51
1

it contains garbage (random) values. Please do see more information on storage classes to have a better understanding.

Megharaj
  • 1,589
  • 2
  • 20
  • 32