Lecture 3
Uli Raich
First Semester 2017/2018
COPYRIGHT © 2024 by the contributing authors
Slide 1 of 30
To make a language a programming language it needs to implement:
- Assignments
- Conditional statements
- loops
COPYRIGHT © 2024 by the contributing authors
Slide 2 of 30
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”
COPYRIGHT © 2024 by the contributing authors
Slide 3 of 30
As a first example people usually write the
“Hello World” program
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.
COPYRIGHT © 2024 by the contributing authors
Slide 4 of 30
- 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
COPYRIGHT © 2024 by the contributing authors
Slide 5 of 30
We modify the program to do some calculation:
I call the program assignments.c and I compile it with
gcc -o assignments assigments.c
COPYRIGHT © 2024 by the contributing authors
Slide 6 of 30
Why do we get this strange result for the division? and how can we correct this?
Yes, the reason is that div has got the wrong (namely integer) type
To correct we must first convert a and b to doubles (or floats) before
doing the calculation and div must be a double as well.
COPYRIGHT © 2024 by the contributing authors
Slide 7 of 30
Printing formatted output
You have seen that I use the function printf to output
the results of the calculation.
The format string “%d” tells the system to output the
result as a decimal number.
In an exercise this afternoon, where you will implement a
simple calculator and we will use the format %10.4f
There are many additional number formats to output
- strings,
- decimal,
- octal and hex numbers …
Look up the man page for printf for an explanation.
Instead of printing on the screen you can also convert the
number to a string using sprintf.
or print to a file using fprintf
(the file must be opened first, see later)
The equivalent call for input also exists and is called scanf.
COPYRIGHT © 2024 by the contributing authors
Slide 8 of 30
Conditions: the if statement
Conditions can be tested with if
if (a < b)
printf(“a is smaller than b\n”);
else
printf(“a is bigger or equal to b\n”);
COPYRIGHT © 2024 by the contributing authors
Slide 9 of 30
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
COPYRIGHT © 2024 by the contributing authors
Slide 10 of 30
COPYRIGHT © 2024 by the contributing authors
Slide 11 of 30
Can we also do calculations as long as the Fibonacci number
does not exceed a certain value?
COPYRIGHT © 2024 by the contributing authors
Slide 12 of 30
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.
COPYRIGHT © 2024 by the contributing authors
Slide 13 of 30
COPYRIGHT © 2024 by the contributing authors
Slide 14 of 30
Instead of printing to stdout with printf you can also print to a file with fprintf:
COPYRIGHT © 2024 by the contributing authors
Slide 15 of 30
The main routine has 2 parameters, which we did not use
yet as well as a return code.
int main(int argc, char ** argv) or
int main(int argc, char *argv[ ]);
int argc is the number of arguments passed
char **argv is a pointer to a list of null terminated C strings.
COPYRIGHT © 2024 by the contributing authors
Slide 16 of 30
Command line arguments example
Please note that the argv values are zero terminated strings even
if you give a number! If you expect a number then you have to convert
the string into a number with atoi or atof
COPYRIGHT © 2024 by the contributing authors
Slide 17 of 30
- if, else if, else
- switch (tag)
- Combination of assignment and condition: the ? operator.
COPYRIGHT © 2024 by the contributing authors
Slide 18 of 30
Numeric command line arguments and if conditions
COPYRIGHT © 2024 by the contributing authors
Slide 19 of 30
Imagine a simple calculator taking 3 parameters:
- 2 numbers
- An mathematical operator: +,-,*,/
We do not want to have too many if statements to find out which operation must be carried out
switch (operator) {
case ‘+’:
do addition;
break;
case ‘-’:
do subtraction;
break;
default:
error;
}
COPYRIGHT © 2024 by the contributing authors
Slide 20 of 30
COPYRIGHT © 2024 by the contributing authors
Slide 21 of 30
We can have a series of values of the same type in an array:
int myArray[10]; the size of the array is fixed!
An array is in reality a pointer to a series of values of the same type.
Or we can define structures where values of different types can be combined
In addition we have enumerations with a certain fixed range of values.
COPYRIGHT © 2024 by the contributing authors
Slide 22 of 30
Composite data type example
COPYRIGHT © 2024 by the contributing authors
Slide 23 of 30
If (a < b)
c = a
else
c = b;
Can be written in a single statement:
c = (a<b) ? a : b;
COPYRIGHT © 2024 by the contributing authors
Slide 24 of 30
COPYRIGHT © 2024 by the contributing authors
Slide 25 of 30
Up to now the size of our programs did not exceed one page.
Bigger problems must be broken down into smaller, manageable pieces using functions
Example: the operations of our calculator:
double add(double num1, double num2) {
return (num1+num2);
}
The other operations look similar.
These functions may go into the same file as main but also into separate files.
Then they are compiled separately and linked to the main routine
to form an executable program.
COPYRIGHT © 2024 by the contributing authors
Slide 26 of 30
How does the main program know the name of the function and its parameters?
Define these in an include file to be added to the calling program:
and the call itself:
result = add( 5.2, 4.8);
COPYRIGHT © 2024 by the contributing authors
Slide 27 of 30
Contents of the include file
This is how the add.h include file could look like:
/*
include file for add.c function
*/
double add(double,double);
COPYRIGHT © 2024 by the contributing authors
Slide 28 of 30
A big number of libraries are available for use with C
and you can write you own libraries to extend the set.
There are two types of libraries: static libraries and dynamic ones.
The static libraries are named lib name of the library .a
Example libm.a is a library that contains a large set of
mathematical functions
Its dynamic brother is called libm.so
When linking statically the library is added to the executable making
it substantially bigger but independent of library versions
The dynamic library is loaded once into memory and can be used by
several executables. The executable is therefore much smaller but there is a risk
it will not work if it was linked to a wrong library version, the library
is not available or cannot be found.
COPYRIGHT © 2024 by the contributing authors
Slide 29 of 30
Let’s say your program sine.c uses the sine function.
The you must link it to libm
In the static case:
gcc -static -o sine sine.o -lm
In the dynamic case you skip the -static option
gcc -o sine sine.c -lm
COPYRIGHT © 2024 by the contributing authors
Slide 30 of 30
--
Uli Raich - 2017-09-07
Comments
This topic: Embedded_Systems
> WebHome >
LectureSlides > Lecture3:IntroductionToCProgramming
Topic revision: r9 - 2017-11-03 - uli
Copyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback