How to use variables

Declare with a type

Every variable has a type known at compile time. Initialize it before its value is read.

int students = 12;
double price = 4.50;
char grade = 'A';

Choose useful names

Names may contain letters, digits, and underscores but cannot begin with a digit. C is case-sensitive; concise, descriptive lowercase names are common.

Update a value

Assignment replaces a stored value. Compound assignment performs an operation and stores the result.

int score = 10;
score = score + 5;
score += 2;
printf("%d\n", score);

Use const for read-only values

const prevents modification through that name and communicates intent. It does not automatically make a compile-time constant in every C context.

const double tax_rate = 0.16;
double total = 100.0 * (1.0 + tax_rate);