Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: variable declaration initialization order

Tags:

c++

When I'm defining some variables like this:

int a = pop(), b = pop(), c = pop();

does C++ give a guarantee that a is going to be initialized first, then b and then c? or is the order not defined?

like image 482
shoosh Avatar asked Mar 06 '13 13:03

shoosh


People also ask

How variables are declared and initialized in C language?

Initialization of Variable int a=10; int a=b+c; a=10; a=b+c; Multiple variables can be initialized in a single statement by single value, for example, a=b=c=d=e=10; NOTE: C variables must be declared before they are used in the c program.

In what order are global variables initialized?

Example# Whereas inside a Translation Unit, order of initialization of global variables is specified, order of initialization across Translation Units is unspecified. That may lead to Static Initialization Order Fiasco.

How are the variables declared in C?

Variable Declaration in C You will use the keyword extern to declare a variable at any place. Though you can declare a variable multiple times in your C program, it can be defined only once in a file, a function, or a block of code.


1 Answers

[dcl.decl]/3 says

-3- Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.

Which means your code is treated like:

int a = pop();
int b = pop();
int c = pop();
like image 169
Jonathan Wakely Avatar answered Nov 12 '22 06:11

Jonathan Wakely