class: center, middle ### COMP26020 Programming Languages and Paradigms Part 1: C Programming *** # Introduction to C --- # C: Origin - C was designed in the 70s by Dennis Ritchie at Bell Labs - Originally used to develop applications for Unix, then to reimplement its OS kernel with Ken Thompson -- - Ritchie & Thompson 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 .leftsmallcol[ - Old language, but still **omnipresent** today - In the top ten of most programming languages popularity indexes ] -- .rightlargecol[
] --- # C: Characteristics - **Pros:** - *Low-level*: close to the hardware, lots of freedom for the programmer to manipulate the machine - Fast, low memory footprint - Portable - Established a popular syntax -- - **Cons**: programmer freedom comes at a cost: **a large area for making mistakes** - Lack of memory safety, risks of undefined behaviour at runtime -- **Still extensively used** in: systems software, high performance computing, embedded systems, etc. --- # Popular Software Written in C
--- # Programming Mistakes in C .leftcol[ - Programming mistakes in C are common and lead to: - Build failing π - Crashes π - Program misbehaving π - Hard to reproduce bugs π‘ - Security vulnerabilities π± - C is not particularly recommended for new projects - Still a gigantic amount of legacy C code running today that not going anywhere anytime soon ] .rightcol[
] --- # Why Learn C Today? - Still the default programming languages in many different domains - C is not going anywhere anytime soon, vast amount of C codebases still relevant today - We will need C experts in the decades to come - Proximity to the hardware: knowing C helps understanding how a computer works - Knowing C's syntax will make it easy to lean many other programming languages --- # Hello World ```c #include
int main() { printf("hello, world!\n"); return 0; } ``` .codelink[
`03-c-introduction/hello-world.c`
] Assuming the code is in `hello-world.c`, to compile and run on the Linux command line: ```sh $ gcc hello-world.c -o hello $ ./hello hello, world! ``` --- # Hello World ```c #include
int main() { printf("hello, world!\n"); return 0; } ``` -- - `#include` to get access to functions from external libraries -- - `main` will run first when the program is executed -- - It returns an integer (`int`) and takes no parameters -- - Contains two statements, in C statements ends with `;` -- - `printf` used to print text on the *standard output* (console) -- - `return` to go back to the calling context, and exit if returning from `main` --- # Compilation
```bash $ gcc hello-world.c -o hello ``` --- # Compilation ```c #include
*nt main() { printf("hello, world!\n"); return 0; } ``` -- ```sh $ 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 issues? fix them in the order they are reported**] --- # Compilation: Warning and Errors - Errors are unrecoverable - Warnings *may* indicate a problem ```c #include
int main() { int x; printf("hello, world!\n"); return 0; } ``` ```sh 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: Warning and Errors - Errors are unrecoverable - Warnings *may* indicate a problem - **Warnings almost always indicate a problem and
you should fix them
** - Some warnings (picky ones) are disabled by default, enable them with the `-Wall -pedantic` flags --- # Comments ```c /* listing2.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 your 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]