C Tutorial
C Constants
const and #define name values that should not change. Use them instead of magic numbers.
A name that must stay put
A magic number is a bare 7 or 0.19 in the middle of a formula. A named constant tells the reader what that number is. C gives you two common tools: const on a variable, and#define in the preprocessor.
Use them for facts in this program: a maximum score, a tax rate, a conversion factor. If the value must change while the program runs, it is a variable, not a constant.
const variables
Write const before the type. Initialize it on the same line. The compiler rejects any later assignment. You may read it as often as you like.
Example
#include <stdio.h>
int main(void) {
const int max_score = 100;
int score = 88;
printf("%d / %d\n", score, max_score);
return 0;
}max_score = 50; after the declaration does not compile. That is the feature. If the number must change, drop const.
Run the listings in /c/try. gcc compiles them there. Do not use Python at /try, HTML at /html/try, or C++ at /cpp/try.
#define macros
#define is a preprocessor directive. It runs before the compiler. It pastes a token into the source wherever the name appears. There is no type and no semicolon.
Example
#include <stdio.h>
#define DAYS_IN_WEEK 7
#define GREETING "hello"
int main(void) {
int weeks = 3;
printf("%d\n", weeks * DAYS_IN_WEEK);
printf("%s\n", GREETING);
return 0;
}After the preprocessor, weeks * DAYS_IN_WEEK is weeks * 7. The name never exists as a typed object in the compiled program.
Which one to use
| const | #define | |
|---|---|---|
| Typed | Yes | No — text paste |
| Scope | Follows C rules | From the line onward, in this file |
| Debugger | A real object | Often just a number in the listing |
| Semicolon | Yes, on the declaration | No |
Prefer const for values that have a type. Use #define when you need a simple token substitution, or when older C code around you already does. A later chapter covers enum for sets of integer names.
One place to change
Array lengths and repeated limits belong in one named constant. Change that line, recompile, and every use updates. Copying 4 into a declaration and a loop bound is how those two copies drift apart.
Example
#include <stdio.h>
int main(void) {
const int n = 4;
int total = n * 10;
printf("slots %d, capacity %d\n", n, total);
return 0;
}Next: operators, which combine these values.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Physics
g and c
Standard gravity used in school problems is 9.81 m/s². The real field varies with latitude and height; you would not use 9.81 to put a satellite in orbit.
c is 2.998×10⁸ m/s in vacuum. const stops a later line overwriting either figure. A 70 kg person then “weighs” 687 N on Earth, which is mass times g, not mass itself.
W = m g
Weight is a force. Bathroom scales that say “70 kg” are reporting mass, not newtons.
Example
#include <stdio.h>
int main(void) {
const double g = 9.81;
const double c = 2.998e8;
double mass = 70.0;
printf("weight = %.1f N\n", mass * g);
printf("c = %.3e m/s\n", c);
return 0;
}Maths
π once
Circle area is πr². Write π in one place. If you paste 3.14 in four formulas and then decide you wanted 3.14159, you will miss one.
const double is the C way to name that fact for the rest of the function. A later chapter uses #define; the compiler cannot type-check a macro as easily.
A = π r²
Example
#include <stdio.h>
int main(void) {
const double PI = 3.14159;
double r = 2.5;
printf("area = %.4f\n", PI * r * r);
return 0;
}