C Tutorial
C Introduction
C is a compiled language. You write source, a compiler builds a program, then the program runs.
What is C?
C is a general-purpose language used for operating systems, embedded software, tools, and anything that must sit close to the machine. You choose types. You see when memory is used. A compiler turns a .cfile into a program before anything runs.
That compile step is the main difference from Python on StudyGrid. Python reads a file and executes it. C must build first. If the types do not match, gcc reports an error instead of running the program.
A first program
This is a complete C program. Copy it into the C editor and change the message.
Example
#include <stdio.h>
int main(void) {
printf("Hello, C\n");
return 0;
}Click Try it in C under the example. That opens /c/try — a gcc compile-and-run editor.
A second example: numbers
printf can print integers with %d. This program adds two values and prints the sum. Change a or b and compile again.
Example
#include <stdio.h>
int main(void) {
int a = 7;
int b = 5;
printf("%d + %d = %d\n", a, b, a + b);
return 0;
}What each line does
#include <stdio.h>brings in input and output (printf,scanf).int main(void)is where the program starts.voidmeans main takes no arguments.printfwrites text to the console.\nstarts a new line.return 0;tells the operating system the program finished normally.
Compiled, not interpreted
- You save a
.cfile. - A compiler such as gcc checks types and builds an executable.
- You run that program. It prints output or reads input.
On StudyGrid the C editor does steps 2 and 3 for you. Locally you would typegcc -std=c17 main.c -o main and then run ./main.
C vs Python vs HTML vs C++ on StudyGrid
| Python | HTML | C++ | C | |
|---|---|---|---|---|
| Dashboard | /tutorial | /html | /cpp | /c |
| Editor | /try | /html/try | /cpp/try | /c/try — gcc |
| Result | Output and charts | A rendered page | g++ stdout | gcc stdout |
| File | .py | .html | .cpp | .c |
Next: how to compile a file locally, then the rules of C syntax. Later chapters cover types, pointers, structs, files, and the standard library.
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
Current from an ammeter
Current is charge per second. The SI unit is the ampere: one coulomb of charge passing a point in one second. A first program does not have to compute that. It only has to print a reading a person already took.
Lab notebooks start the same way. Write the quantity, the number, and the unit on one line. If the unit is missing, the 0.45 is useless.
I = Q / t
Example
#include <stdio.h>
int main(void) {
printf("Current I = 0.45 A\n");
return 0;
}Maths
A 3-4-5 triangle
A right triangle whose legs are 3 and 4 has hypotenuse 5. That is Pythagoras, not a coincidence: 9 + 16 = 25. Printers and carpenters still use the triple to square a corner.
Printing the three numbers is a Hello World with a reason to exist. Later chapters compute the 5 from the 3 and the 4. Here you only name them.
a² + b² = c²
Example
#include <stdio.h>
int main(void) {
printf("Right triangle sides: 3, 4, 5\n");
return 0;
}