class: center, middle ### COMP26020 Programming Languages and Paradigms Part 1: C Programming *** # Introduction to C --- # C: Origin - C was designed in the early 1970s by Dennis Ritchie at Bell Labs - Developed alongside Unix: first used for Unix utilities, then to rewrite the Unix kernel with Ken Thompson -- - Ritchie & Thompson were awarded the ACM Turing Award in 1983 *for their development of generic operating systems theory and specifically for the implementation of the UNIX operating system*
--- # C: Popularity - Old language, but still **omnipresent** today - In the top ten of most programming language popularity indexes
--- # C: Characteristics - **Pros:** - *Low-level*: close to the hardware, lots of freedom for the programmer to control the machine - Fast - Low memory footprint - Portable - Its syntax has influenced many later languages -- - **Cons**: the programmer's freedom comes at a cost: **plenty of room for mistakes** - Lack of memory safety, risk of undefined behaviour at runtime -- **Still extensively used** in: systems software, embedded systems, high-performance computing, etc. --- # Popular Software Written in C
--- # Programming Mistakes in C .leftcol[ - Programming mistakes in C are common and lead to: - Build failures π - Crashes π - Incorrect program behaviour π - Hard-to-reproduce bugs π‘ - Security vulnerabilities π± - C is generally not recommended for new projects - Yet a vast amount of legacy C code is still running today, and it is not going anywhere soon ] -- .rightcol[
] --- # Why Learn C Today? - Still the default programming language in many domains - C is not going anywhere soon: vast C codebases are still relevant today - C experts will be needed for decades to come - Proximity to the hardware: knowing C helps in understanding how a computer works - Knowing C's syntax makes many other programming languages easier to learn --- # Hello World ```c #include
int main() { printf("hello, world!\n"); return 0; } ``` .codelink[
`03-c-introduction/hello-world.c`
] - C is a **compiled language**: the source code must be translated into an executable program - Assuming the code is in `hello-world.c`, to compile and run it on the Linux command line: ```bash $ gcc hello-world.c -o hello $ ./hello hello, world! ``` --- # Hello World ```c #include
int main() { printf("hello, world!\n"); return 0; } ``` .codelink[
`03-c-introduction/hello-world.c`
] - `#include` to get access to functions from external libraries -- - Program execution starts in the `main` function -- - It returns an integer (`int`) and takes no parameters -- - Contains two statements: in C, statements end with `;` -- - `printf` prints text to the *standard output* (console) -- - `return` goes back to the calling function; returning from `main` ends the program (`0` indicates success) --- # Compilation
```bash $ gcc hello-world.c -o hello ``` --- # Compilation ```c #include
*nt main() { printf("hello, world!\n"); return 0; } ``` -- ```bash $ gcc hello-world.c -o hello hello-world.c:3:1: error: unknown type name βntβ; did you mean βintβ? 3 | nt main() { | ^~ | int ``` -- .center[**Multiple errors?
Fix them in the order they are reported
**] --- # Compilation: Warnings and Errors - Errors are fatal: compilation fails and no executable is produced - Warnings do not stop compilation, but *may* indicate a problem ```c #include
int main() { int x; printf("hello, world!\n"); return 0; } ``` ```bash $ gcc -Wall hello-world.c -o hello hello-world.c: In function βmainβ: hello-world.c:4:9: warning: unused variable βxβ [-Wunused-variable] int x; ^ ``` --- # Compilation: Warnings and Errors - Errors are fatal: compilation fails and no executable is produced - Warnings do not stop compilation, but *may* indicate a problem - **In practice, warnings almost always indicate a problem:
fix them
** - Many warnings are disabled by default; enable them with the following GCC flags: - `-Wall`: enables most common warnings, recommended - `-pedantic`: warns about non-standard C, sometimes too picky --- # Comments ```c /* comments.c, illustrate the use of comments */ #include
// necessary to get access to printf /* this function simply prints out 'hello world' and returns */ int main() { printf("hello, world!\n"); // here we print ... /* the line below will not be compiled: */ // printf("goodbye!\n"); return 0; /* ... and return */ } ``` .codelink[
`03-c-introduction/comments.c`
] ```c /* style 1 */ // style 2 ``` .center[**
Use comments
to explain what the code does:
uncommented code is hard to understand**] --- # Summary - Brief intro to C - Hello world - Compilation process - Comments ---- .center[Feedback form: https://bit.ly/3yzIVls]