Tsallaka zuwa abun ciki
Justice Tech School

Constants and Macros

const makes a variable read only. #define creates a text substitution macro, and enum creates integer constants. Prefer const and enum to macros when possible.

Example
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
const int MAX = 10;
enum { LOW = 1, HIGH = 5 };
printf("%.2f %d %d %d\n", PI, SQUARE(4), MAX, HIGH);

Result

3.14 16 10 5

Exercise

Define a macro.

# SIZE 10

Quiz Test

What does #define do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.