C Tutorial

C Header Files

A .h file shares declarations. Include guards stop a header from being pasted twice.

Declarations in a .h file

A header holds declarations other files need: a struct, a prototype, a typedef, a#define. A .c file holds definitions: function bodies, the actualint main(void). You include the header so every translation unit sees the same types.

StudyGrid /c/try is a single editor buffer. You cannot truly splitpoint.h and point.c there. The demos below keep the header text in the same file so gcc can still compile them.

Include guards

If a header is pasted twice, the compiler sees two copies of the same struct and errors. The usual fix is a guard: #ifndef POINT_H, then #define POINT_H, then the declarations, then#endif. The second include finds POINT_H already defined and skips the body.

Example

#include <stdio.h>

#ifndef POINT_H
#define POINT_H

struct Point {
  int x;
  int y;
};

#endif

#ifndef POINT_H
#define POINT_H

struct Point {
  int x;
  int y;
};

#endif

int main(void) {
  struct Point p;
  p.x = 3;
  p.y = 4;
  printf("(%d, %d)\n", p.x, p.y);
  return 0;
}

The second guarded block is a stand-in for including the same header twice. It compiles because the first block defined POINT_H. Without the guards, gcc would reject the duplicate struct Point.

Prototypes belong in the header

Put the function’s prototype in the header-shaped region. Put the body with main, the way a.c file would after #include "point.h".

Example

#include <stdio.h>

#ifndef POINT_H
#define POINT_H

struct Point {
  int x;
  int y;
};

int point_sum(struct Point p);

#endif

int point_sum(struct Point p) {
  return p.x + p.y;
}

int main(void) {
  struct Point p;
  p.x = 3;
  p.y = 4;
  printf("%d\n", point_sum(p));
  return 0;
}

In a real project the guarded block would live in point.h. This file would start with#include "point.h" and then define point_sum and main. The compiler still only needs the prototype before the call.

What goes where

In the .hIn the .c
struct layout, typedef, enumFunction bodies
Function prototypesint main(void)
Include guards#include "that.h"

Do not put a function definition in a header unless it is static inline and you know why. Two.c files that both include a header with a non-static function body will clash at link time.

One file in the browser

The C editor compiles one buffer. Copy a listing as-is. When you move to a local project, split the guarded declarations into point.h, keep the definitions in point.c, and compile withgcc -std=c17 point.c -o point.

Try the guarded listings at /c/try. That is gcc and C17 in one file.

Next: a shelf of complete programs you can compile as practice.

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

A shared energy prototype

In a real tree of files, kinetic.h would hold the prototype and kinetic.c the body. /c/try is one buffer, so the guard and the function sit together. 4 kg at 5 m/s is 50 J.

The include guard stops a second paste of the prototype. Two copies of a function body would be a linker error; two copies of a prototype are usually a compile error.

KE = ½ m v²

Example

#include <stdio.h>

#ifndef KINETIC_H
#define KINETIC_H
double kinetic_energy(double mass, double speed);
#endif

double kinetic_energy(double mass, double speed) {
  return 0.5 * mass * speed * speed;
}

int main(void) {
  printf("%.1f J\n", kinetic_energy(4.0, 5.0));
  return 0;
}

Maths

Circle area behind a guard

Unit circle, r = 1, area = π. The header pattern is the same as the energy example: declare, guard, define, call. Two copies of this idea in one chapter is deliberate. Headers are boring until they are missing.

Example

#include <stdio.h>

#ifndef CIRCLE_H
#define CIRCLE_H
double circle_area(double r);
#endif

double circle_area(double r) {
  return 3.14159 * r * r;
}

int main(void) {
  printf("%.3f\n", circle_area(1.0));
  return 0;
}

FAQ: C Header Files

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 header files 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 header files in this C C lesson (C Header Files).

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.