8

Does int a = int(); necessarily give me a zero?

How about if int is replaced by char, double, bool or pointer type?

Where is this specified in the language standard, please?

updogliu
  • 6,066
  • 7
  • 37
  • 50
  • possible duplicate of [Do built-in types have default constructors?](http://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors) – Kaz Apr 11 '12 at 06:14

2 Answers2

18

Does int a = int(); necessarily give me a zero?

Yes, the standard guarantees that it gives you zero.
This is known as Value Initialization. For the type int, Value Initialization basically ends up being an Zero Initialization.

Where is this specified in the language standard, please?

The rules are clearly specified in the standard in section 8.5. I will quote the relevant ones to the Q here:

C++03: 8.5 Initializers
Para 7:

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

Value Initialization & Zero Initialization are defined in 8.5 Para 5 as:

To value-initialize an object of type T means:

— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
otherwise, the object is zero-initialized

To zero-initialize an object of type T means:

if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject
is zero-initialized;
— if T is a union type, the object’s first named data member is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.

Note: The bold texts are emphasized by me.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • And there's another paragraph somewhere where it spells out that value initialization for `int` means initialize to `0`... – juanchopanza Apr 11 '12 at 06:20
  • @juanchopanza: If you follow the bold text in the standard quotes above, You will see how it comes to that. – Alok Save Apr 11 '12 at 06:23
  • I know that, the text wasn't there when I wrote the comment. I recently went through this in c++11 trying to figure out `{}` initialization :) – juanchopanza Apr 11 '12 at 06:25
  • @AlokSave: What you think about constructors of built in types? int() looks like a constructor call & Bjarne Stroustrup in his book The C++ programming language says that built in types have also constructors. Visit & read this: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15 and http://www.geeksforgeeks.org/c-default-constructor-built-in-types/ – Destructor May 29 '15 at 03:51
  • @AlokSave: Please read the sections 10.4.2 & 6.2.8 of The C++ Programming Language by Stroustrup. – Destructor May 29 '15 at 03:54
-1

Yes, any built-in type is always initialized to zero when default-initialized. Keep in mind that in most scenarios a built-in type is not default initialized so this won't necessarily print out 0:

int i;
std::cout << i << "\n";
orlp
  • 112,504
  • 36
  • 218
  • 315
  • Value of uninitialized built in type as in the example is indeterminate and accessing is undefined behaviour(https://en.cppreference.com/w/cpp/language/default_initialization) – Tobias Nov 04 '22 at 20:03