Difference: Lecture3:IntroductionToCProgramming (1 vs. 9)

Revision 92017-11-03 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: A bit of C programming

Line: 250 to 250
  c = (a<b) ? a : b;
Added:
>
>
questionResv2.png
 

The ? operator example

question.png

Deleted:
<
<
questionRes.png
 

Functions

Line: 309 to 308
  and you can write you own libraries to extend the set.
Changed:
<
<
There are 2 types of libraries: static libraries and dynamic ones.
>
>
There are two types of libraries: static libraries and dynamic ones.
  The static libraries are named lib name of the library .a
Changed:
<
<
Example libm.a for the mathematics library containing
>
>
Example libm.a is a library that contains a large set of
 
Changed:
<
<
a great number of mathematical functions
>
>
mathematical functions
  Its dynamic brother is called libm.so
Line: 327 to 326
  several executables. The executable is therefore much smaller but there is a risk
Changed:
<
<
it will not work if it was linked to a wrong library version.
>
>
it will not work if it was linked to a wrong library version, the library

is not available or cannot be found.

 

Linking libraries

Line: 358 to 359
 
META FILEATTACHMENT attachment="structs.png" attr="" comment="" date="1505125511" name="structs.png" path="structs.png" size="30043" user="uli" version="1"
META FILEATTACHMENT attachment="switch-1.png" attr="" comment="" date="1505124673" name="switch-1.png" path="switch-1.png" size="23707" user="uli" version="1"
META FILEATTACHMENT attachment="switch-2.png" attr="" comment="" date="1505124673" name="switch-2.png" path="switch-2.png" size="34389" user="uli" version="1"
Deleted:
<
<
META FILEATTACHMENT attachment="questionRes.png" attr="" comment="" date="1505125481" name="questionRes.png" path="questionRes.png" size="8264" user="uli" version="1"
 
META FILEATTACHMENT attachment="assignement.png" attr="" comment="" date="1504792877" name="assignement.png" path="assignement.png" size="30988" user="uli" version="1"
META FILEATTACHMENT attachment="typeError.png" attr="" comment="" date="1504792877" name="typeError.png" path="typeError.png" size="10701" user="uli" version="1"
META FILEATTACHMENT attachment="typeCast.png" attr="" comment="" date="1504792877" name="typeCast.png" path="typeCast.png" size="24274" user="uli" version="1"
Line: 371 to 371
 
META FILEATTACHMENT attachment="numericCommandLine.png" attr="" comment="" date="1505127678" name="numericCommandLine.png" path="numericCommandLine.png" size="56393" user="uli" version="1"
META FILEATTACHMENT attachment="arguments.c" attr="" comment="" date="1505411092" name="arguments.c" path="arguments.c" size="474" user="uli" version="1"
META FILEATTACHMENT attachment="while.png" attr="" comment="" date="1505463515" name="while.png" path="while.png" size="16495" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="questionRes.png" attr="" comment="" date="1509718660" name="questionRes.png" path="questionRes.png" size="8498" user="uli" version="1"
META FILEATTACHMENT attachment="questionResv2.png" attr="" comment="" date="1509718829" name="questionResv2.png" path="questionResv2.png" size="8498" user="uli" version="1"

Revision 82017-10-06 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

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

Revision 72017-09-15 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: A bit of C programming

Line: 144 to 144
  does not exceed a certain value?
Changed:
<
<
while.png
>
>
while.png
 

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
 
META FILEATTACHMENT attachment="typeError.png" attr="" comment="" date="1504792877" name="typeError.png" path="typeError.png" size="10701" user="uli" version="1"
META FILEATTACHMENT attachment="typeCast.png" attr="" comment="" date="1504792877" name="typeCast.png" path="typeCast.png" size="24274" user="uli" version="1"
META FILEATTACHMENT attachment="fibonacci.png" attr="" comment="" date="1504793137" name="fibonacci.png" path="fibonacci.png" size="30690" user="uli" version="1"
Deleted:
<
<
META FILEATTACHMENT attachment="while.png" attr="" comment="" date="1504793320" name="while.png" path="while.png" size="26008" user="uli" version="1"
 
META FILEATTACHMENT attachment="pointerExample.png" attr="" comment="" date="1504793514" name="pointerExample.png" path="pointerExample.png" size="41661" user="uli" version="1"
META FILEATTACHMENT attachment="hello.png" attr="" comment="" date="1504889659" name="hello.png" path="hello.png" size="7576" user="uli" version="1"
META FILEATTACHMENT attachment="lecture_3.odp" attr="" comment="" date="1504890075" name="lecture_3.odp" path="lecture_3.odp" size="442239" user="uli" version="1"
Line: 371 to 370
 
META FILEATTACHMENT attachment="commandLineExample.png" attr="" comment="" date="1505124615" name="commandLineExample.png" path="commandLineExample.png" size="31878" user="uli" version="1"
META FILEATTACHMENT attachment="numericCommandLine.png" attr="" comment="" date="1505127678" name="numericCommandLine.png" path="numericCommandLine.png" size="56393" user="uli" version="1"
META FILEATTACHMENT attachment="arguments.c" attr="" comment="" date="1505411092" name="arguments.c" path="arguments.c" size="474" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="while.png" attr="" comment="" date="1505463515" name="while.png" path="while.png" size="16495" user="uli" version="1"

Revision 62017-09-14 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: A bit of C programming

Line: 370 to 370
 
META FILEATTACHMENT attachment="file.png" attr="" comment="" date="1505124210" name="file.png" path="file.png" size="43479" user="uli" version="1"
META FILEATTACHMENT attachment="commandLineExample.png" attr="" comment="" date="1505124615" name="commandLineExample.png" path="commandLineExample.png" size="31878" user="uli" version="1"
META FILEATTACHMENT attachment="numericCommandLine.png" attr="" comment="" date="1505127678" name="numericCommandLine.png" path="numericCommandLine.png" size="56393" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="arguments.c" attr="" comment="" date="1505411092" name="arguments.c" path="arguments.c" size="474" user="uli" version="1"

Revision 52017-09-11 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: A bit of C programming

Changed:
<
<

Lecture 3

Uli Raich

>
>

Lecture 3

Uli Raich

First Semester 2017/2018

 

Programming Languages

Line: 59 to 59
 
assignement.png
Added:
>
>
I call the program assignments.c and I compile it with

gcc -o assignments assigments.c

Type casting

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.

typeCast.png

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.

 

Conditions: the if statement

Line: 85 to 134
  This can easily be done in a for loop
Deleted:
<
<

 

The for loop

fibonacci.png

Deleted:
<
<

 

The while loop

Can we also do calculations as long as the Fibonacci number

Line: 99 to 146
  while.png
Deleted:
<
<

 

Pointers

We 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

pointerExample.png

Added:
>
>

Printing to a file

Instead of printing to stdout with printf you can also print to a file with fprintf:

file.png

 

Command line arguments

Line: 134 to 184
 

Command line arguments example

Added:
>
>
commandLineExample.png

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

Conditional statements

  • if, else if, else
  • switch (tag)
  • Combination of assignment and condition: the ? operator.

Numeric command line arguments and if conditions

numericCommandLine.png

The switch statement

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

Switch example

switch-1.png

switch-2.png

Composite variables

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.

Composite data type example

structs.png

The ? operator

If (a < b)

c = a

else

c = b;

Can be written in a single statement:

c = (a<b) ? a : b;

The ? operator example

question.png

questionRes.png

Functions

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.

Include files

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:

include-1.png

and the call itself:

result = add( 5.2, 4.8);

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

Libraries

A 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 libraries

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

 %SLIDESHOWEND%

-- Uli Raich - 2017-09-07

Line: 144 to 354
 
META FILEATTACHMENT attachment="include.png" attr="" comment="" date="1504775835" name="include.png" path="include.png" size="5106" user="uli" version="1"
META FILEATTACHMENT attachment="include-1.png" attr="" comment="" date="1504775835" name="include-1.png" path="include-1.png" size="5319" user="uli" version="1"
Changed:
<
<
META FILEATTACHMENT attachment="question.png" attr="" comment="" date="1504775835" name="question.png" path="question.png" size="16957" user="uli" version="1"
META FILEATTACHMENT attachment="structs.png" attr="" comment="" date="1504775835" name="structs.png" path="structs.png" size="30043" user="uli" version="1"
META FILEATTACHMENT attachment="switch-1.png" attr="" comment="" date="1504775835" name="switch-1.png" path="switch-1.png" size="23707" user="uli" version="1"
META FILEATTACHMENT attachment="switch-2.png" attr="" comment="" date="1504775835" name="switch-2.png" path="switch-2.png" size="34389" user="uli" version="1"
META FILEATTACHMENT attachment="questionRes.png" attr="" comment="" date="1504775904" name="questionRes.png" path="questionRes.png" size="8264" user="uli" version="1"
>
>
META FILEATTACHMENT attachment="question.png" attr="" comment="" date="1505125481" name="question.png" path="question.png" size="16957" user="uli" version="1"
META FILEATTACHMENT attachment="structs.png" attr="" comment="" date="1505125511" name="structs.png" path="structs.png" size="30043" user="uli" version="1"
META FILEATTACHMENT attachment="switch-1.png" attr="" comment="" date="1505124673" name="switch-1.png" path="switch-1.png" size="23707" user="uli" version="1"
META FILEATTACHMENT attachment="switch-2.png" attr="" comment="" date="1505124673" name="switch-2.png" path="switch-2.png" size="34389" user="uli" version="1"
META FILEATTACHMENT attachment="questionRes.png" attr="" comment="" date="1505125481" name="questionRes.png" path="questionRes.png" size="8264" user="uli" version="1"
 
META FILEATTACHMENT attachment="assignement.png" attr="" comment="" date="1504792877" name="assignement.png" path="assignement.png" size="30988" user="uli" version="1"
META FILEATTACHMENT attachment="typeError.png" attr="" comment="" date="1504792877" name="typeError.png" path="typeError.png" size="10701" user="uli" version="1"
META FILEATTACHMENT attachment="typeCast.png" attr="" comment="" date="1504792877" name="typeCast.png" path="typeCast.png" size="24274" user="uli" version="1"
Line: 157 to 367
 
META FILEATTACHMENT attachment="pointerExample.png" attr="" comment="" date="1504793514" name="pointerExample.png" path="pointerExample.png" size="41661" user="uli" version="1"
META FILEATTACHMENT attachment="hello.png" attr="" comment="" date="1504889659" name="hello.png" path="hello.png" size="7576" user="uli" version="1"
META FILEATTACHMENT attachment="lecture_3.odp" attr="" comment="" date="1504890075" name="lecture_3.odp" path="lecture_3.odp" size="442239" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="file.png" attr="" comment="" date="1505124210" name="file.png" path="file.png" size="43479" user="uli" version="1"
META FILEATTACHMENT attachment="commandLineExample.png" attr="" comment="" date="1505124615" name="commandLineExample.png" path="commandLineExample.png" size="31878" user="uli" version="1"
META FILEATTACHMENT attachment="numericCommandLine.png" attr="" comment="" date="1505127678" name="numericCommandLine.png" path="numericCommandLine.png" size="56393" user="uli" version="1"

Revision 42017-09-08 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: A bit of C programming

Line: 33 to 33
  “Hello World” program
Changed:
<
<
#include <stdio.h>

void main() {

printf(“Hello World!\n”);

}

>
>
hello.png
 
Let us try to compile and execute this program
Line: 59 to 53
 
  • enum
And you can define your own data types with typedef
Changed:
<
<

Assignements

>
>

Assignments

  We modify the program to do some calculation:


assignement.png

Added:
>
>

Conditions: the if statement

Conditions 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 numbers

The Fibonacci numbers:

Line: 108 to 116
 

Pointer Example

pointerExample.png

Added:
>
>

Command line arguments

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 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%

-- Uli Raich - 2017-09-07

Line: 129 to 155
 
META FILEATTACHMENT attachment="fibonacci.png" attr="" comment="" date="1504793137" name="fibonacci.png" path="fibonacci.png" size="30690" user="uli" version="1"
META FILEATTACHMENT attachment="while.png" attr="" comment="" date="1504793320" name="while.png" path="while.png" size="26008" user="uli" version="1"
META FILEATTACHMENT attachment="pointerExample.png" attr="" comment="" date="1504793514" name="pointerExample.png" path="pointerExample.png" size="41661" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="hello.png" attr="" comment="" date="1504889659" name="hello.png" path="hello.png" size="7576" user="uli" version="1"
META FILEATTACHMENT attachment="lecture_3.odp" attr="" comment="" date="1504890075" name="lecture_3.odp" path="lecture_3.odp" size="442239" user="uli" version="1"

Revision 32017-09-07 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: A bit of C programming

Line: 59 to 59
 
  • enum
And you can define your own data types with typedef
Deleted:
<
<

 

Assignements

Changed:
<
<
We modify the program to do some calculation:

>
>
We modify the program to do some calculation:


assignement.png

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

The for loop

fibonacci.png

The while loop

Can we also do calculations as long as the Fibonacci number

does not exceed a certain value?

while.png

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.

Pointer Example

pointerExample.png

  %SLIDESHOWEND%
Line: 79 to 123
 
META FILEATTACHMENT attachment="switch-1.png" attr="" comment="" date="1504775835" name="switch-1.png" path="switch-1.png" size="23707" user="uli" version="1"
META FILEATTACHMENT attachment="switch-2.png" attr="" comment="" date="1504775835" name="switch-2.png" path="switch-2.png" size="34389" user="uli" version="1"
META FILEATTACHMENT attachment="questionRes.png" attr="" comment="" date="1504775904" name="questionRes.png" path="questionRes.png" size="8264" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="assignement.png" attr="" comment="" date="1504792877" name="assignement.png" path="assignement.png" size="30988" user="uli" version="1"
META FILEATTACHMENT attachment="typeError.png" attr="" comment="" date="1504792877" name="typeError.png" path="typeError.png" size="10701" user="uli" version="1"
META FILEATTACHMENT attachment="typeCast.png" attr="" comment="" date="1504792877" name="typeCast.png" path="typeCast.png" size="24274" user="uli" version="1"
META FILEATTACHMENT attachment="fibonacci.png" attr="" comment="" date="1504793137" name="fibonacci.png" path="fibonacci.png" size="30690" user="uli" version="1"
META FILEATTACHMENT attachment="while.png" attr="" comment="" date="1504793320" name="while.png" path="while.png" size="26008" user="uli" version="1"
META FILEATTACHMENT attachment="pointerExample.png" attr="" comment="" date="1504793514" name="pointerExample.png" path="pointerExample.png" size="41661" user="uli" version="1"

Revision 22017-09-07 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"
Changed:
<
<
Start Presentation %SLIDESHOWSTART
>
>
Start Presentation

 

A bit of C programming

Lecture 3

Uli Raich

Line: 24 to 24
  or
Changed:
<
<
#include “myOwnIncludeFile.h”

>
>
#include “myOwnIncludeFile.h”

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.

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

Assignements

We modify the program to do some calculation:

  %SLIDESHOWEND%

Revision 12017-09-07 - uli

Line: 1 to 1
Added:
>
>
Start Presentation
META TOPICPARENT name="LectureSlides"
%SLIDESHOWSTART

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”

-- Uli Raich - 2017-09-07

Comments

<--/commentPlugin-->

META FILEATTACHMENT attachment="include.png" attr="" comment="" date="1504775835" name="include.png" path="include.png" size="5106" user="uli" version="1"
META FILEATTACHMENT attachment="include-1.png" attr="" comment="" date="1504775835" name="include-1.png" path="include-1.png" size="5319" user="uli" version="1"
META FILEATTACHMENT attachment="question.png" attr="" comment="" date="1504775835" name="question.png" path="question.png" size="16957" user="uli" version="1"
META FILEATTACHMENT attachment="structs.png" attr="" comment="" date="1504775835" name="structs.png" path="structs.png" size="30043" user="uli" version="1"
META FILEATTACHMENT attachment="switch-1.png" attr="" comment="" date="1504775835" name="switch-1.png" path="switch-1.png" size="23707" user="uli" version="1"
META FILEATTACHMENT attachment="switch-2.png" attr="" comment="" date="1504775835" name="switch-2.png" path="switch-2.png" size="34389" user="uli" version="1"
META FILEATTACHMENT attachment="questionRes.png" attr="" comment="" date="1504775904" name="questionRes.png" path="questionRes.png" size="8264" user="uli" version="1"
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 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