11

Given the following declaration:

char inputBuffer[12];

What is the default value of either char within the array? I'm interested in knowing this because if at any time I want to clear a position in the array, I need to know what value to give it.

Andrei Oniga
  • 8,219
  • 15
  • 52
  • 89
  • 1
    it depends where the array is declared. .e.g in a function it will be garbage in there, in the BSS it will be set to 0. Just have look at the wiki page http://en.wikipedia.org/wiki/Data_segment – AndersK Jul 17 '14 at 07:57
  • possible duplicate of [The initialization of static variables in C](http://stackoverflow.com/questions/13251083/the-initialization-of-static-variables-in-c) – harmic Jul 17 '14 at 07:58
  • 1
    It is called uninitialized because it is not initialized. If there was a defined default value, it would be called default-initialized. To clear a position in an array you have to do exactly nothing. – n. m. could be an AI Jul 17 '14 at 08:00
  • n.m. "To clear a position in an array you have to do exactly nothing." - not sure that's a meaningful or useful perspective... to have a notion of "clearing" values you have to either select a *sentinel* value that will be *deemed* clear/unused etc., or keep some separate, additional record of which indices are "clear". – Tony Delroy Jul 17 '14 at 08:18
  • " I'm interested in knowing this because if at any time I want to clear a position in the array, I need to know what value to give it." -- That makes no sense. Give it the value you want it to have. – Jim Balter Jul 17 '14 at 10:14
  • @TonyD If a position in its uninitialized state considered cleared, then it is cleared in any other state, there's no need to do anything. – n. m. could be an AI Jul 18 '14 at 07:14

2 Answers2

12

The array elements have indeterminate value except if the array it is defined at file-scope or have static storage-class specifier then the array elements are initialized to 0.

 #include <stdio.h>

 char inputBuffer1[12];          // elements initialized to 0
 static char inputBuffer2[12];   // elements initialized to 0

 void foo(void)
 {
     char inputBuffer3[12];         // elements have indeterminate value!
     static char inputBuffer4[12];  // elements initialized to 0
 }
ouah
  • 142,963
  • 15
  • 272
  • 331
  • In my scenario, the variable is global, so they're initialized to 0. If I were to "clear" the value of an array element, that would mean passing it 0, right? – Andrei Oniga Jul 17 '14 at 08:22
  • @AndreiOniga if you want to have an array explicitly initialized to 0 (to avoid indeterminate value): at declaration time, `char inputBuffer3[12] = {0};`, after it has been declared use `memset`. – ouah Jul 17 '14 at 09:11
10

If char inputBuffer[12]; is global or static, it is initialized with \0

char inputBuffer1[12];  /* Zeroed */
static char inputBuffer1[12];  /* Zeroed */

int fn()
{
  static char inputBuffer3[12];  /* Zeroed */
}

If it is local to function, it contains garbage value.

int fn2()
{
  char inputBuffer4[12];  /* inderminate value */
}

Quoting from ISO/IEC 9899:TC2 Committee Draft — May 6, 2005 WG14/N1124

Section 6.7.8 Initialization (emphasis mine)

10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these rules.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100