Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
![]() Slide 1: A bit of C programming | ||||||||
Line: 115 to 115 | ||||||||
if (a < b) | ||||||||
Changed: | ||||||||
< < | printf(“a is bigger than b\n”); | |||||||
> > | printf(“a is smaller than b\n”); | |||||||
else | ||||||||
Changed: | ||||||||
< < | printf(“b is bigger than a\n”); | |||||||
> > | printf(“a is bigger or equal to b\n”); | |||||||
Calculating the Fibonacci numbers |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
![]() Slide 1: A bit of C programming | ||||||||
Line: 144 to 144 | ||||||||
does not exceed a certain value? | ||||||||
Changed: | ||||||||
< < | ![]() | |||||||
> > | ![]() | |||||||
Pointers | ||||||||
Line: 175 to 175 | ||||||||
int main(int argc, char ** argv) or | ||||||||
Changed: | ||||||||
< < | int main(int argv, char *argv[ ]); | |||||||
> > | int main(int argc, char *argv[ ]); | |||||||
int argc is the number of arguments passed | ||||||||
Line: 363 to 363 | ||||||||
| ||||||||
Deleted: | ||||||||
< < |
| |||||||
| ||||||||
Line: 371 to 370 | ||||||||
| ||||||||
Added: | ||||||||
> > |
|
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
![]() Slide 1: A bit of C programming | ||||||||
Line: 370 to 370 | ||||||||
| ||||||||
Added: | ||||||||
> > |
|
Line: 1 to 1 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() Slide 1: A bit of C programming | |||||||||||
Changed: | |||||||||||
< < | Lecture 3Uli Raich | ||||||||||
> > | Lecture 3Uli RaichFirst Semester 2017/2018 | ||||||||||
Programming Languages | |||||||||||
Line: 59 to 59 | |||||||||||
![]() | |||||||||||
Added: | |||||||||||
> > | I call the program assignments.c and I compile it with
gcc -o assignments assigments.c
Type castingWhy 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.![]() Printing formatted outputYou 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
| ||||||||||
Conditions: the if statement | |||||||||||
Line: 85 to 134 | |||||||||||
This can easily be done in a for loop | |||||||||||
Deleted: | |||||||||||
< < | |||||||||||
The for loop![]() | |||||||||||
Deleted: | |||||||||||
< < | |||||||||||
The while loopCan we also do calculations as long as the Fibonacci number | |||||||||||
Line: 99 to 146 | |||||||||||
![]() | |||||||||||
Deleted: | |||||||||||
< < | |||||||||||
PointersWe can define variables which do not contain the value but | |||||||||||
Line: 112 to 158 | |||||||||||
myText points to the place in memory where Hello World is stored. | |||||||||||
Deleted: | |||||||||||
< < | |||||||||||
Pointer Example | |||||||||||
Added: | |||||||||||
> > | Printing to a fileInstead of printing to stdout with printf you can also print to a file with fprintf:![]() | ||||||||||
Command line arguments | |||||||||||
Line: 134 to 184 | |||||||||||
Command line arguments example | |||||||||||
Added: | |||||||||||
> > | ![]() Conditional statements
Numeric command line arguments and if conditions![]() The switch statementImagine a simple calculator taking 3 parameters:
case ‘+’: do addition; break; case ‘-’: do subtraction; break; default: error; } Switch example![]() ![]() Composite variablesWe 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. Composite data type example![]() The ? operatorIf (a < b) c = a else c = b; Can be written in a single statement: c = (a<b) ? a : b;The ? operator example![]() ![]() FunctionsUp 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.Include filesHow 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:![]() Contents of the include fileThis is how the add.h include file could look like: /*include file for add.c function */ double add(double,double); LibrariesA big number of libraries are available for use with C and you can write you own libraries to extend the set. There are 2 types of libraries: static libraries and dynamic ones. The static libraries are named lib name of the library .a Example libm.a for the mathematics library containing a great number 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.Linking librariesLet’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 | ||||||||||
%SLIDESHOWEND%
-- ![]() | |||||||||||
Line: 144 to 354 | |||||||||||
| |||||||||||
Changed: | |||||||||||
< < |
| ||||||||||
> > |
| ||||||||||
| |||||||||||
Line: 157 to 367 | |||||||||||
| |||||||||||
Added: | |||||||||||
> > |
|
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
![]() Slide 1: A bit of C programming | ||||||||
Line: 33 to 33 | ||||||||
“Hello World” program | ||||||||
Changed: | ||||||||
< < | #include <stdio.h> void main() { printf(“Hello World!\n”); } | |||||||
> > | ![]() | |||||||
Let us try to compile and execute this program | ||||||||
Line: 59 to 53 | ||||||||
| ||||||||
Changed: | ||||||||
< < | Assignements | |||||||
> > | Assignments | |||||||
We modify the program to do some calculation:
![]() | ||||||||
Added: | ||||||||
> > | Conditions: the if statementConditions can be tested with if if (a < b) printf(“a is bigger than b\n”); else printf(“b is bigger than a\n”); | |||||||
Calculating the Fibonacci numbersThe Fibonacci numbers: | ||||||||
Line: 108 to 116 | ||||||||
Pointer Example | ||||||||
Added: | ||||||||
> > | Command line argumentsThe 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 argv, char *argv[ ]); int argc is the number of arguments passed char **argv is a pointer to a list of null terminated C strings.Command line arguments example | |||||||
%SLIDESHOWEND%
-- ![]() | ||||||||
Line: 129 to 155 | ||||||||
| ||||||||
Added: | ||||||||
> > |
|
Line: 1 to 1 | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
![]() Slide 1: A bit of C programming | |||||||||||||
Line: 59 to 59 | |||||||||||||
| |||||||||||||
Deleted: | |||||||||||||
< < | |||||||||||||
Assignements | |||||||||||||
Changed: | |||||||||||||
< < | We modify the program to do some calculation: | ||||||||||||
> > | We modify the program to do some calculation:
![]() Calculating the Fibonacci numbersThe 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 loopThe for loop![]() The while loopCan we also do calculations as long as the Fibonacci number does not exceed a certain value?![]() PointersWe 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. Pointer Example | ||||||||||||
%SLIDESHOWEND% | |||||||||||||
Line: 79 to 123 | |||||||||||||
| |||||||||||||
Added: | |||||||||||||
> > |
|
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
Changed: | ||||||||
< < |
![]() | |||||||
> > |
![]() | |||||||
A bit of C programmingLecture 3Uli Raich | ||||||||
Line: 24 to 24 | ||||||||
or | ||||||||
Changed: | ||||||||
< < | #include “myOwnIncludeFile.h” | |||||||
> > | #include “myOwnIncludeFile.h”
The C main programAs 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. C data types
AssignementsWe modify the program to do some calculation: | |||||||
%SLIDESHOWEND% |
Line: 1 to 1 | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Added: | |||||||||||||||||
> > |
![]()
Slide 1: A bit of C programmingLecture 3Uli RaichSlide 2: Programming LanguagesTo make a language a programming language it needs to implement:
Slide 3: C librariesC 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”-- ![]() Comments
|