c++ c-style zero-initialization { 0 }

https://en.cppreference.com/w/cpp/language/zero_initialization shows that zero-initialization happens in the following scenario:

int array[32] = {};

; but never says anything about this:

int array[32] = { 0 };

Does the latter also zero-initialize the whole array in c++, or only the first element? If so, is it also true for structs?

Does the latter also zero-initialize the whole array in c++, or only the first element?

Yes.

The first style is C++ style.

The second one is old C style.

Both zero initialise the whole array elements (not just the first element).

If so, is it also true for structs?

Yes.

C Programming Tutorial for Beginners, Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) The digraph ck is often used to represent the sound / k / after short vowels, like "wicket". C is the eleventh least frequently used letter in the English language (after G, Y, P, B, V, K, J, X, Q, and Z ), with a frequency of about 2.20% in words.

"C" Programming Language: Brian Kernighan, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: Aug 15, 2018 C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). The UNIX OS was totally written in C. Today C is the most widely used and popular System Programming Language. Most of the state-of-the-art software have been implemented using C.

...but never says anything about this

It does. This is actually aggregate initialization.

As per cppreference:

If the number of initializer clauses is less than the number of members and bases or initializer list is completely empty, the remaining members and bases are initialized by their default member initializers, if provided in the class definition, and otherwise by empty lists, in accordance with the usual list-initialization rules (which performs value-initialization for non-class types and non-aggregate classes with default constructors, and aggregate initialization for aggregates). If a member of a reference type is one of these remaining members, the program is ill-formed.

This guaruntees that the remaining elements of the array are initialized with empty lists. For int (which is scalar) this initializes it to zero. For structs this would perform value-initialize if the struct is not an aggregate or aggregate-initialize otherwise.

C Tutorial, "C" is one of the most widely used programming languages of all time. Prof Brian Kernighan Duration: 8:26 Posted: Aug 18, 2015 C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.

Use CppInsights to see what actually happens:

This:

int main()
{
    int array0[5];
    int array1[5] = {};
    int array2[5] = { 0 };
    int array3[5] = { -1 };
    int array4[5] = { 1, 2 };
}

is equivalent to:

int main()
{
  int array0[5];
  int array1[5] = {0, 0, 0, 0, 0};
  int array2[5] = {0, 0, 0, 0, 0};
  int array3[5] = {-1, 0, 0, 0, 0};
  int array4[5] = {1, 2, 0, 0, 0};
}

So, int array2[5] = { v } explicitly initializes the first element with v, and default initializes all other elements.

Learn C, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. This is a list of operators in the C and C++ programming languages.All the operators listed exist in C++; the fourth column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.. When not overloaded, for the operators &&, ||, and , (the comma operator), there is a sequence point after the evaluation of the first operand.

Learn C Programming, learn-c.org is a free interactive C tutorial for people who want to learn C, fast. As well as C and Simula's influences, other languages also influenced this new language, including ALGOL 68, Ada, CLU and ML . Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining and default arguments.

C Programming Language, C is a powerful general-purpose programming language. Our C tutorials will guide you to learn C programming one step at a time with the help of examples. C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs.C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This C++ tutorial adopts a simple and practical approach to describe the concepts of C++ for beginners to advanded software engineers.. Why to Learn C++. C++ is a MUST for students and working

Cprogramming.com: Learn C and C++ Programming, This page contains all topics of C with clear explanations and examples. It also contains job interview questions, MCQ quizzes and output based questions. Microsoft C++, C, and Assembler documentation. Learn how to use C++, C, and assembly language to develop applications, services, and tools for your platforms and devices.

Comments
  • The latter is particularly peculiar to use because any other value initializes only the first element (and zeroes the rest, thx to @ChrisDodd for clarifying that)...
  • For what it's worth int array[4] = { 1 }; would produce {1, 0, 0, 0} so don't think the latter syntax initializes the whole array with that value.
  • @Resurrection: not true -- any array initializer that is smaller than the array will zero initialize all the trailing (unspecified) elements.
  • a C statement like: int array[32] = {}; results in the compiler outputting the following: "untitled2.c:1:17: warning: ISO C forbids empty initializer braces [-Wpedantic]" suggest: int array[32] = {0}; Notice the initializer inside the braces. However, if you want to use a value other than 0, suggest: for( size_t i = 0; i< 32; i++ ) { array[i] = 1; }
  • the C language and the C++ language are very different, (and getting further apart with each new release) Please pick one and erase the other tag
  • Both zero initialise the whole array elements this can be misleading. It's true only if the value used in the initializer is 0. Otherwise, any array initializer shorter than the size of the array will initialize trailing values with 0.
  • Yes that's what the question is about {0}. What you said is correct (if the value used in the initialised is something else), thanks
  • "The second one is old C style" -- is there a new C style for this?