FirstHack Learn
Log in Sign up free

Introduction to C

9 min read · 60 views

Introduction to C

C was written in 1972 by Dennis Ritchie at Bell Labs, mostly so that the Unix operating system could be rewritten in something friendlier than assembly language. That origin still shapes the language today. C gives you direct access to memory and to the machine, and it adds very little on top that you did not explicitly ask for.

This is the reason C feels strict when you are learning it. The language is not being difficult on purpose. It simply refuses to guess what you meant.

Why colleges teach C first

There are four honest reasons, and none of them is "C is the best language".

  1. C is small. It has about 32 keywords. You can genuinely hold the entire language in your head by the end of a semester. Python and Java are friendlier to write but much larger to learn completely.
  2. Nothing is hidden. Memory, sizes of types, and the cost of every operation are visible. When you later learn that Java has a garbage collector, you will understand what it is collecting, because in C you were the collector.
  3. Your syllabus depends on it. Data structures, operating systems, computer networks, compiler design and embedded systems papers are almost always taught assuming C. Learning it now saves you from learning it later under exam pressure.
  4. The syntax transfers. C++, Java, C#, JavaScript, PHP and Go all borrowed C's braces, semicolons, for loops and if statements. The second language you learn will feel much easier than the first.

What C is not good at is also worth saying plainly. Nobody builds a website or a mobile app in plain C today. C is a good language for understanding computers, and a working language for systems that must be fast or small.

Compiled versus interpreted

Python runs through an interpreter: a program that reads your source code and carries out the instructions as it goes, every single time you run it. C uses a compiler: a program that translates your entire source file into machine code once, producing a separate executable file. After that, the executable runs on its own. The compiler is no longer involved.

Aspect C (compiled) Python (interpreted)
When translation happens Once, before running Every run, line by line
Output of translation A machine-code executable Nothing you keep
Typing mistakes found At compile time Only when that line runs
Typical speed Fast Slower
Portability of the output Rebuild for each OS/CPU Same script runs anywhere Python does

Behind the single gcc command you will type, four steps actually run: the preprocessor handles lines beginning with a hash, such as includes; the compiler turns C into assembly; the assembler turns assembly into an object file; and the linker joins your object file with library code such as printf to produce the final executable.

ℹ️Compile errors are free feedback

An interpreted language finds a typo only when execution reaches that line, which might be after ten minutes of running. C tells you before anything runs at all. A screen full of compiler errors is not failure. It is the fastest, cheapest review you will ever get.

Your first program

C
#include <stdio.h>

int main(void)
{
    printf("Hello, world\n");
    return 0;
}

Six lines, and every one of them is doing a job.

  • #include <stdio.h> is a preprocessor directive. Before compilation proper begins, the preprocessor pastes the contents of the standard input/output header file here. That header describes printf to the compiler, so the compiler knows what arguments it takes. Without this line the compiler does not know what printf is.
  • int main(void) declares the function where execution starts. Every C program has exactly one main. int says main hands an integer back to the operating system when it finishes. void in the brackets says main takes no arguments.
  • The braces { and } mark the beginning and end of the function body. Braces group statements throughout C.
  • printf("Hello, world\n"); calls the printing function. The text in double quotes is a string literal. The \n inside it is an escape sequence meaning newline. Without it, your output and your shell prompt end up on the same line.
  • return 0; gives 0 back to the operating system, which by convention means "finished successfully". Any non-zero value conventionally means something went wrong.
  • The semicolon ends each statement. C ignores line breaks and indentation entirely; the semicolon is what actually separates one instruction from the next.

A second program

Here is a slightly larger one that stores values and prints a calculation.

C
#include <stdio.h>

int main(void)
{
    int marks = 78;
    int total = 100;

    printf("Student marks: %d out of %d\n", marks, total);
    printf("Percentage: %d%%\n", (marks * 100) / total);
    printf("Line one\nLine two\n");

    return 0;
}

%d is a format specifier: a placeholder that printf replaces with the value of the matching argument, in order. To print an actual percent sign you write %%, because a lone % means "a placeholder follows".

How to compile and run it

On a Linux machine or WSL, save the file as hello.c and run these two commands in the terminal.

TEXT
gcc hello.c -o hello
./hello

The -o hello part names the output file. Without it, gcc produces a file called a.out, which is why that filename shows up in so many textbooks. On Windows with MinGW, the executable is hello.exe and you run it by typing hello.

Get into the habit of compiling with warnings switched on from day one:

TEXT
gcc -Wall -Wextra hello.c -o hello

Warnings are not errors. The program still compiles. But they point at code that is legal and probably wrong, which is exactly the code that costs you marks and hours.

Common mistakes

  • Missing semicolon. Writing printf("Hi\n") without the semicolon gives an error that often points to the next line, because the compiler only realises something is wrong when it reaches the following statement. When an error makes no sense, check the line above it.
  • Forgetting #include <stdio.h>. The program may still compile with a warning about an implicit declaration, then behave oddly. Include the header.
  • Using Printf or Main. C is case sensitive. printf, int and main must be lowercase.
  • Editing the source and running the old executable. Compilation is a separate step. If you change hello.c and immediately run ./hello, you are running yesterday's program. Recompile every time.
  • Forgetting \n. The output is not wrong, but it is jammed against the prompt. Add the newline.

You now know what C is, why it is on your syllabus, and how to get a program from a text file to a running process. Try compiling both programs above yourself, and then change the numbers and predict the output before you run it. When you want problems to solve rather than text to read, head to Practice.

Create a free account to track what you have finished.