What is the value of an uninitialized string in c?

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 is the meaning of an uninitialized string in C? Does the compiler automatically allocate storage of size 10 and populate it with garbage values? What mainly happens when writing the above code?

+5
source share
6 answers

10 bytes are allocated on the stack, that’s all. Their value remains as it is, which means that this is what was ever written before such 10 bytes before it was allocated.

+7
source

, - . , , , 10 .

- C . , .

: , , , , 0, . undefined , undefined - , .

+3

- , , Java - undefined. : , .

+1

, , . .

, undefined. , , . - . . .

, , , . , , , . , - .

+1

C 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 are unlucky, it will not have a final null byte, so you cause undefined behavior and, possibly, the call printf()will fail (segmentation error, someone?).

+1
source

contains garbage values ​​(random). Please read more about storage classes for a better understanding.

0
source

All Articles