Ad space (banner)
๐Ÿ”งC Lessons
Lesson 19 / 68

Constants (#define and const)

This lesson covers declaring constants in C using #define and const, so you can safely handle values that shouldn't change. It's for anyone searching "C define const usage."

#define is a preprocessor directive that performs a text substitution before compilation, and const is a keyword that creates a variable that can't be changed. Both are used as a way to express a constant.

The example defines a perfect score with #define MAX_SCORE 100 and uses it in building a message. A name defined with #define is characteristically just replaced as plain text before compilation.

A common early mistake is not seeing the difference between #define and const. #define is a simple, typeless substitution, while const is treated as a typed variable โ€” a distinction worth choosing between based on your purpose.

In real projects, constants are a basic technique used constantly to avoid magic numbers and communicate a value's intent through its name.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)