This page provides content about Programming with C and version control with Git. This is the materiel used during the training week of M1 Mosig.
Lecture Slides
Programming with C
The lectures are based on the lecture slides given in CS107: Computer Organization & Systems from Standford University.
More specifically, our focus is on the following lectures:
- Lecture 1: Introduction
- Lecture 4: Chars and C-Strings
- Lecture 5: From C Strings to Pointers
- Lecture 6: More Pointers and Arrays
We encourage you to also read the other lectures from that course.
Note that the slides prepared by Gregory MouniƩ are also accessible here
Version control with Git
TBD
Practical exercises
A first exercise about compiling C programs
Courtesy of Martin Quinson
Here is a first C program:
#include <stdio.h>
int main(int argc, char** argv) {
int answer; 3
for (int i=0; i<5; i++) {
printf("What is the answer?\n");
scanf("%d", &answer);
if (answer == 42) {
printf("Correct answer!\n");
exit(0); // Ends the program
} else {
printf("No, the answer is not %d\n", answer);
}
}
printf("Sorry, you lost\n");
return 1;
}
- Step 1: Copy and understand the provided source code
- To write programs in C, you could use advanced editors like Visual Studio. To start learning, we suggest you to use a simpler editor like kate, geany, vim, neovim, emacs, etc.
- Step 2: Compile and execute your program
$ gcc game42.c -Wall -Wextra -Werror -o game42
[Potential error messages]
$ ./game42
[The program starts executing]
- You can use
gccorclangas compiler - The
Wparameters ask the compilor to help us find potential errors - The
-o game42defines the name of the generated executable file - Using
./is necessary to execute the file
Beginner level
If you are a beginner in C, we suggest to start with some of the exercises proposed on this page.
More specifically, here is the sequence of exercises that we suggest you to solve:
- Compute the square of a number
- Computer the sum and difference of numbers
- Test if even or odd
- A simple calculator
- Computing average score
- Computing working hours
- Playing with a matrix
- Count voyels
- Playing with pointers
- Arrays and pointers
Medium and advanced programmers
- Solve the exercises proposed by Gregory MouniƩ