Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 135 ยท Getting Started

Getting Started with C

Learn what the C programming language is, where it runs and why it still matters. Write, compile and run your first C program with printf in your browser.

C is a small, fast, compiled programming language that sits just one step above the machine. Dennis Ritchie created it at Bell Labs in the early 1970s to rewrite the Unix operating system, and more than fifty years later it's still one of the most used languages in the world. When software has to be fast, tiny, portable or close to the hardware, there's a very good chance it's written in C.

This course starts from zero. You don't need any programming experience, although knowing another language helps. Every lesson has live editors: you write real C code, press Run, and our server compiles it with GCC 15 and runs it a moment later. By the end you'll understand not just C, but how programs really use memory and the processor, which makes you a better programmer in every language.

Where C runs

C is everywhere, mostly in places you never see. A few examples:

  • Operating systems. The Linux kernel, the core of Windows, the lower layers of macOS and iOS, and Android's foundations are written mostly in C. So are the device drivers that talk to your disk, graphics card and Wi-Fi chip.
  • Embedded systems. Microcontrollers in washing machines, cars, pacemakers, thermostats, routers and toys usually run C. Many of these chips have only a few kilobytes of memory, and C fits.
  • Databases. SQLite (probably the most deployed database on Earth), PostgreSQL, Redis and the storage engines of many other databases are C.
  • Language runtimes. The standard Python interpreter (CPython), Ruby, PHP, Lua and Perl are all written in C. When you run a Python script, C code is doing the work underneath.
  • Tools and libraries. Git, curl, OpenSSL, zlib (compression), FFmpeg (video) and the C standard library itself. Countless programs written in other languages call these C libraries.
  • Scientific and high-performance code. Numeric libraries, simulations and parts of machine-learning frameworks rely on C for raw speed.

The reason is simple: C compiles to plain machine code, needs almost no runtime support, and has a compiler for practically every processor ever made. A C program can run on a supercomputer or on a chip that costs ten cents.

Why learn C?

  • You learn how computers actually work. C makes you think about memory addresses, bytes, the stack and the heap. Concepts that other languages hide become visible, and once you understand them, bugs in any language make more sense.
  • Speed and control. There's no garbage collector and no virtual machine. You decide exactly how much memory a program uses and when.
  • Portability. Standard C runs on nearly every platform. Code written carefully decades ago still compiles today.
  • It's small. The core language has only a few dozen keywords. You can learn all of it, which you can't say about most modern languages.
  • It's the common language of software. Operating system interfaces, libraries and other languages' "foreign function interfaces" speak C. Knowing it lets you read and connect to almost anything.
  • Careers. Embedded systems, operating systems, networking, security, game engines and databases all need people who are comfortable with C.

C also has sharp edges. It trusts you completely: it won't stop you from reading past the end of an array or using memory you already freed. This course points out every one of those traps, shows you the warning signs, and teaches the safe habits professionals use.

Note: C, C++ and C# are three different languages. C++ grew out of C and can use most C code, but modern C++ looks very different. If you're curious about how the two relate, see C and C++ in our C++ course.

Your first program

Here is a complete C program. Press Run and the output appears below the editor:

C Hello, C

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The first run can take a moment, because the code travels to our server, gets compiled into a real program, and then runs. Let's go through it line by line.

  • #include <stdio.h> tells the compiler to bring in the standard input/output header. A header is a file of declarations; this one declares printf and other functions for reading and writing text. C itself has no built-in way to print. Printing comes from the standard library.
  • int main(void) starts the main function. Every C program has exactly one main, and that's where execution begins. int means main gives a whole number back to the operating system when it finishes. void inside the parentheses means "this function takes no arguments".
  • The curly braces { } hold the body of the function: the statements it runs, in order.
  • printf("Hello, C!\n"); prints text. The text in double quotes is a string literal. The \n at the end is an escape sequence that means "new line". Without it, the cursor would stay at the end of the line.
  • return 0; ends main and reports success. By convention, 0 means "everything went fine" and any other value signals an error.
  • Every statement ends with a semicolon ;.

Tip: Since C99, reaching the closing brace of main without a return acts like return 0;. That rule applies only to main. In this course we still write return 0; because it makes the intent obvious and you'll see it in nearly every C program.

Printing several lines and numbers

A program runs its statements from top to bottom. Each printf call continues where the previous one stopped, so \n decides where lines break:

C Several lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The third and fourth calls print on the same line, because the first of them has no \n. The last two lines use format specifiers: %d is a placeholder for a whole number (a decimal int). The values after the text fill the placeholders from left to right, and C calculates 6 * 7 before printing. You'll meet all the specifiers in Printing Output with printf.

When something goes wrong

Mistakes are a normal part of programming, and the compiler is your first helper. Run this program, which is broken on purpose:

C A missing semicolon

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

GCC refuses to build it and prints something like:

TEXT
main.c: In function 'main':
main.c:4:39: error: expected ';' before 'return'

Read it piece by piece: the file (main.c), line 4, column 39, and what it expected (a ; before the word return). Add the semicolon after the closing parenthesis and run it again. When you see several errors, fix the first one and recompile; later errors are often just side effects of the first. The lesson on C Syntax shows you more errors and how to read them.

Running C in this course

Every editor on this site works the same way:

  1. You edit a single file called main.c.
  2. When you press Run, the code is sent to our server and compiled with GCC 15.2 using the C23 standard, with warnings switched on (-Wall -Wextra). The math library is linked too (-lm).
  3. If compilation succeeds, the program runs and its output appears below the editor. If it fails, you see the compiler's messages instead. Warnings are shown as well; treat them like errors, because they usually point at real bugs.
  4. Programs that read input get it from the Input box. You'll use it in Reading Input with scanf and fgets.

Each run is a fresh program: nothing carries over between runs, there's no internet access, and runs are time-limited, so an accidental infinite loop is simply stopped.

Here's a slightly bigger program that previews things you'll learn soon. Don't try to understand every line yet; run it, then change the numbers:

C A small taste of C

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

This program stores values in variables (language, year, scores), adds up an array of numbers with a for loop, and prints an average with two decimal places (%.2f). The text after // is a comment, a note for humans that the compiler ignores. Each of these ideas gets its own lesson.

A short history of C

Knowing a little history explains why C looks the way it does:

  • 1972: Dennis Ritchie develops C at Bell Labs, growing it from an earlier language called B. Unix is soon rewritten in C, which proves that an operating system can be written in a high-level language and moved to new hardware.
  • 1978: Brian Kernighan and Dennis Ritchie publish The C Programming Language. Its version of C becomes known as "K&R C".
  • 1989/1990: ANSI and then ISO standardize the language (C89/C90). From now on, C is defined by an official standard instead of one compiler.
  • 1999: C99 adds // comments, bool, long long, declarations anywhere in a block, and much more.
  • 2011 and 2018: C11 adds threads, atomics and _Generic; C17 fixes defects.
  • 2024: C23 is published, adding true/false and bool as keywords, nullptr, constexpr, typeof, binary literals and digit separators like 1'000'000.

This course compiles everything as C23, but it teaches the portable core that every modern compiler supports first, and labels each newer feature with the standard it needs. How C Works says more about the standards.

How this course is organized

  1. Getting Started: how C programs are built, syntax, comments, output and input.
  2. Variables, types and operators: storing data and calculating with it.
  3. Control flow and functions: making decisions, repeating work, organizing code.
  4. Arrays, strings and pointers: the heart of C, where you learn how memory really works.
  5. Memory management, structures and the preprocessor.
  6. Files and the standard library.
  7. Data structures, advanced C, projects and practice.

Every lesson ends with a short quiz. Don't skip the editors: change the examples, break them on purpose, read the messages, and fix them. That's the fastest way to learn C.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where does a C program start running?

    Every C program has exactly one main function, and execution starts there. #include lines are handled before compilation; they don't run.
  2. 2. What does printf("%d + %d = %d\n", 2, 3, 2 + 3); print?

    Each %d is replaced by the next value in order. C calculates 2 + 3 first, so the last placeholder becomes 5.
  3. 3. Two statements run one after another: printf("Hi"); and printf("There");. What appears?

    printf prints exactly what you give it. Without \n there is no line break, so the second call continues on the same line.
  4. 4. Why does the program need #include <stdio.h>?

    Printing is part of the standard library, not the core language. The <stdio.h> header declares printf so the compiler knows how to call it.
  5. 5. The compiler reports five errors. What's the best first step?

    Later errors are often caused by the first one. Fix the first error, recompile, and see what's left. A program with errors doesn't run at all.

Summary

  • C is a small, fast, compiled language created by Dennis Ritchie in the early 1970s to build Unix.
  • It powers operating systems, embedded devices, databases, language runtimes and core libraries.
  • Every program has one main function; int main(void) takes no arguments and returns an int (0 means success).
  • #include <stdio.h> makes printf available; \n starts a new line and %d prints a whole number.
  • Statements end with semicolons, and compiler messages give the file, line and column of each problem.
  • Our editors compile your code as C23 with GCC 15 and warnings on; treat warnings like errors.

Next: How C Works: Compiling and Linking, where you'll see what happens between pressing Run and seeing your output.