Thuta Learning
BasicProgrammingbeginner

Constants

Relax. We'll talk through this in plain words — no textbook voice.

For values you don't want changing while the program runs, use const. Think PI, tax rate, maximum score, minutes per hour, and the like.

c
#include <stdio.h>

int main() {
  const int MINUTES_PER_HOUR = 60;
  const double PI = 3.14159;

  printf("Minutes: %d
", MINUTES_PER_HOUR);
  printf("PI: %.2lf", PI);
  return 0;
}

const means that variable can never be assigned a new value again. If you try, the compiler will throw an error.

You should see
Minutes: 60 PI: 3.14

Info

Writing constant names in uppercase is a common naming style that keeps them easy to spot in a project.

Easy traps

  • Avoid writing const int LIMIT; without a value and setting it later. It's cleaner to assign the value right when you declare the constant.