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"); } }