Bobby
Bobby

Reputation: 121

Is `int x;` really just a declaration or an implicit definition too?

In C, suppose an object is declared, for instance int x; as an automatic variable. Without initializing it with any value, is it still considered just a declaration and not definition?

Cause ChatGPT argues that when an object is declared, sometimes the compiler sets aside some memory associated with it (object). Therefore it is an implied definition. Please only answer if you are completely sure with the C semantics.

Upvotes: 2

Views: 162

Answers (2)

0___________
0___________

Reputation: 67430

  1. Declaration: A declaration introduces the name of a variable, function, or type to the compiler. It specifies the type of the entity but does not allocate memory (for variables) or provide implementation (for functions). Key Characteristics:

    • Informs the compiler about the name and type of the entity.
    • Does not allocate memory for variables or define the body of functions.
  2. Definition: A definition not only declares the entity but also allocates memory (for variables) or provides the implementation (for functions). Key Characteristics:

    • A definition is a declaration plus additional information (memory allocation or implementation).
    • There can only be one definition of a variable or function in a program (except the possibility of having Tentative Definitions of the same object in many compilation units).
Aspect Declaration Definition Tentative Definition
Purpose Introduces an entity to the compiler. Allocates memory (for variables) or provides implementation (for functions). Serves as a placeholder; acts as a definition if no full definition is provided later.
Memory Allocation No (for variables). Yes (for variables). Allocates memory only if no other definition exists in the same translation unit.
Function Body No (only function prototype). Yes (full implementation). Not applicable (only for objects, not functions).
Multiplicity Can appear multiple times in a program. Must appear exactly once for each entity. Can appear multiple times; merged into one definition.
Storage Duration N/A (does not allocate memory). Allocates memory for static duration objects. Allocates memory for static duration objects if no full definition is present.
Initialization Not applicable. Variables can be initialized (e.g., int x = 5;). Initialized to zero if no full definition is provided.
Examples (Variables) extern int x; int x = 42; int x;
Examples (Functions) int add(int a, int b); int add(int a, int b) { return a + b; } Not applicable (only applies to variables).

So int x; is a declaration and definition (tentative definition in the file scope) in both file and block scope.

Declarations and definitions are mentioned in the C standard in many places:

  • 6.2.2: Linkages of Identifiers
  • 6.7: Declarations
  • 6.7.6: Declarators
  • 6.9: External Definitions
  • 6.9.1: Function Definitions
  • 6.9.2: External Object Definitions

Upvotes: 0

dbush
dbush

Reputation: 223629

An object declared at file scope without an initializer as in this case is considered a tentative definition. Other identical declarations may appear, each of which is also a tentative definition.

If no definition (i.e. one with an initializer) exists, then that tentative definition, along with any others that may appear, are considered a "full" definition for that identifier which is initialized to 0.

This is spelled out in section 6.9.2p2 of the C standard:

A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

Such a declaration at block scope (i.e. in the body of a function) is a unique declaration for that object and is also a definition, as such a declaration sets aside storage for that object.

All definitions are also declarations, as stated in section 6.7p5:

A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:

  • for an object, causes storage to be reserved for that object;
  • for a function, includes the function body;
  • for an enumeration constant, is the (only) declaration of the identifier;
  • for a typedef name, is the first (or only) declaration of the identifier.

Upvotes: 10

Related Questions