Declarations, initialization, and assignment
Separate introducing a name, defining an object, supplying its first value, and changing it later.
In this chapter9 sections
Introducing a variable, giving it a starting value, and changing that value are different actions. They can look similar in C. Let us read a few short declarations carefully and separate their jobs.
1. A declaration introduces information
A declaration tells C about a name and its properties, including a type where appropriate. Introduce a name in the right scope before using it. A declaration is not automatically an instruction to read input or display output.
int attempts;
attempts = 0;Assume these lines are inside a function. The first introduces an ordinary local integer object. The second assigns zero to it. The first line supplies no explicit starting value, so the second line must happen before any read in this example. Introducing an object and giving it a usable value are separate steps.
2. A definition has an additional role
An object definition causes storage to be reserved under its lifetime rules. A function definition supplies a body. A declaration can also describe an entity defined elsewhere. This lets one source file know how to use something whose definition is in another source file.
extern int total;
int total = 0;The first line is a declaration without an initializer; in this example it is not the object’s definition. The second line defines the object. They are shown together to compare roles; a multi-file design commonly places a shared declaration in a header and a matching definition in one source file. extern does not fetch the value from another file during every use.
Go deeper: file-scope declarations need care
At file scope, int total; is commonly a tentative definition with external linkage, not the same case as an uninitialized ordinary automatic local. If no other external definition for that object appears in that translation unit, the tentative-definition rules provide a definition as if initialized to zero at its end. Initializers, storage-class specifiers, and scope all matter; do not classify declarations by one keyword alone.
3. Initialization supplies the initial value
int attempts = 0;The 0 is an initializer in this declaration. It gives the object its starting value as part of the definition. For this ordinary automatic local, the initializer is evaluated when execution reaches the declaration. A later assignment is a different construct, even though it can also use =.
When an ordinary automatic local int has no initializer, do not read it until you supply a valid value. Its value is indeterminate. Static-storage objects have different rules and receive zero values when no explicit initializer is given. Always identify the case before stating a starting value.
4. Assignment changes the destination object
int count = 4;
int previous = count;
count = 6;previous receives a copy of count’s current value. They are separate objects. Later assigning six to count leaves previous at four. Assignment stores a suitable value in its destination; it does not create a permanent link between these integer objects.
= performs assignment in an assignment expression; == compares values. C calls a suitable assignable expression a modifiable lvalue. The phrase is about the kind of expression and object it designates, not merely being text on the left of an equal sign. A numeric constant is not a destination for a new stored value.
5. Const constrains access
const int maximum_attempts = 3;This object is defined with a const-qualified type. You cannot use an expression referring to it as an ordinary modifiable destination. Attempting to modify an object originally defined as const has undefined behavior, even if a cast removes the visible qualification. Const-qualified access to some other, mutable object is a separate case; qualifiers do not freeze all possible access paths.
In C17, an ordinary const int object is not automatically an integer constant expression. That is a specific language category, not just an everyday description of a value that should remain unchanged. C23’s constexpr facility is separately labeled and concerns constant objects; it is not the C++ facility for constexpr functions.
6. Scope and lifetime answer different questions
Scope describes where a name can be used in the source text. Lifetime describes the execution period during which an object exists. An ordinary automatic local and a static local can have similar-looking block scope while their objects have different lifetimes. A physical stack diagram is not the definition of either concept.
Why can const int limit = 3; be valid even though later assigning a new value through limit is not allowed?
Read the explanation
Initialization supplies the object’s initial value as part of defining it. A later assignment needs a modifiable destination. These are different operations with different rules.
Primary references and further reading
- WG14 N1570 — public C11 committee draft: Sections 6.2.1–6.2.4, 6.3.2.1, 6.5.16, 6.7, 6.7.3, 6.7.9, and 6.9.2 provide the core rules used in this C17-baseline explanation. This is a public draft, not the final ISO publication.
- WG14 N3096 — public C23 draft: Constant-expression initialization and C23-specific declaration facilities.
Before you move on
- A declaration introduces information about a name; some declarations also define an entity.
- An initializer belongs to a definition and differs from a later assignment expression.
- Starting-value rules depend on the object’s storage duration and declaration context.
Keep these ideas separate
- Initialisation / Assignment
- Initialisation supplies an object’s starting value. Assignment requires a suitable modifiable destination.
Why is a const object allowed to receive an initial value even though later modification is restricted?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- Initialization supplies an initial value; assignment later stores a value in an existing modifiable object.
✓ Correct reasoning. Both can involve values, but they occur in different language contexts and can have different rules.
- Assignment creates a new variable name every time.
Assigning to an existing object does not introduce a new identifier.
- Every local declaration initializes an int to zero.
An ordinary automatic int without an initializer does not receive an implicit zero value.
C17
Ready for the next idea?
Mark this chapter as read whenever you feel ready.