First Steps with C11
Content |
Some remarks on compiling C code:
-
Always compile with the -Wall flag to get warnings that often indicate errors.
-
Compile with -std=c11 because by default many compiler still use the C89 standard by default.
For all examples do the following:
-
Compile the program, run it and check the exit code. Use the -o flag such that the executable is named like the source file without the .c suffix.
-
Look at the output the C preprocessor produces (call gcc with the -E flag).
-
Look at the assembler code produced by the compiler:
-
Use the -S flag such that gcc stops after translating the C code into assembler code.
-
Also use the -fno-asynchronous-unwind-tables and -fomit-frame-pointer flags (otherwise gcc produces lots of code we do not want to understand at the moment).
-
-
For the ULM we used a different assembly language. However, there are many similarities. Try to understand the meaning of the assembler code produced by gcc
-
Check how the assembler code produced by gcc is different when you compile with optimization flags, e.g.
-
Compile with -O1
-
Compile with -O2
-
Compile with -O3
-
Example on Arrays
#define N 5 unsigned char x[N] = {1,2,3,4,5}; int main() { register unsigned long long res = 0; for (register unsigned long long i=0; i<N; ++i) { res += x[i]; } return res; }
Example on For loops
#define N 5 int main() { register unsigned long long res = 1; for (register unsigned long long i=2; i<=N; ++i) { res *= i; } return res; }
Example on While loops
Explain why the following program will not compile. Then fix it and proceed as before:
#define N 5 int main() { register unsigned long long res = 1; while (N>1) { res *= N; --N; } return res; }