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
- Open a chapter and copy an example, or go straight to Try C.
- Press Compile & run (or Ctrl + Enter).
- Read the build log. If gcc is unhappy, the program does not run.
- Read program output underneath. That is what
printfprinted.
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)
| System | Typical setup |
|---|---|
| Windows | MSYS2 or MinGW-w64. You want gcc on PATH. |
| macOS | Xcode Command Line Tools: xcode-select --install. Apple Clang compiles C. |
| Linux | Install gcc from the package manager, for example sudo apt install gcc. |
Check that it works:
Example
gcc --versionCompile 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
./mainOn 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
.cname, not.txt. - Headers you actually use, usually
#include <stdio.h>for this tutorial. - Exactly one
mainfunction in the program you run. Writeint 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;
}