Slide 1: A bit of C programming
Lecture 3
Uli Raich
Slide 2: Programming Languages
To make a language a programming language it needs to implement:
- Assignments
- Conditional statements
- loops
Slide 3: C libraries
C uses a large number of code libraries and you can create C libraries yourself.
These libraries may use special data types, which are defined in include files
Before using the library functions
#include <stdio.h>
or
#include “myOwnIncludeFile.h”
Slide 4: The C main program
As a first example people usually write the
“Hello World” program
#include <stdio.h>
void main() {
printf(“Hello World!\n”);
}
Let us try to compile and execute this program
First we start the editor, we type and save the program,
then we compile it using the gcc compiler and finally we execute it.
Slide 5: C data types
- C has a number of data types:
- char, short, int, long, float, double
- unsigned char, unsigned short, unsigned int
- can be extended to boolean (in C99 you can #include <stdbool.h>
- No strings! But a pointer to a zero terminated chain of characters
- struct
- union
- enum
And you can define your own data types with typedef
Slide 6: Assignements
We modify the program to do some calculation:
Slide 7: Calculating the Fibonacci numbers
The Fibonacci numbers:
0 1 1 2 3 5 8 13 21 34 …
or xn = xn-1+xn-2
How can we write a program to calculate up to 12 such numbers?
This can easily be done in a for loop
Slide 8: The for loop
Slide 9: The while loop
Can we also do calculations as long as the Fibonacci number
does not exceed a certain value?
Slide 10: Pointers
We can define variables which do not contain the value but
the address of where the value is stored in memory:
char a=5; is the value
char *myText=”Hello World!”;
myText points to the place in memory where Hello World is stored.
Slide 11: Pointer Example
--
Uli Raich - 2017-09-07
Comments