Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Constants and sizeof

โญ beginnerโณ 9 min read๐Ÿ“ Lesson 6 of 225

Values that cannot change (const, #define) and asking the compiler how big things really are.

Constants

A const variable cannot be assigned after initialization:

const int MAX_USERS = 100;
MAX_USERS = 5;   // compile error

#define is a preprocessor text substitution โ€” different mechanism, same goal for simple constants:

#define MAX_USERS 100   // no semicolon, no equals

Prefer const for typed constants; you will meet the preprocessor properly in module 17.

sizeof asks the compiler

sizeof yields the size of a type or expression in bytes; its result type is size_t, printed with %zu:

printf("%zu\n", sizeof(int));      // typically 4
printf("%zu\n", sizeof(double));   // typically 8
char c = 'x';
printf("%zu\n", sizeof(c));        // 1

sizeof is evaluated at compile time for types โ€” no runtime cost.

โšก Now practice

Ready to Code
Size DetectiveUse sizeof and printf to report the real sizes and values on this platform.
1 challenge ยท ยท ~12 min