2

Possible Duplicate:
The initialization of static variable in C

I know that either global variables or static are automatically initialized with zero in C. However, I'm not sure if both or only one of them are initialized. Note that I'm not talking about variables defined in functions but globally in the .c file.

So which of the following variables are automatically initialized with zero?

static struct mystruct var1;
struct mystruct var2;
static struct { int x; int y; } var3;
Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636

2 Answers2

6

C FAQ.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Global variables will be initialized to 0. In Linux, and I'm guessing most OSes, those variables are allocated in newly mapped memory pages which are set to 0 by the kernel. Thus for globals it should be safe to assume they are always initialized to 0. (At least in Linux) – Chang Hyun Park Apr 24 '14 at 13:44
  • It is a link-only answer. Please consider editing the pertinent information into your answer – Lorraine Nov 21 '22 at 16:44
1

I ran the following code in codepad

struct mystruct { int a; };

static struct mystruct var1;
struct mystruct var2;
static struct { int x; int y; } var3;

#include <stdio.h>
void main()
{
    int x;
    printf("var1.a: %d\n", var1.a);
    printf("var2.a: %d\n", var2.a);
    printf("var3.x: %d\n", var3.x);
    printf("var3.y: %d\n", var3.y);
    printf("x: %d\n", x);
}

results:

var1.a: 0
var2.a: 0
var3.x: 0
var3.y: 0
x: 1075105060

Regardless, I don't like making assumptions about initialisation but YMMV.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Asim Ihsan
  • 1,501
  • 8
  • 18
  • Here's an example of the danger of assumptions: https://bugs.launchpad.net/ubuntu/+source/gcc-4.1/+bug/254025 A standard saying X doesn't prevent an implementor doing Y. One saying that I always keep at the top of my mind with respect to protocol implementations is: "Be strict in what you send but permissive in what you accept". IMHO this applies to variable initialisation as well. – Asim Ihsan Dec 09 '10 at 13:34
  • 2
    I would -1 your comment if I could. There is no danger in assuming the language behaves as required on such fundamental things. Should we also suppose 2+2 might not be 4? The link you gave seems irrelevant. It was closed as wontfix with no explanation of what was going on, but it's surely not an issue of uninitialized static variables. Much more likely it's a memory corruption bug in the program and had nothing to do with gcc. – R.. GitHub STOP HELPING ICE Dec 09 '10 at 14:30
  • By the way, since the last line of your program uses the value of `x`, a conformant implementation *could* output values other than 0 for the first 4. If your program invokes undefined behavior, the whole output is allowed to deviate in any way the implementor pleases from what you would expect with the UB code removed. – R.. GitHub STOP HELPING ICE Dec 09 '10 at 14:32
  • 2
    It's **int main(void)**, man! With a return 0 if you care. – Jens Aug 30 '12 at 19:13