What does the term 'initializer' in C mean and what are the types of initializers?
What does the term 'initializer' in C mean and what are the types of initializers?
A few examples of initializers (in red)
Code:int a = 42; int b[100] = {42, 24, 0, -1}; a = 17; /* no initializer here */ const double g = 9.8;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Where have you heard about that? They should have an explanation for the terms ...
Scalar is when you initialize one "element". Aggregate is when you initialize more than one.
In the examples above, the 1st and 3rd example are scalar initializers, the 2nd is an aggregate initializer.
I don't really find the distinction useful.
The response from qny is correct. However consider this for loop:
The purpose of the first part of the for loop syntax is for an initializer, even though it may not be formally an "initializer". So, informally, I would say that an initializer is any statement which assigns an initial value to a variable.Code:for (i=0; i < 30; i++) printf(".");
There is another type of initializer, which is a static initializer. Static initializers happen conceptually at compile time. They can be used to make a function which "remembers" its values in between calls.Code:i=0; while (i < 30) { printf("."); i++; }
Code:void print_foo() { static int foo=0; printf("Foo is %d\n", foo); foo++; }
@ c99tutorial So to sum up in for eg: int a=6; 6 is an scalar initializer right
or the whole statement is initializer?
The statement is a definition, an initialization, and a declaration all in one. the initializer is 6.
You can have declarations that are not definitions;
You can have definitions thar are not initializations;
All initializatiosn are at the same time definitions and declarations.
Code:extern int a[256]; /* declaration, not definition, not initialization */ int b[100]; /* declaration and definition, not initialization */ int c[4] = {1, 2}; /* declaration, definition, and initialization */
@qny thank you for simple an useful explanation