C Part I · Module 2 · Values, Variables, Types and Representation
Variables and Objects in C
Every program in Module 1 either printed fixed text or returned a fixed number. None of them held anything, worked anything out, or changed. This chapter is where that changes, and it starts by being careful about words. You will meet the idea of an object — a piece of storage, with a type, holding a value — and the idea of a variable, which is a name in your source code that refers to one. Those two things are commonly described as if they were the same, and treating them as the same is comfortable for about five modules and then becomes the single biggest obstacle to understanding pointers. Three short programs are enough to see the difference for yourself, and the last one makes it undeniable.
Foundation12 min readCPart IFundamentalsMental Model
Part I · Module 2 · Chapter 2.1 · Variables and Objects in C
1. The Thing Module 1 Could Not Do
A program that prints Hello, world! and stops is a complete program, but it has no memory of anything. It cannot count, compare, accumulate, or react to what it found a moment ago. Every program that does useful work needs somewhere to keep a value while it runs.
That is the gap this chapter fills, and the word for what fills it is object.
Be warned that C's use of that word has nothing to do with object-oriented programming. In C, an object is simply a piece of storage that can hold a value. That is the whole definition. It is a deliberately plain word for a deliberately plain thing.
2. Your First Object
Here is a complete program that creates one:
#include <stdio.h>
int main(void)
{
int count = 3;
printf("count is %d\n", count);
return 0;
}Output:
count is 3The new line is int count = 3;. It is worth taking apart completely, because four separate ideas are packed into eleven characters.
int is the type. Chapter 1.2 described a type as saying what kind of value you are dealing with, and from that following both how it is stored and what operations on it mean. Here it says: whole numbers. It also decides how much storage is needed, which is Chapter 2.2's subject, and it is why Chapters 2.5 through 2.12 exist.
count is the name. You chose it. The compiler does not care what it means; you and everyone who reads your code do.
= here means start out holding. This is initialization — giving an object a value at the moment it comes into existence. Chapter 2.3 is about why that moment matters more than it looks.
3 is the value it starts with.
; ends the declaration, exactly as it ends any statement in C.
Put back together: this brings into existence an object of type int, named count, holding the value 3.
3. What a Name Is Not
Now change the value:
#include <stdio.h>
int main(void)
{
int count = 3;
printf("count is %d\n", count);
count = 4;
printf("count is %d\n", count);
return 0;
}Output:
count is 3
count is 4The line count = 4; is assignment: it stores a new value in the object that count names. As Chapter 1.4 established, statements run in the order written, so the first printf sees 3 and the second sees 4.
Something important just happened, and it is easy to read past. The object did not change — its value did. There is still exactly one object, in the same storage, of the same type, named the same thing. What is different is what it holds.
That distinction is the whole point of this chapter, and here is the program that makes it impossible to ignore:
#include <stdio.h>
int main(void)
{
int count = 3;
int limit = count;
count = 9;
printf("count is %d, limit is %d\n", count, limit);
return 0;
}Before reading on, predict the output.
A common prediction is count is 9, limit is 9 — on the reasoning that limit was set to count, so they are linked. The actual output is:
count is 9, limit is 3int limit = count; did not tie the two names together. It read the value currently held by the object named count — which was 3 — and gave that value to a second, separate object named limit. Changing one afterwards has no effect on the other, because they were never the same object.
4. The Three Things an Object Has
Collecting what the three programs showed:
Two of those three are settled when the object comes into existence and never change: its type and its storage. Only the value varies.
That asymmetry explains something you would otherwise have to memorise. You can write count = 4; because 4 is a whole number and count holds whole numbers. Write count = "hello"; instead and your compiler is obliged to tell you about it: the type was decided at the declaration and is not up for renegotiation. C checks the declaration once and then holds you to it for the rest of the program.
5. Naming Is Part of the Work
The compiler accepts any name that follows its rules. It will not tell you a name is bad, because it cannot know.
The rules themselves are short: a name is made of letters, digits and underscores, must not start with a digit, and must not be one of C's own keywords — you cannot call something return. Names are case-sensitive, so count, Count and COUNT are three different names, and this catches people out more often than it should.
Beyond the rules, the useful guidance is short too. A name should say what the value means, not what type it is or how it is stored. count, limit, total_bytes and error_code all tell a reader something. x, n2, temp and data mostly do not.
This is not a style preference. In C you will spend a great deal of time reasoning about what is in an object at a given moment, and a name that states its meaning is doing part of that reasoning for you every time you read the line.
6. Declaration Is a Statement That Does Something
One last observation, because it prevents a common misreading.
int count = 3; is not an instruction to the compiler that sits outside the running program. It is a statement inside main, in sequence with the others, and reaching it is what causes the object to come into existence with that value. The statements above it have already run; the statements below it can use count; the statements above it cannot, because at that point the name did not yet exist.
That ordering is why this compiles:
int count = 3;
printf("count is %d\n", count);and why reversing the two lines does not. The compiler reads your file in order and refuses a name it has not been told about yet — the same refusal Chapter 1.5 showed you for a misspelled printf, arriving for the same reason.
7. What You Should Be Able to Say Now
- An object in C is a piece of storage that can hold a value. It has nothing to do with object-oriented programming.
- A variable is a name in your source code that refers to an object.
- An object has three aspects: a type and storage, both fixed when it comes into existence, and a value, which can change.
int count = 3;brings an object into existence with a starting value. That is initialization.count = 4;is assignment — it puts a new value into the existing object. The object itself does not change.int limit = count;copies the value out of one object into a second, separate object. The two are not linked afterwards.- A name exists from its declaration onward, not before.
8. Exercise
Exercise 1 — predict, then run. Write down the exact output before compiling:
#include <stdio.h>
int main(void)
{
int a = 2;
int b = a;
a = 7;
b = 1;
printf("a is %d, b is %d\n", a, b);
return 0;
}Exercise 2 — count the objects. In the program above, how many objects exist inside main? How many names? How many values were stored in total over the life of the program?
Exercise 3 — explain the failure. A beginner writes int limit = count; and expects limit to follow count for the rest of the program. Using §3, explain in two sentences what they have assumed and what actually happened.
Exercise 4 — repair it. This does not compile. Say why, and fix it by moving exactly one line:
#include <stdio.h>
int main(void)
{
printf("total is %d\n", total);
int total = 10;
return 0;
}Exercise 5 — judge the names. For each, say whether it is legal C and whether it is a good name: total_bytes, 2nd_value, Count, return, x, error_code.
9. Summary
- An object is storage that can hold a value. A variable is a name that refers to one. They are not the same thing, and keeping them apart is what makes Module 7 straightforward.
- Three aspects, two of them fixed. Type and storage are settled when the object comes into existence; only the value changes.
- Initialization gives an object its first value; assignment replaces the value in an object that already exists.
- Assignment copies a value, it does not link two names.
int limit = count;produces a second object, and changing one afterwards leaves the other alone. - A name says what a value means. The compiler will not help you here, and readers of your code will rely on it constantly.
- A name exists from its declaration onward. Using it earlier is an error for the same reason a misspelled function name is.
You now have an object that occupies storage — a phrase this chapter has used repeatedly without ever saying what storage actually is. That is the next chapter, and it is the one the rest of Part I leans on hardest: what memory is, what it means for an object to occupy part of it, and what an address is.
Standards & specifications
- Governing standard
- ISO/IEC 9899 (the C language standard)(opens ISO/IEC (JTC 1/SC 22/WG 14) in a new tab)
Defines the C language itself — the type system, expression semantics, object model, storage duration and the behaviour categories a conforming implementation must honour. It is a FAMILY of editions (C89/C90, C99, C11, C17, C23) and this curriculum names the edition wherever a claim depends on one. It does not define any foreign-function interface to a hardware description language, and it does not define platform behaviour: what a memory-mapped access does on real silicon is the implementation's and the device's contract, not this standard's. Working drafts published by WG14 are freely available and are what most engineers read day to day; a draft is not the published standard.
This page also covers RTL structure, verification approach and debugging technique. Those are engineering practice built on the standard, not requirements the standard itself imposes.
Where this fits
Part of the C Programming curriculum.
