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 uses cc.
  • 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 .gitignore and README.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
Turn-in directory00_print_c/
Files to turn inprint_c.c
Allowed functionsputchar
  • Write a program which displays the character c on 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
Turn-in directory01_print_c_nicely/
Files to turn inprint_c_nicely.c
Allowed functionsputchar
  • Write a program which displays the character c on standard output, followed by a newline.

Example output:

$ ./a.out
c
$

hello_codam

hello_codam
Turn-in directory02_hello_codam/
Files to turn inhello_codam.c
Allowed functionsputchar
  • 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 directory03_normal_alpha/
Files to turn innormal_alpha.c
Allowed functionsputchar
  • 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 directory04_partial_alpha/
Files to turn inpartial_alpha.c
Allowed functionsputchar
  • 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 output:

$ ./a.out
cdeafghijklmnopqrstuvqxyz
$

partial_partial_alpha

partial_partial_alpha
Turn-in directory05_partial_partial_alpha/
Files to turn inpartial_partial_alpha.c
Allowed functionsputchar
  • Write a program which displays the alphabet in lowercase on standard output, starting from the character in variable start_char, ending with the character in stop_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 directory06_wild_alpha/
Files to turn inwild_alpha.c
Allowed functionsputchar
  • 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 directory07_wilder_alpha/
Files to turn inwilder_alpha.c
Allowed functionsputchar
  • 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 directory08_wildest_alpha/
Files to turn inwildest_alpha.c
Allowed functionsputchar
  • 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 -> z direction. If the starting character is uppercase, display the alphabet in the Z -> A direction.
    • 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.
  • 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 .gitignore and README.md are also allowed, garbage like .DS_Store or *.o files will turn your grade into a 0.