class: center, middle ### COMP26020 Programming Languages and Paradigms -- Part 1 *** # Introduction to C ??? - Hello everyone. - In this video I will do a quick introduction to the C Programming language. - We will also learn how to write, compile and execute a few simple C programs. --- # C: Origin and Popularity .leftcol[ - C was designed in the 70s by Dennis Ritchie ] .rightcol[
]
.center[**Still very popular today!**] ??? - C is a relatively old programming language, it was developed in the 70s by Dennis Ritchie, the person on the right of the picture. - It is still today one of the most popular programming languages. - For example for more than 20 years it is consistently ranked first or second by the TIOBE index that measures the popularity of programming languages. --- # C: Characteristics .large[ - **Pros:** - Syntax - Low level - Fast - Controlled memory footprint ] ??? - Here are some high level characteristics about C. - In terms of pros, it has a very simple and extremely popular syntax. - Many newer languages such as Java or Go take much inspiration from the syntax of C. - C is also a Low level language that efficiently maps to machine code and integrates well with assembly. - It gives a high degree of control over the hardware, for example contrary to many languages C gives you the power to write at a specific virtual address in memory. - Because of its simplicity C is also very fast, and also allows programs to have a low memory footprint. -- .large[ - **Cons**: large area for making mistakes - Lack of memory safety, risks of undefined behaviour at runtime ] ??? - Now this all come at a relatively high cost which is a very large area to make mistakes, resulting from the high degree of control over the hardware. - The compiler will not catch every mistake. - And some errors may lead to undefined behaviour that is sometimes hard to debug. -- **Still extensively used** in: systems software, high performance computing, embedded systems, etc. ??? - Still, the benefits of may make that it is the default programming language in many domains, including systems software such as operating systems or virtual machine monitors, but also high performance computing, embedded systems, amongst other. --- # Popular Software Written in C
??? - A lot of extremely popular software is written in C. - You probably know at least some of these. - We have the Linux kernel, the most popular OS in HPC, cloud/servers, embedded systems - The Xen hypervisor, one of the most popular virtual machine monitors. - The GNU Libc standard library, used by most programs in many Linux distributions to interface with the OS. - We also have Apache, NGINX, and Redis, that are among the most popular web / key-value store servers. - Finally, git is by far the most popular version control system. --- # 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! ``` ??? - Let's see an example of a little C program. - This program simply writes hello world to the console, also named the standard output. - Before studying it more in details we can build and run it on the Linux command line as follows. - Type `gcc` followed by the name of the source file containing the code, here hello-world.c, then `-o`, followed by the name of the executable we wish to create, here hello. - Then we run this executable with `./hello`, and it prints out hello world. --- # Hello World ```c #include
int main() { printf("hello, world!\n"); return 0; } ``` ??? - Now let's have a look at the source code. -- - `#include` to get access to functions from external libraries ??? - The first line with pound include allows us to get access to function from external library. - We need the `printf` function from `stdio.h` to be able to print to the standard output. -- - `main` will run first when the program is executed ??? - Next we have a function named main. -- - It returns an integer (`int`) and takes no parameters ??? - In C functions are implemented as follows. - You have the return type, here int, followed by the function name, main. -- - Contains two statements, in C statements ends with `;` ??? - Then between braces we have the function body, containing the function's code. - This code is composed of two statements. - In C statements ends with a semicolon. -- - `printf` used to print text on the *standard output* (console) ??? - The first statement is a function call: the printf function is called with a parameter between parentheses. - `printf` is used to print something on the standard output, and that something is passed as a parameter. - It is a character string, delimited by double quotes. - Anti-slash n is a special character defining a carriage return. -- - `return` to go back to the calling context, and exit if returning from `main` ??? - Finally, the second statement uses the return keyword that returns from the current function to the calling context. - We return the integer value zero, a common return code indicating successful execution. - Because we return from main, this actually triggers the exit of the program with return code 0. --- # Compilation
```bash $ gcc hello-world.c -o hello ``` ??? - The compilation is the process of creating an executable program from the source code. - For this we use a tool named the compiler. - The C compiler we will use is named the GNU C compiler, GCC. - A compiler takes the source code as input, here it's `hello-world.c`. - It generates the machine code ready for execution on the CPU and puts it into an executable, here `hello`. - `.c` is a common file name extension for C source code. - Executables generally don't have a file name extension on Linux. - Concretely, we call the compiler like this on the command line. - `gcc`, followed by the name of the source file `hello-world.c`, followed by `-o` to indicate the name of the output program, `hello`. --- # Compilation ```c #include
*nt main() { printf("hello, world!\n"); return 0; } ``` ??? - The compiler will check and report some programming mistakes. - For example here the main function has a incomplete return type, the programmer forgot the i at the beginning of int. -- ```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 ``` ??? - The compiler catches the mistake and indicate that there is an error in file listing1.c, line 8, character 1: the name of the type is unknown. - For this particular issue it is even suggesting a fix. -- .center[**Multiple issues? fix them in the order they are reported**] ??? - An important thing to note is that when multiple issues are listed, it is important to fix them in the order they are reported, because mistakes down the list may result from the ones up the list. --- # 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; ^ ``` ??? - There are two levels of issues reported by the compiler. - On the one hand, errors, like the one we saw in the previous slide, make the compilation impossible and the compiler stops. - On the other hand, warnings may or may not be indicative of an issue and the compilation continues. - For example here we have an unused variable reported by the compiler, also it will not trigger a bug at runtime. -- - **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 ??? - Compilers are very advanced today and to my experience, warnings are almost always indicative of an issue and you should always fix them. --- # 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**] ??? - A last thing I want to talk about in this videos are comments. - These are annotations in the source file that will not be processed by the compiler. - It is important to comment your code a bit to explain what it does, for other people to understand your code easily, and also for yourself to get back into pieces of code you wrote in the past long time ago. - There are two comments style supported in C. - Everything that is between slash star star slash delimiters is commented. - You can also use the double slash to comment the rest of the line. --- # Summary - Brief intro to C - Hello world - Compilation process - Comments ---- .center[Feedback form: https://bit.ly/3yzIVls]
??? - So let's recap. - In this video we have seen a brief introduction to the C programming language. - We looked at a simple hello world program and described how to compile C sources into executable programs. - Finally, we looked at comments. - In the next video we will study variables, types, and how to print more stuff on the console.