C Tutorial

C Get Started

Install a compiler locally, or use the StudyGrid C editor. Either way you need a .c file and a main function.

Two ways to run C

You can compile in the browser on StudyGrid, or install a compiler on your computer. The language is the same. The editor is faster for lessons. A local compiler is what you use for real projects.

The StudyGrid compiler is /c/try. Python stays at /try. HTML stays at/html/try. C++ stays at /cpp/try. C examples never open those three.

Use the StudyGrid editor

  1. Open a chapter and copy an example, or go straight to Try C.
  2. Press Compile & run (or Ctrl + Enter).
  3. Read the build log. If gcc is unhappy, the program does not run.
  4. Read program output underneath. That is what printf printed.

If the program uses scanf, type values in the stdin box on the left, then compile again. The editor compiles as C17 with gcc.

Install a compiler (optional)

SystemTypical setup
WindowsMSYS2 or MinGW-w64. You want gcc on PATH.
macOSXcode Command Line Tools: xcode-select --install. Apple Clang compiles C.
LinuxInstall gcc from the package manager, for example sudo apt install gcc.

Check that it works:

Example

gcc --version

Compile a file locally

Save this as main.c:

Example

#include <stdio.h>

int main(void) {
  printf("Compiled locally\n");
  return 0;
}

Then in a terminal in that folder:

Example

gcc -std=c17 main.c -o main
./main

On Windows with MinGW the run command is often main.exe. -std=c17 asks for C17, which is what the StudyGrid editor uses.

What you need in every file

  • A .c name, not .txt.
  • Headers you actually use, usually #include <stdio.h> for this tutorial.
  • Exactly one main function in the program you run. Write int main(void).
  • A semicolon at the end of each statement.

Example

#include <stdio.h>

int main(void) {
  int n = 3;
  printf("%d\n", n);
  return 0;
}

If gcc prints a wall of errors, start at the first error. Later messages are often fallout from that first missing semicolon or typo.

Download from a lesson

Every example has Download .c. Save the file, compile it with gcc, and compare the result with the browser editor. Next: the syntax rules the compiler enforces.

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.

Chemistry

Label before you weigh

Copper(II) sulfate pentahydrate is CuSO4·5H2O. The dots in the formula are water of crystallisation, not a product. If two bottles sit on the bench, the label is the only thing that stops you using the anhydrous salt by mistake.

A C file starts in main. The first useful line is often that label, printed so the rest of the session has a name.

Example

#include <stdio.h>

int main(void) {
  printf("Sample: CuSO4.5H2O\n");
  printf("Ready to weigh.\n");
  return 0;
}

Engineering

A board saying it booted

Embedded work is still C. After reset, the chip runs from a known address. The first human-readable line on a serial port is how you tell a dead board from a live one.

Nothing is measured yet. The point of the listing is the compile-run loop: a .c file, a main, stdout.

Example

#include <stdio.h>

int main(void) {
  printf("logger v1: ok\n");
  return 0;
}

FAQ: C Get Started

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 get started 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 get started in this C C lesson (C Get Started).

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.