1

There is a code snippet as below,

int var1;
extern int var2;

It is a multiple choice. The answer is, First statement declares and defines var1, but second statement only declares var2. But I think it is supposed to be "Both statements only declare variables, don’t define them." Which one is correct?

codexplorer
  • 541
  • 5
  • 21
  • 3
    How does `int var1;` not define a variable? – Blaze Nov 22 '19 at 12:24
  • 1
    no. First declares and defines, the second one only declares. – 0___________ Nov 22 '19 at 12:24
  • 4
    The first is a *tentative* definition. See e.g. [this external and tentative definitions reference](https://en.cppreference.com/w/c/language/extern) for details. – Some programmer dude Nov 22 '19 at 12:26
  • https://stackoverflow.com/questions/3095861/about-tentative-definition – Antti Haapala -- Слава Україні Nov 22 '19 at 12:28
  • @codexplorer The answer depends on in which scope these declarations are used and how many there are translation units.:) – Vlad from Moscow Nov 22 '19 at 12:30
  • "The answer" is more correct than what you "think it is supposed to be". The first is a tentative definition (a declaration that may be a definition, depending on whether an actual external definition - e.g. one with an explicit initialiser - is found in the same compilation unit). The second is a declaration, but not a definition (tentative or otherwise). – Peter Nov 22 '19 at 12:32
  • @SanderDeDycker indeed I thought about tentative definitions in multiple compilation units, which is unsupported. You are correct that it is specified for a single one. Didn't realize that this is called tentative, too – Ctx Nov 22 '19 at 12:53

3 Answers3

6

This might help. source

Declaration of a variable is for informing to the compiler the following information: name of the variable, type of value it holds and the initial value if any it takes. i.e., declaration gives details about the properties of a variable. Whereas, Definition of a variable says where the variable gets stored. i.e., memory for the variable is allocated during the definition of the variable.

In C language definition and declaration for a variable takes place at the same time. i.e. there is no difference between declaration and definition. For example, consider the following declaration

int a;

Here, the information such as the variable name: a, and data type: int, which is sent to the compiler which will be stored in the data structure known as symbol table. Along with this, a memory of size 2 bytes(depending upon the type of compiler) will be allocated.

Suppose, if we want to only declare variables and not to define it i.e. we do not want to allocate memory, then the following declaration can be used

extern int a;

In this example, only the information about the variable is sent and no memory allocation is done. The above information tells the compiler that the variable a is declared now while memory for it will be defined later in the same file or in different file.

Ahmed Tounsi
  • 1,482
  • 1
  • 14
  • 24
  • It is said "In C language definition and declaration for a variable takes place at the same time. i.e. there is no difference between declaration and definition.". What if the variable is a pointer? Because definition of a variable says where the variable gets stored. i.e., memory for the variable is allocated during the definition of the variable.Then why should I still allocate a memory for the pointer? Thanks, – codexplorer Nov 22 '19 at 14:37
  • Could I say, a pointer variable is just assigned a indeterminate value when it is a tentative definition if in the end of translation unit there is no initializer for it to make it a full definition? Thanks, – codexplorer Nov 23 '19 at 00:49
2

The answer depends on several factors.

If these declarations

int var1;
extern int var2;

are block scope declarations then the first declaration is also a definition and the second declaration is just a declaration without a definition. The variable var1 is not initialized that is it has an indeterminate value.

If these declarations are declarations of the file scope then whether the first declaration is a definition is defined by whether the declaration has an external definition.

If the declaration does not have an external definition then this declaration named as tentative definition is a definition and have an implicit initializer equal to 0.

As for the second declaration then again whether it is a definition depends on whether there is an external definition or not. If there is no external definition then the linker can either create the definition or issue an error.

In C a declaration with the file scope is also a definition when either it has an initializer or it is a tentative definition without an external definition.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Actually, the extern keyword extends the visibility of the C variables and C functions.

Declaring vs Defining a variable?

Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them.

Snippet 1:

extern int var;
int main(void) 
{ 
    var = 10;
    return 0; 
}

Snippet 1 throws an error in compilation. Because var is declared but not defined anywhere. Essentially, the var isn’t allocated any memory. And the program is trying to change the value to 10 of a variable that doesn’t exist at all.

Snippet 2:

#include "somefile.h" 
extern int var; 
int main(void) 
{ 
   var = 10; 
   return 0; 
} 

Supposing that "somefile.h" has the definition of var. Snippet 2 will be compiled successfully.

Premkumar chalmeti
  • 800
  • 1
  • 8
  • 23