0

We know that a struct can be declared as:

struct correct {
    int year;
    int *p; 
};

but we can't declare it as:

struct wrong {
    int year = 2020;
    int *p; 
};

So my questions are:

Q1-Why C is designed to not allow set a default value to a struct's member? Isn't it more convenient to allow it so we-programmers don't need to initialize it later?

Q2-For the correct struct type, is the year and p both initialized to 0 by GCC?

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • 1
    The first question is largely opinions based so is not suitable for Stack Overflow. For the second one it depends how/where a variable of that type is declared: [What happens to a declared, uninitialized variable in C? Does it have a value?](https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) – kaylum Aug 12 '20 at 06:58
  • @kaylum It's not opinion based. Why they made a certain design choice is something that can be objectively answered, provided you know the answer, which can be tough to find. – klutt Aug 12 '20 at 07:35
  • 1
    Here's an interesting article on the history of the C language: http://www.bell-labs.com/usr/dmr/www/chist.html. A core question was how a struct maps to memory. Quote: _"I wanted the structure not merely to characterize an abstract object but also to describe a collection of bits that might be read from a directory."_ Member initialization was unlikely considered a core question, as there are other ways to initialize structs. – user2962393 Aug 12 '20 at 08:17
  • You can however write `struct foo { int year; int *p; } bar = {2020};` and that initializes `bar.year` to 2020 and `bar.p` to NULL, fully standard compliant to any version of the language. – Lundin Aug 12 '20 at 11:26

1 Answers1

5

Answering Q2 first: Initialization to 0 is specified only for variables in particular storage spaces. Local variables inside functions are not specified to be initialized at all.

Having gotten this out of the way, this allows me to speculate about Q1: Why the designers of the C language have designed C that way.

The linker can place all the static (local to the compilation unit) and global symbols into a single memory area which can be easily zeroed out at program startup. That is a very simple concept which covers a lot.

Otherwise, you would need a per type way to define the default values. That could be done as either a prototype memory area containing the default values to be copied or a piece of code which sets a piece of memory to the default values. The concept for the latter exists, is called constructors, and is implemented in the programming language C++. Every time a variable of some type is defined somewhere, there would need to be code for copying the default values or calling the constructor.

C having being designed as a systems language for writing the Unix operating system, I presume that they just wanted to avoid all that complexity in compiler, linker, runtime, etc. If you want a more high level language, you can choose or invent another language. Even if back in the early 1970s, constraints in CPU cycles and memory and storage would probably favour less complex code as well.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
ndim
  • 35,870
  • 12
  • 47
  • 57