Basic C
Introduction
This explanation is meant as a starting point to give you some general idea about what you are supposed to learn to succesfully complete the exercises. It will not go into any depth. You can find way better explanations about the various concepts online, so we suggest you do that. When in doubt, you can always consult your peers.
The C Programming Language
C is a programming language. According to Wikipedia: A programming language is a system of notation for writing computer programs. We will use the C programming language because it has limited syntax and it will allow you to very easily make horrible mistakes. Using C will ensure you'll gain a deep understanding of how software works under the hood. This foundation will allow you to easily learn other languages and concepts in your future software engineering career.
Compiling a C Program
The source code for a very basic C program looks like this:
int main() { return 0; }
This program will not do anything usefull, it will just exit successfully. To try and run the program you can put the source code in a file with the .c extension like: main.c. Then you need a compiler to translate the source code to an executable program. At Codam, we use the cccompiler.
You can use it like this:
cc main.c
If the compilation was succesfull you should now have a file called a.out in your current directory. This file is your program! Now let's run it:
./a.out
You might question why you need the ./ before a.out. It's because, when you leave it out, this happens.
Variables
char somechar = 'c';
Conditions
if (somechar == 'c') { return true; } else if (somechar == 'a') { return true; } else { return false; }
Loops
while (somechar != 'h') { somechar += 1; }
Getting familiar with the syntax
To get you more familiar with C syntax, you can edit and run the following code snippets right here in the browser using the play button.
void get_42() { return (41); } #include <stdio.h> int main() { int ft = get_42(); if (ft == 42) { printf("[Succeeded!] Return value of get_42 is: %d", ft); } else { printf("[Failed!] Return value of get_42 isn't 42"); } }
void get_42() { return (41); } #include <stdio.h> int main() { int ft = get_42(); if (ft == 42) { printf("[Succeeded!] Return value of get_42 is: %d", ft); } else { printf("[Failed!] Return value of get_42 isn't 42"); } }
Instructions
- Only this page will serve as reference.
- Make sure you have the appropriate permissions on your files and directories.
- You have to follow the submission procedures for all your exercises.
- Your exercises will be checked and graded by your fellow classmates.
- On top of that, your exercises will be checked and graded by a program called Moulinette.
- Moulinette is very meticulous and strict in its evaluation of your work. It is entirely automated and there is no way to negotiate with it. So if you want to avoid bad surprises, be as thorough as possible.
- Moulinette is not very open-minded. It won’t try and understand your code if it doesn’t respect the Norm. Moulinette relies on a program called norminette to check if your files respect the norm. TL;DR: it would be idiotic to submit a piece of work that doesn’t pass norminette’s check.
- Using an external function that is not explicitly allowed by the subject will get you a 0.
- You’ll only have to submit a main() function if we ask for a program.
- Moulinette compiles with these flags:
-Wall -Wextra -Werror, and usescc. - If your program doesn’t compile, you’ll get 0.
- We expect your repository to be clean. This means you can only hand in the files asked by the subject, and useful files like
.gitignoreandREADME.md - Got a question? Ask your peer on the right. Otherwise, try your peer on the left.
Advice: Do not forget to add the standard 42 header in each of your .c/.h files. The norminette check its existence anyway!
Warning: Norminette must be launched with the -R CheckForbiddenSourceHeader flag. Moulinette will use it too.
print_c
| print_c | |
|---|---|
| Turn-in directory | 00_print_c/ |
| Files to turn in | print_c.c |
| Allowed functions | putchar |
- Write a program which displays the character
con standard output. - To help you get started, we will provide some code for you to copy/paste/edit. Make sure you understand every line before you hand it in!
#include <stdio.h> int main() { char c = '0'; putchar(c); return 0; }
Example output:
$ ./a.out
c$
print_c_nicely
| print_c_nicely | |
|---|---|
| Turn-in directory | 01_print_c_nicely/ |
| Files to turn in | print_c_nicely.c |
| Allowed functions | putchar |
- Write a program which displays the character
con standard output, followed by a newline.
Example output:
$ ./a.out
c
$
hello_codam
| hello_codam | |
|---|---|
| Turn-in directory | 02_hello_codam/ |
| Files to turn in | hello_codam.c |
| Allowed functions | putchar |
- Write a program which displays
Hello Codam!on standard output, followed by a newline.
Example output:
$ ./a.out
Hello Codam!
$
normal_alpha
| normal_alpha | |
|---|---|
| Turn-in directory | 03_normal_alpha/ |
| Files to turn in | normal_alpha.c |
| Allowed functions | putchar |
- Write a program which displays the lowercase alphabet on standard output, followed by a newline.
Example output:
$ ./a.out
abcdefghijklmnopqrstuvwxyz
$
partial_alpha
| partial_alpha | |
|---|---|
| Turn-in directory | 04_partial_alpha/ |
| Files to turn in | partial_alpha.c |
| Allowed functions | putchar |
- Write a program which displays the alphabet in lowercase on standard output, starting from the character in variable
start_char, followed by a newline. - Your programs needs to have a variable called
start_char, by default it will hold the character'c'. Changing this value changes the output of your program. The value can be set to any lowercase letter.- Example:
char start_char; start_char = 'c';
- Example:
Example output:
$ ./a.out
cdeafghijklmnopqrstuvqxyz
$
partial_partial_alpha
| partial_partial_alpha | |
|---|---|
| Turn-in directory | 05_partial_partial_alpha/ |
| Files to turn in | partial_partial_alpha.c |
| Allowed functions | putchar |
- Write a program which displays the alphabet in lowercase on standard output, starting from the character in variable
start_char, ending with the character instop_char, followed by a newline. - Your programs needs to have a variable called
start_char, by default it will hold the character'c'. Changing this value changes the output of your program. The value can be set to any lower or uppercase letter. - Your programs needs to have a variable called
stop_char, by default it will hold the character'x'. Changing this value changes the output of your program. The value can be set to any lowercase letter.
Example output:
$ ./a.out
cdeafghijklmnopqrstuvqx
$
wild_alpha
| wild_alpha | |
|---|---|
| Turn-in directory | 06_wild_alpha/ |
| Files to turn in | wild_alpha.c |
| Allowed functions | putchar |
- Write a program which displays the alphabet in alternating lower and uppercase on standard output, starting from the character in variable
start_char, followed by a newline. - Your programs needs to have a variable called
start_char, by default it will hold the character'C'. Changing this value changes the output of your program. The value can be set to any lower or uppercase letter.
Example output:
$ ./a.out
CdEfGhIjKlMnOpQrStUvWxYz
$
wilder_alpha
| wilder_alpha | |
|---|---|
| Turn-in directory | 07_wilder_alpha/ |
| Files to turn in | wilder_alpha.c |
| Allowed functions | putchar |
- Write a program which displays the alphabet based on the starting character in variable
start_char, followed by a newline. Use the following rules:- If the starting character is lowercase, display a maximum of 10 letters. If the starting character is uppercase, display a maximum of 12 letters.
- 2 Lowercase characters are always followed by an uppercase character. Uppercase characters are always alone.
- Lowercase characters don't like to be alone, always try to display them in pairs. An exception can be made for the last one.
- Your programs needs to have a variable called
start_char, by default it will hold the character'C'. Changing this value changes the output of your program. The value can be set to any lower or uppercase letter.
Example output:
$ ./a.out # using start_char = 'C'
CdeFghIjkLmn
$ ./a.out # using start_char = 't'
tuVwxYz
$
wildest_alpha
| wildest_alpha | |
|---|---|
| Turn-in directory | 08_wildest_alpha/ |
| Files to turn in | wildest_alpha.c |
| Allowed functions | putchar |
- Write a program which displays the alphabet starting from the character in variable
start_char, followed by a newline. Use the following rules:- If the starting character is lowercase, display the alphabet in the
a -> zdirection. If the starting character is uppercase, display the alphabet in theZ -> Adirection. - Do not display these characters:
'F', 'P', 'Z', 'd', 'n', 'x' - Replace these characters with the corresponding number:
'a' && 'A' ->4, 'e' && 'E' ->3, 'i' && 'I' ->1, 'o' && 'O' ->0`` - The first and last character are displayed in uppercase, all others are displayed in lowercase. Except where other rules apply.
- The character
'u'is always displayed uppercase. - When there is nothing to display, we still expect the newline.
- If the starting character is lowercase, display the alphabet in the
- Your programs needs to have a variable called
start_char, by default it will hold the character'c'. Changing this value changes the output of your program. The value can be set to any lower or uppercase letter. - Your programs needs to have a variable called
stop_char, by default it will hold the character'x'. Changing this value changes the output of your program. The value can be set to any lowercase letter.
Example output:
$ ./a.out
C3fgh1jklm0pqrstUvwy
$
Submission & Evaluation
Turn in your assignment in your Git repository. Only the work on the main branch of your repository will be evaluated during the defense. Don’t hesitate to double check the names of your files to ensure they are correct.
Warning You need to add only the files requested by the subject. Usefull files like
.gitignoreandREADME.mdare also allowed, garbage like.DS_Storeor*.ofiles will turn your grade into a 0.