i486
i486

Reputation: 6573

Define volatile int variable as extern int

What will happen if a variable is defined as volatile int x in file1.c and extern int x in file2.c (instead of extern volatile int x)? Is there a chance for compiler to know that x is volatile and compile as such in file2.c.

Upvotes: 1

Views: 406

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409432

The compiler only have knowledge of the current translation unit (basically the current source file with all includes), nothing else.

If an extern variable is not declared using the correct type, then you will have undefined behavior.

Upvotes: 8

Related Questions