Part 2 covered how to compile and run C code on macOS. Now we step inside the program to see how values are stored. In C you do not just pick a variable name—you must also declare its type. The compiler needs that information to know how many bytes to reserve and how to interpret those bytes later.
New Terms In This Post
- Type: the rule that decides how many bytes a value uses and how to interpret them
- Initialization: giving a variable its first value at the moment of declaration
- Format code (format specifier): the symbol in
printfthat tells it how to format a value based on its type - Byte: the base unit for measuring memory size
Core Ideas
Study Notes
- Time required: 40–50 minutes
- Prerequisites: you have already run
clang hello.c -o hello- Goal: connect variable declarations, primitive types,
sizeof, and output formatting
Variables are labels that hold values. Types are the rules for how much space those values take and how to read that space. Since C requires the type up front, learn variables and types together.
We will focus on four questions:
- Why do we declare the type before storing a value?
- What do
int,char,float, anddoublerepresent? - How does
sizeofreveal type sizes? - How do you match
printfformat specifiers to each type?
Follow Along in Code
Declare and Initialize Variables
In C you typically write the type first, then the variable name.
#include <stdio.h>
int main(void) {
int age = 17;
float height = 172.4f;
char grade = 'A';
printf("age = %d\n", age);
printf("height = %.1f\n", height);
printf("grade = %c\n", grade);
return 0;
}
int, float, and char are types. Writing age = 17, height = 172.4f, grade = 'A' directly in the declaration is initialization.
Type information tells the compiler how many bytes to set aside. Think of types as a memory usage plan: char usually takes 1 byte, int often takes 4 bytes. Without the type, the compiler would not know how much space to allocate.
The Most Common Primitive Types
int: integer valueschar: a single characterfloat: floating-point numbers- double: floating-point numbers with higher precision
This sample prints all four in the same program.
#include <stdio.h>
int main(void) {
int student_count = 28;
char section = 'B';
float temperature = 23.5f;
double average_score = 91.78;
printf("students = %d\n", student_count);
printf("section = %c\n", section);
printf("temperature = %.1f\n", temperature);
printf("average = %.2f\n", average_score);
return 0;
}
At this stage it is enough to remember: integers -> int, single characters -> char, numbers with decimals -> float or double. double typically keeps more significant decimal digits and a wider range than float.
Check Memory Size with sizeof
C tightly couples types to memory size. Use sizeof to see how large each type or variable is, in bytes.
#include <stdio.h>
int main(void) {
printf("sizeof(char) = %zu\n", sizeof(char));
printf("sizeof(int) = %zu\n", sizeof(int));
printf("sizeof(float) = %zu\n", sizeof(float));
printf("sizeof(double) = %zu\n", sizeof(double));
return 0;
}
On a 64-bit Mac you usually see char = 1, int = 4, float = 4, double = 8. Aside from char, sizes can vary between systems, so measuring is safer than assuming. %zu is the go-to format specifier for sizeof results.
Match printf to Each Type
printf does not work with values alone. It also needs a format specifier so it knows how to interpret the bytes currently in memory.
#include <stdio.h>
int main(void) {
int year = 2026;
char level = 'A';
float rate = 4.5f;
printf("year = %d\n", year);
printf("level = %c\n", level);
printf("rate = %.1f\n", rate);
return 0;
}
Start with these matches:
%d:int%c:char%f:floatordouble
For floating-point values you can control precision such as %.1f or %.2f. In printf, float arguments are promoted to double, which is why %f works for both here. If you mismatch types and format codes, you will get warnings or nonsensical output, so always double-check the pairing.
Update Values After Declaration
Variables can change after they are declared.
#include <stdio.h>
int main(void) {
int score = 80;
printf("before = %d\n", score);
score = 95;
printf("after = %d\n", score);
return 0;
}
The name stays the same, but the value in memory changes. This idea becomes the basis for conditionals and loops, which react when values change.
Practical Example: Summarize Exam Results
#include <stdio.h>
int main(void) {
int score = 88;
double attendance_rate = 96.5;
char passed = 'Y';
printf("score = %d\n", score);
printf("attendance = %.1f%%\n", attendance_rate);
printf("passed = %c\n", passed);
return 0;
}
%% prints a literal percent sign. Small rules like this feel awkward at first, but running the code a few times helps them stick.
Why This Matters
You need a solid feel for variables and types before tackling conditionals and loops. Conditionals look at the current value to branch; loops rely on values changing. Keep four points from this post:
- C requires you to declare the type before creating a variable.
- Types dictate both the kind of value and how memory is interpreted.
sizeoflets you measure sizes directly.printfmust use the format specifier that matches the value’s type.
Once these become second nature, arrays and pointers make more sense because you already know why size and format matter.
Practice in CodeSandbox
The sandbox below uses CodeSandbox's Universal starter. For C, the key learning loop is still compile and run in the terminal, so recreate the lesson code as a source file and repeat that cycle directly.
💬 댓글
이 글에 대한 의견을 남겨주세요