-1

I might be a something basic or whatever I am not able to still figure out what will happen

for eg

if I write

char temp[3]="";

or

 char temp[3]={0};

or

 char temp[3]={};

or

char temp;

What will be the initialization In all four cases.

And if 0 is stored is it stored as ascii value?

And if NULL then also is the ascii value stored.

If some elements are not declared do which value they have

garbage value or something specified

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
theboringdeveloper
  • 1,429
  • 13
  • 17

3 Answers3

2

The first three are equivalent and the array will be initialized to zero.

The last case is different, because you don't initialize the single character. How it's initialized depends on where you define the variable. If it's a global variable it will be zero-initialized. If it's a local variable then it will not be initialized at all and have an indeterminate value.

And zero is zero, i.e. 0 and not '0'.

Lastly, NULL is for pointers, not for non-pointer values. There is some confusion since the string terminator character '\0' (which is equal to 0) is also called the null character. The null character and a null pointer are two different things semantically, even if they can have the same actual value.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2
char temp[3]={};

isn't correct C.

char temp[3]={0};

initializes temp[0] to 0 and the rest is initialized as if they were default-initialized global variables, which for chars means that the rest will be 0 also.

char temp[3]="";

is initialization from a (empty) string which behaves the same as if you broke down the string into character literals and assigned those. For an empty string, the broken down version would be { '\0' }, which is the same as {0}, which makes it equivalent to the case above it.

char temp; will be default initialized (for chars == zeroed) if it's a global that isn't followed by a nontentative definition or it will have undefined contents if it's an automatic variable.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
2

1)

char temp[3]="";

and

 char temp[3]={0};

are equivalent. The array temp will be filled with 3 zeros. It's as if you had: char temp[3] = {0, 0, 0};.

2)

 char temp[3]={};

is illegal in C. Empty initializers are not allowed in C.

3)

char temp;

This, depends on where temp is declared.

If it's in a block scope then temp will be uninitialized and its value is indeterminate.
If it's at file scope then temp will be initialized to 0, provided there are no other definitions for it1. It's as if you had: char temp = 0;

1 This may sound odd. But C has a concept called "tentative definitions". See: About Tentative definition.

Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238