The C Programming Language
created in the 1970s (BCPL ๐ B ๐ C) by Dennis Ritchie๐
general-purpose, procedural (imperative) programming language
simple, small, relatively low-level
close to the hardware (von Neumann architecture), but still portable
even today used for system programming (OS kernels), embedded systems, device drivers, performance-critical applications
associated with the UNIX operating system (today Linux, OSX, BSD, etc.)
bootstrap language for many other languages (e.g. Python)
| Year | Standards |
|---|---|
| 1978 | K&R book, 1st ed. |
| 1983 | ANSI C, K&R 2nd ed. |
| โฆ | โฆ |
| 2011 | C11 |
| 2018 | C17 |
| 2024 | C23 |
The K&R book ("The C book")
A book on C programming, written by Brian Kernighan๐ and Dennis Ritchie.
It is a classic.
The program structure
C program: set of functions and variables
main()is the entry point, called by the OS
Data types
fundamental:
characters:
char(byte),signed/unsigned integers:
int(short, long)floating point numbers:
float,double
derived:
arrays
[..]pointers (memory addresses)
*structures
structunions
unionenumerations
enum(named integer constants)
special:
bit fields
Operations
over fundamental data types
arithmetic:
+,-,*,/,%logical:
&&,||,!bitwise:
&,|,^,~,<<,>>assignment:
=,+=,-=,*=,/=,%=,&=,|=,^=comparison:
==,!=,<,>,<=,>=
over derived data types
arrays: indexing
[i]pointers: dereferencing
*, pointer arithmetic+,-structures, unions, bit fields: member access
.pointers: dereferencing and member access
->
Expressions
arithmetic:
a + b,a * b,a / b,a % blogical:
a && b,a || b,!abitwise:
a & b,a | b,a ^ b,~a,a << b,a >> bassignment:
a = b,a += b,a -= b,a *= b,a /= b,a %= b,a &= b,a |= b,a ^= bconditional:
a ? b : cfunction calls
Statements
Statements can be:
simple, terminated by semicolon
;compound (block), enclosed in braces
{ }
Expressions are statements, but not all statements are expressions:
declaration:
int a;,float b;iteration (loop):
for,while,do ... whileconditional (selection):
if,switchjump:
break,continue,return,gotolabelled: label,
case,defaultempty (null):
;
Functions
declaration: return type, name, parameters
definition: return type, name, parameters, body
instead of procedures: functions with return type
void
Declarations are optional.
Other features
All additional features: input/output, memory management, multithreading โฆ must be implemented in a library. Standard libraries:
stdio.hstdlib.hstring.hmath.hpthread.h, etc.
Program
Two kinds of source files:
header files (
.h) for declarations,source files (
.c) for definitions.
Preprocessor and macros
Preprocessor is one of the most powerful features of C. It is purely textual, runs before the compiler and handles directives, macros, and conditional compilation.
Directives:
#include,#define,#ifdef,#ifndef,#else,#endif.Macros are used for text substitution, mostly for constants, inline functions, fragments of code, etc.:
#define PI 3.14159#define SQUARE(x) ((x) * (x))
Hello, World!
Hello, World! is the first program that is traditionally used as an introduction to a programming language. It prints "Hello, World!" to the console.
It is a kind of a tradition started by Brian Kernighan.