YOUR C LEARNING COMPANION

Find your next idea.

Learning path
C foundationsChapter 7 of 73

Variables, values, and types

Give meaning to data: understand the difference between an object, its name, its type, and its current value before moving into declaration, initialization, assignment, and conversion.

12 min readFoundationsC17 baseline
Offline reading
In this chapter14 sections
0% through this chapter

Look at int apples = 4;. It gives us a name, a type, and a starting value. We will unpack these ideas one at a time, then see what changes when the program stores a new value.

1. Data needs an interpretation

The same stored pattern can mean different things under different rules. It might represent a number, a character, or a colour component. A representation is how information is encoded. A program needs rules for interpreting it and for deciding which operations make sense.

A value is information used by the program, such as the integer four. The source spelling 4 lets us write that value. The stored representation is how an implementation encodes it. Think separately about the value, its written spelling, and its storage.

A type gives values some of their rules. It helps determine which values fit and which operations and conversions apply. For example, dividing two integers uses different rules from floating-point division. We will trace that difference below.

This is not a promise that every possible bit pattern is a valid value of every type. Nor does C give every type a universal size or representation. Those boundaries become important as we go deeper.

2. Object, name, and value are different

C uses the word object for a region of data storage whose contents can represent values. In the example below, apples names an integer object with starting value four. A name in source code is an identifier. In everyday C discussion, a named object is often called a variable.

C17 · illustrative fragment
int apples = 4;
Name
apples
Declared type
int
Object
The integer object introduced by this definition
Initial value
4
This is a conceptual relationship, not a diagram of required byte positions in RAM.

The name is not the value. If the object later contains six, its name can still be apples. The type is not the current value either: both four and six are representable by int.

The storage analogy is useful: imagine a place for a value, with a label and rules about what may be held there. Its limit is that a compiler need not create one visible physical memory box for every variable. It can use registers, combine work, or eliminate unnecessary storage while preserving the behavior C requires.

Explore with a visual model

See a value get copied

VISUAL MODEL

Change a starting value, then follow three lines. Watch the two objects separately.

Assume these declarations are inside a function.

  1. int apples = 4;
  2. int saved = apples;
  3. apples = 6;
Object · intapplesInitializer not reached
Object · intsavedInitializer not reached

Neither initializer has been reached. No starting value is shown for apples or saved in this trace yet.

The diagram shows logical objects and values. Physical storage and optimisation depend on the implementation.

Read the complete trace
  1. Neither initializer has been reached. No starting value is shown for apples or saved in this trace yet.
  2. The initializer gives apples the value 4. Its name and type stay the same when a later assignment changes its value.
  3. The initializer gives saved a copy of 4. The two integer objects hold separate values.
  4. apples now holds 6. saved still holds 4, the value copied earlier.
Go deeper: can an object have no name?

Yes. Objects obtained through allocation and objects created by some expressions need not have a variable name. Later, pointers give us ways to access objects through addresses. A function name is an identifier too, but a function is not an object in C.

3. Declarations introduce information

A declaration introduces a name and information about it. A definition also defines the entity. For an ordinary object, its definition causes storage to be reserved; for a function, it supplies the body. Start with local integer objects here. The multi-file distinction will become useful later.

C17 · illustrative fragment
int apples;

For an ordinary local object inside a function, this is both a declaration and a definition. It introduces the name apples with type int. It does not explicitly give the object an initial value. Introducing a name and choosing an initial value are separate decisions.

A semicolon completes this declaration. Whitespace helps readers see its parts, but the compiler uses C's grammatical rules to recognize those parts. Do not read it as an instruction to display a number or ask someone for input; neither happens here.

Go deeper: not every declaration is a definition

extern int total; commonly declares an object defined elsewhere. A shared header can contain this declaration while one source file supplies the definition. More detailed linkage rules come later; for now, the key idea is that “the name is known here” and “the object is defined here” can differ.

4. Initialization gives an object its starting value

C17 · illustrative fragment
int apples = 4;

Here 4 is the initializer: it supplies the object’s starting value. For the ordinary local variable shown here, this happens when execution reaches the declaration. The = introduces an initializer in this form. Later, the same symbol can appear in an assignment expression.

If you leave this ordinary automatic local int without an initializer, it has an indeterminate value. That is a technical term, not a usable mystery number. Supply a valid value before reading it. Seeing zero in one run does not establish a starting-value rule.

C17 · illustrative fragment
int attempts;       /* no usable initial value supplied here */
attempts = 0;        /* now a value has been assigned */

This warning is about these automatic local objects. Objects with static storage duration follow initialization rules that provide zero values when no explicit initializer is given. C has several storage durations; “an uninitialized variable is always zero” and “it is always random garbage” are both poor universal rules.

A useful habit is to choose the starting value deliberately. Zero is appropriate for a counter that has counted nothing. It may be misleading for a measurement whose real state is “not available yet.” Initialization gives a value; the program still decides what that value means.

5. Assignment changes a stored value

C17 · illustrative fragment
int apples = 4;
int saved = apples;
apples = 6;

There are two objects here. Initializing saved reads the current value of apples and uses it to initialize a different object. After the final line, apples contains six and saved still contains four. The second name does not create a permanent connection between their future values.

An assignment computes a value on the right and stores it in a suitable destination on the left. An ordinary non-const integer object is a familiar destination. The formal term is modifiable lvalue: an expression designating an object that the assignment is allowed to change. We will study its full rules later.

The = symbol does not assert mathematical equality. It performs assignment in this expression. To ask whether two values compare equal, C uses ==. A statement such as apples = apples + 1; reads the old value, computes a new value, and stores it. For signed integers, that computation must remain within the type's representable range.

6. A type is a contract, not just a size

int, char, float, and double are different types, not simply different container sizes. Types determine ranges, precision, permitted operations, and conversion rules. Choose according to the meaning and requirements of the data, not merely whether one example fits.

ExampleWhat it expresses
int count = 4;An integer quantity, with no fractional part
double length = 2.5;A floating-point quantity
char grade = 'A';A character value represented using the execution character set

A C byte is the unit measured by sizeof, and sizeof(char) is always one. CHAR_BIT from <limits.h> tells you how many bits a byte contains; it is at least eight. The standard guarantees minimum integer ranges rather than making every int four bytes. Use the implementation's limits when exact ranges matter.

Floating-point types represent a finite set of values too. They are useful for approximate real-number calculations, but cannot represent every decimal fraction exactly. More bytes do not automatically mean “correct for every purpose.”

7. Values can be converted

When a value is used where another type is required, C may convert it. Conversion changes the value's type for that use; it does not change the declared type of the original object.

C17 · illustrative fragment
int count = 3;
double measured = count;  /* measured starts with 3.0 */
int whole = 3.9;          /* whole starts with 3 */

In the last line, conversion to int discards the fractional part toward zero, provided the resulting integer is representable. This is not rounding to the nearest integer. Out-of-range floating-to-integer conversions need care; a cast does not make an invalid conversion safe.

C17 · illustrative fragment
double first = 7 / 2;          /* integer division first: 3.0 stored */
double second = (double)7 / 2; /* floating division: 3.5 stored */

In 7 / 2, both operands are integers. C first produces the integer result three, then converts it to double for storage. In the second line, the cast converts an operand before division, so floating-point division produces 3.5. Read the operation first, then the destination conversion.

Go deeper: conversions can surprise you

Integer promotions and the usual arithmetic conversions determine many mixed-type operations. Signed and unsigned operands are especially worth checking: in C17, -1 < 1u is false because the signed value is converted to unsigned int for this comparison. This deserves its own detailed lesson, rather than the rule “the bigger-looking type always wins.”

8. Read-only access is not the same as a compile-time constant

C17 · illustrative fragment
const int maximum_attempts = 3;

const qualifies the type. An expression referring to this object cannot be used as a modifiable destination. Attempting to change an object originally defined as const, even by casting away that qualification, has undefined behavior.

There is another distinction: reading a mutable object through a const-qualified access path does not freeze all other valid ways to modify that object. We will study that case with pointers. For this declaration, however, the object itself was defined as const.

In C17, an ordinary const int object is not automatically an integer constant expression, a category required in some language constructs. The everyday word “constant” can hide several different rules. An enumeration constant is one way to give an integer constant expression a name in C17.

9. A name's visibility and an object's lifetime differ

Scope describes where an identifier can be used in source code. Lifetime describes the period during execution when its object exists. One is about visibility in the program text; the other is about an object during execution.

For the ordinary automatic local objects in this chapter, execution leaving the enclosing block ends their lifetime. A different entry to that block creates a new instance. Other storage durations behave differently: a static local object can outlive the block's individual executions.

These rules do not require a particular physical stack layout. The object-and-value model is the language model we reason with; implementation machinery must respect the required behavior, but can organize the work differently.

10. Connect the ideas

When you meet a declaration, ask what is being introduced, what type it has, whether a value is initialized, where the name is visible, and how long the object exists. When you meet an assignment, ask which object changes and which conversions occur.

A small thought check
C17 · illustrative fragment
int cups = 4;
int remembered = cups;
cups = 6;
double half = 5 / 2;

What values do remembered and half have after these statements? Explain the rules, rather than guessing an output.

Read the explanation

remembered is four: it received a copy of the earlier integer value, not a connection to future assignments. half is 2.0: both division operands are integers, so integer division produces two before conversion to double. The destination's type does not change the earlier operation.

You now have a vocabulary for asking better questions: a name refers to an object, the object has a type and lifetime, and its contents represent a value. Declarations, initialization, assignment, and conversion describe different parts of that story.

Primary references for deeper checking

Before you move on

  • A name, an object, its type, and its current value are different ideas.
  • Initialisation supplies a starting value; later assignment stores another suitable value.
  • Copying an integer value creates no permanent connection between the objects’ later values.
Keep these ideas separate
Value / Representation
A value is the information; its representation is how an implementation encodes that information.
Explain it in your own words

After copying apples into saved, why can changing apples leave saved unchanged?

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.

After int saved = apples;, a later assignment changes apples. What happens to saved?
Read the explanation for every choice
  1. saved automatically follows apples.

    The initializer copied an int value; it did not create a live connection between the objects.

  2. saved keeps its copied value until it is changed separately.

    ✓ Correct reasoning. The objects are separate. Changing one does not itself assign to the other.

  3. saved ceases to exist.

    An assignment to apples does not end saved’s lifetime.

C17

Ready for the next idea?

Mark this chapter as read whenever you feel ready.

CHAPTER 07 / 73

In this chapter

Variables, values, and types

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping