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
TypedYesNo — text paste
ScopeFollows C rulesFrom the line onward, in this file
DebuggerA real objectOften just a number in the listing
SemicolonYes, on the declarationNo

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;
}

FAQ: C Constants

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c constants examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c constants in this C C lesson (C Constants).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.