Start Presentation

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

hello.png


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: Assignments

We modify the program to do some calculation:


assignement.png

Slide 7: 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”);

Slide 8: 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 9: The for loop

fibonacci.png

Slide 10: The while loop

Can we also do calculations as long as the Fibonacci number

does not exceed a certain value?

while.png

Slide 11: 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 12: Pointer Example

pointerExample.png

Slide 13: 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.

Slide 14: Command line arguments example

-- Uli Raich - 2017-09-07

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng assignement.png r1 manage 30.3 K 2017-09-07 - 14:01 UnknownUser  
PNGpng fibonacci.png r1 manage 30.0 K 2017-09-07 - 14:05 UnknownUser  
PNGpng hello.png r1 manage 7.4 K 2017-09-08 - 16:54 UnknownUser  
PNGpng include-1.png r1 manage 5.2 K 2017-09-07 - 09:17 UnknownUser  
PNGpng include.png r1 manage 5.0 K 2017-09-07 - 09:17 UnknownUser  
Unknown file formatodp lecture_3.odp r1 manage 431.9 K 2017-09-08 - 17:01 UnknownUser  
PNGpng pointerExample.png r1 manage 40.7 K 2017-09-07 - 14:11 UnknownUser  
PNGpng question.png r1 manage 16.6 K 2017-09-07 - 09:17 UnknownUser  
PNGpng questionRes.png r1 manage 8.1 K 2017-09-07 - 09:18 UnknownUser  
PNGpng structs.png r1 manage 29.3 K 2017-09-07 - 09:17 UnknownUser  
PNGpng switch-1.png r1 manage 23.2 K 2017-09-07 - 09:17 UnknownUser  
PNGpng switch-2.png r1 manage 33.6 K 2017-09-07 - 09:17 UnknownUser  
PNGpng typeCast.png r1 manage 23.7 K 2017-09-07 - 14:01 UnknownUser  
PNGpng typeError.png r1 manage 10.5 K 2017-09-07 - 14:01 UnknownUser  
PNGpng while.png r1 manage 25.4 K 2017-09-07 - 14:08 UnknownUser  
Edit | Attach | Watch | Print version | History: r9 | r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r4 - 2017-09-08 - uli
 
  • Edit
  • Attach
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