0
#include <iostream>

int a;
void foo();

int main() 
{
 std::cout << "a = " << a << std::endl;
 foo();
 return 0;
}

void foo(){
    int b;
    std::cout << "b = " << b << std::endl;
}

Output:

a = 0
b = 32650

I have created a function named foo that declares a int variable and prints it. It prints some junk value because b is not initialized at the time of declaration then how is a getting initialized to 0 everytime?

Why is a initialized to 0 while b being initialized to some junk value?

Saif
  • 1,287
  • 1
  • 17
  • 32
  • 8
    `a` is initialized to 0 because it is a global variable, and all the global variables are implicitly initialized with 0 – user7860670 Jun 19 '18 at 19:35
  • @juanchopanza No, only `a` is getting initialized to `0` just because it is globally defined. – Saif Jun 19 '18 at 19:36
  • 1
    Global variables are auto initialized to zeros at startup. Local variables are not auto initialized at all (unless they are a class/struct type that has a default constructor) – Remy Lebeau Jun 19 '18 at 19:36
  • 4
    Note that observing that a variable prints `0` doesn't necessarily mean it's initialized. Reading uninitialized variables is undefined behavior and may very well also print `0`. Learning c++ by trying and observing (or trial-and-error) can be misleading as observed behavior may have nothing to do with the code if it contains undefined behavior. – François Andrieux Jun 19 '18 at 19:37
  • So this is a general rule in C++? – Saif Jun 19 '18 at 19:37
  • 1
    @SaifUrRahman Ah yes, I thought you were printing `c`. – juanchopanza Jun 19 '18 at 19:37
  • @juanchopanza Sorry about that `c`. I'll remove that. – Saif Jun 19 '18 at 19:38
  • @FrançoisAndrieux So, is it printing `0` just because it is an undefined behaviour at the time of reading the variable `a` inside printing statement or the zero value is assigned at the time of declaration. (for global variables)? – Saif Jun 19 '18 at 19:41
  • 1
    "It prints some junk value because b is not initialized" - It's worse than that. Reading an uninitialized variable is Undefined Behaviour and if your program contains UB the compiler is free to generate *whatever code it likes*, and not just for the access of that variable, but for *all* of your program. So by reading that uninitialized variable you've basically thrown all possibilities of reasoning about *any* part of your program into the trash. And yes, modern compilers do heavily exploit UB. Don't write code with UB. – Jesper Juhl Jun 19 '18 at 19:46
  • Possible duplicate of [Default variable value](https://stackoverflow.com/questions/6032638/default-variable-value) – Michael Doubez Jun 19 '18 at 19:54
  • 1
    @SaifUrRahman In this case, `a` is initialized to zero. Though printing `b` might also print zero, it could print anything or do something entirely different. – François Andrieux Jun 20 '18 at 13:19

2 Answers2

2

In the original version of C++ language standard, all variables with static storage duration were zero-initialized before any other initialization took place.

In modern C++ this initialization stage (aka static initialization) is split into constant initialization (for variables with explicit constant initializers) and zero initialization (for everything else). Your a falls into the second category. So your a is zero-initialized.

Automatic variables of non-class types, like your b, begin their life with indeterminate values, unless you initialize them explicitly. Using that indeterminate value in an expression leads to undefined behavior.

AnT
  • 302,239
  • 39
  • 506
  • 752
2

From draft of c++17 standard (other standards states almost the same):

3.6.2 Initialization of non-local variables [basic.start.init]

...

Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.

...

3.7.1 Static storage duration [basic.stc.static]

...

All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration . The storage for these entities shall last for the duration of the program (3.6.2, 3.6.3).

If you will go deep into definitions, you will find that a is actually has static storage duration (it is global variable) and therefore it is zero-initialized.

Community
  • 1
  • 1