What C is, installing the compiler and your first program
C was born in the 1970s to write the Unix system, and today it still lives inside almost everything: operating systems, databases, microcontrollers, games and the interpreters of other languages. It is a small, fast language very close to the machine. That closeness is its big advantage and also its danger: C trusts you and does not protect you from your own mistakes. In this course you will learn to use it carefully, with the C17 standard (and notes about C23 when needed).
Compiling: from text to program
A C program is a text file with the .c extension. The computer cannot understand it directly; you need a compiler that translates it into machine code and produces an executable. The most used compilers are gcc and clang. Every time you change the text you must compile again. This cycle of write, compile, run and fix will be your routine throughout the course.
Installing the compiler
- Linux (Debian/Ubuntu): sudo apt install build-essential
- macOS: xcode-select --install (installs clang)
- Windows: install MSYS2 and run pacman -S mingw-w64-ucrt-x86_64-gcc, or use WSL with Ubuntu
- Check the installation with: gcc --version
You also need a simple text editor; VS Code, Vim or Notepad++ will do. You do not need a heavy IDE: with an editor and a terminal you have everything and you understand better what is happening.
Your first program
#include <stdio.h>
int main(void) {
printf("Hello, C!\n");
return 0;
}Save this as hello.c. The line #include <stdio.h> asks the preprocessor to bring in the declarations of standard input and output, where printf lives. Every program starts in the main function. The word int says that main returns an integer to the operating system: 0 means everything went well. The braces delimit the body of the function, each statement ends with a semicolon, and the sequence \n at the end of the text is a line break.
Compile and run
gcc -std=c17 -Wall -Wextra -o hello hello.c
./helloThe option -std=c17 fixes the standard, -Wall and -Wextra enable useful warnings and -o hello names the executable (on Windows it will be hello.exe and you launch it with .\hello). If all goes well you will see Hello, C! on screen. If the compiler prints errors, read the first one: it gives the file and line number. The following errors are usually consequences of the first. Typical beginner failures are forgetting a semicolon, misspelling printf, or not closing a brace.
Comments
#include <stdio.h>
/* A block comment can
span several lines. */
int main(void) {
// A line comment ends at the end of the line.
printf("Comments are ignored by the compiler.\n");
return 0;
}Comments are not executed: they explain why a decision was made, not repeat what the code already says. Single-line comments with // exist since C99; block comments /* */ cannot be nested.
Проверьте себя
Вопрос 1 из 2What does the compiler do?
✎ Мои заметки
Сохранено только в этом браузере. Войдите, чтобы хранить в аккаунте.0/5000 символов