Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between...

58
Introduction to C Torje Ho˚ as Digernes Comparison between Languages Why C IO Minneh˚ andtering Sortering Introduction to C Welcome to R’lyeh Torje Ho˚ as Digernes Programvareverkstedet - PVV Trondheim 2017

Transcript of Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between...

Page 1: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

Sortering

Introduction to CWelcome to R’lyeh

Torje Hoas Digernes

Programvareverkstedet - PVV

Trondheim 2017

Page 2: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguagesPython and CJava and C

Why C

IO

Minnehandtering

Sortering

Outline

1 Comparison between LanguagesPython and CJava and C

2 Why CIs there any pros at all?Funksjonskall

3 IOIO functionsprintf() examplescanf() example

4 Minnehandteringpekerestruct

5 SorteringFunksjonspekere

Page 3: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguagesPython and CJava and C

Why C

IO

Minnehandtering

Sortering

Python and COptional Subtitle

• Manual memory management

• Archaic module system (glorified copy paste)

• limited standard library

• No builtin array

Page 4: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguagesPython and CJava and C

Why C

IO

Minnehandtering

Sortering

Python and COptional Subtitle

• Manual memory management

• Archaic module system (glorified copy paste)

• limited standard library

• No builtin array

Page 5: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguagesPython and CJava and C

Why C

IO

Minnehandtering

Sortering

Python and COptional Subtitle

• Manual memory management

• Archaic module system (glorified copy paste)

• limited standard library

• No builtin array

Page 6: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguagesPython and CJava and C

Why C

IO

Minnehandtering

Sortering

Python and COptional Subtitle

• Manual memory management

• Archaic module system (glorified copy paste)

• limited standard library

• No builtin array

Page 7: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguagesPython and CJava and C

Why C

IO

Minnehandtering

Sortering

Outline

1 Comparison between LanguagesPython and CJava and C

2 Why CIs there any pros at all?Funksjonskall

3 IOIO functionsprintf() examplescanf() example

4 Minnehandteringpekerestruct

5 SorteringFunksjonspekere

Page 8: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguagesPython and CJava and C

Why C

IO

Minnehandtering

Sortering

Java and C

• Manual memory management

• Archaic module system (glorified copy paste)

• limited standard library

• No builtin array

Page 9: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguagesPython and CJava and C

Why C

IO

Minnehandtering

Sortering

Java and C

• Manual memory management

• Archaic module system (glorified copy paste)

• limited standard library

• No builtin array

Page 10: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguagesPython and CJava and C

Why C

IO

Minnehandtering

Sortering

Java and C

• Manual memory management

• Archaic module system (glorified copy paste)

• limited standard library

• No builtin array

Page 11: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguagesPython and CJava and C

Why C

IO

Minnehandtering

Sortering

Java and C

• Manual memory management

• Archaic module system (glorified copy paste)

• limited standard library

• No builtin array

Page 12: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

Outline

1 Comparison between LanguagesPython and CJava and C

2 Why CIs there any pros at all?Funksjonskall

3 IOIO functionsprintf() examplescanf() example

4 Minnehandteringpekerestruct

5 SorteringFunksjonspekere

Page 13: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

Is there any pros at all?

• Blazing speed

(produces SEGFAULTS faster than you canimagine)

• Actual threading

(what is Global Interpreter Lock,snakey?)

• bare bones,

(low startup and time and low resource usage.Looking at you Java)

Page 14: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

Is there any pros at all?

• Blazing speed (produces SEGFAULTS faster than you canimagine)

• Actual threading

(what is Global Interpreter Lock,snakey?)

• bare bones,

(low startup and time and low resource usage.Looking at you Java)

Page 15: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

Is there any pros at all?

• Blazing speed (produces SEGFAULTS faster than you canimagine)

• Actual threading

(what is Global Interpreter Lock,snakey?)

• bare bones,

(low startup and time and low resource usage.Looking at you Java)

Page 16: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

Is there any pros at all?

• Blazing speed (produces SEGFAULTS faster than you canimagine)

• Actual threading (what is Global Interpreter Lock,snakey?)

• bare bones,

(low startup and time and low resource usage.Looking at you Java)

Page 17: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

Is there any pros at all?

• Blazing speed (produces SEGFAULTS faster than you canimagine)

• Actual threading (what is Global Interpreter Lock,snakey?)

• bare bones,

(low startup and time and low resource usage.Looking at you Java)

Page 18: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

Is there any pros at all?

• Blazing speed (produces SEGFAULTS faster than you canimagine)

• Actual threading (what is Global Interpreter Lock,snakey?)

• bare bones, (low startup and time and low resource usage.Looking at you Java)

Page 19: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

#include <stdio.h>

int main(){

printf("%s\n", "Hello, World! \n");

return 0;

}

Page 20: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

#include <stdio.h>

int main(){

printf("%s\n", "Hello, World! \n");

return 0;

}

Page 21: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

More pros

• So close to hardware you can actually feel the currentpassing through the processor

• Perfect for reinventing the entire wheel, spokes, hub, andrim

Page 22: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

Outline

1 Comparison between LanguagesPython and CJava and C

2 Why CIs there any pros at all?Funksjonskall

3 IOIO functionsprintf() examplescanf() example

4 Minnehandteringpekerestruct

5 SorteringFunksjonspekere

Page 23: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why CIs there any prosat all?Funksjonskall

IO

Minnehandtering

Sortering

Call by value

alt er call by value, selv pekere

Page 24: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IOIO functionsprintf()examplescanf()example

Minnehandtering

Sortering

Outline

1 Comparison between LanguagesPython and CJava and C

2 Why CIs there any pros at all?Funksjonskall

3 IOIO functionsprintf() examplescanf() example

4 Minnehandteringpekerestruct

5 SorteringFunksjonspekere

Page 25: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IOIO functionsprintf()examplescanf()example

Minnehandtering

Sortering

printf(const char * format, ... )

Skriver ut ... i henhld til format

scanf(const char * format, ...)

Leser input i henhold til format og lagrer til ...

Page 26: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IOIO functionsprintf()examplescanf()example

Minnehandtering

Sortering

Outline

1 Comparison between LanguagesPython and CJava and C

2 Why CIs there any pros at all?Funksjonskall

3 IOIO functionsprintf() examplescanf() example

4 Minnehandteringpekerestruct

5 SorteringFunksjonspekere

Page 27: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IOIO functionsprintf()examplescanf()example

Minnehandtering

Sortering

printf() example

#include <stdio.h>

#define M_PI 3.14159;

double sin(double x);

double cos(double x);

int main(){

double pi = M_PI;

printf("sin(pi/4) = %f -- cos(pi/4) = %f\n", sin(pi/4), cos(pi/4));

return 0;

}

Page 28: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IOIO functionsprintf()examplescanf()example

Minnehandtering

Sortering

printf() example

#include <stdio.h>

#include <math.h>

int main(){

double pi = M_PI;

printf("sin(pi/4) = %f -- cos(pi/4) = %f \n", sin(pi/4), cos(pi/4));

return 0;

}

Page 29: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IOIO functionsprintf()examplescanf()example

Minnehandtering

Sortering

Outline

1 Comparison between LanguagesPython and CJava and C

2 Why CIs there any pros at all?Funksjonskall

3 IOIO functionsprintf() examplescanf() example

4 Minnehandteringpekerestruct

5 SorteringFunksjonspekere

Page 30: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IOIO functionsprintf()examplescanf()example

Minnehandtering

Sortering

scanf() example

#include <stdio.h>

#include <math.h>

int main(){

double pi = M_PI;

printf("sin(pi/4) = %f -- cos(pi/4) = %f \n", sin(pi/4), cos(pi/4));

return 0;

}

Page 31: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

malloc

void *malloc(size t bytes)

Ber operativsystemet om nytt minne og returnerer en peker tildet

Page 32: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

Outline

1 Comparison between LanguagesPython and CJava and C

2 Why CIs there any pros at all?Funksjonskall

3 IOIO functionsprintf() examplescanf() example

4 Minnehandteringpekerestruct

5 SorteringFunksjonspekere

Page 33: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

pekere

• endrer du pa pekeren selv endrer du bare hvor du peker

• derefererer du først endrer du pa det den peker pa

• to mater a dereferere pa: [], *

Page 34: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

malloc eksempel

#include <stdio.h>

#include <stdlib.h>

int main(){

int length;

printf("length: ");

scanf("%i\n",&length );

int * a = malloc(length*sizeof(int));

for (size_t i = 0; i < length; i++) {

scanf("%i ", a+i );

}for (size_t i = 0; i < length; i++) {

printf("%i ", a[i] );

}

return 0;

}

Page 35: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

Outline

1 Comparison between LanguagesPython and CJava and C

2 Why CIs there any pros at all?Funksjonskall

3 IOIO functionsprintf() examplescanf() example

4 Minnehandteringpekerestruct

5 SorteringFunksjonspekere

Page 36: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

struct

egendefinerte datatyper som inneholder variabler.

Page 37: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

struct square

input examples struct square med utskrift og setting

Page 38: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

Mer abstract struct: tabell med lengde

• Pekere vet ikke lengde

• vi kan kombinere pekere og lengde i en struct

og det kanvære lurt a ta med brukt mengde

Page 39: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

Mer abstract struct: tabell med lengde

• Pekere vet ikke lengde

• vi kan kombinere pekere og lengde i en struct

og det kanvære lurt a ta med brukt mengde

Page 40: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

Mer abstract struct: tabell med lengde

• Pekere vet ikke lengde

• vi kan kombinere pekere og lengde i en struct og det kanvære lurt a ta med brukt mengde

Page 41: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

struct Array

struct Array

input example Array

Page 42: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

ekstra funksjoner til Array

• push

• resize

• pop

Page 43: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

push,pop, resize example

Page 44: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

filter

plukk ut positive tall fra en liste

Page 45: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

filterfunksjon

inputex print all numbers larger than 0

Page 46: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandteringpekerestruct

Sortering

fitlerfunksjon

inputex push all numbers larger than 0 to Array

Page 47: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

qsort

void qsort(void *base, size t nel, size t width,

int (*compar)(const void *, const void *));

enkelt, logisk og lett leselig. tar inn startpunkt, antall, breddenpa elementene ... og pakaller ondskapen selv

Page 48: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

qsort

void qsort(void *base, size t nel, size t width,

int (*compar)(const void *, const void *));

enkelt, logisk

og lett leselig. tar inn startpunkt, antall, breddenpa elementene ... og pakaller ondskapen selv

Page 49: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

qsort

void qsort(void *base, size t nel, size t width,

int (*compar)(const void *, const void *));

enkelt, logisk og lett leselig.

tar inn startpunkt, antall, breddenpa elementene ... og pakaller ondskapen selv

Page 50: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

qsort

void qsort(void *base, size t nel, size t width,

int (*compar)(const void *, const void *));

enkelt, logisk og lett leselig. tar inn startpunkt, antall, breddenpa elementene

... og pakaller ondskapen selv

Page 51: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

qsort

void qsort(void *base, size t nel, size t width,

int (*compar)(const void *, const void *));

enkelt, logisk og lett leselig. tar inn startpunkt, antall, breddenpa elementene ... og pakaller ondskapen selv

Page 52: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

Outline

1 Comparison between LanguagesPython and CJava and C

2 Why CIs there any pros at all?Funksjonskall

3 IOIO functionsprintf() examplescanf() example

4 Minnehandteringpekerestruct

5 SorteringFunksjonspekere

Page 53: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

Funksjonspekere

• Ingen lambdafunksjoner.

• Ingen sammenligningsoperatorer eller funksjoner innebygdi objektene.

• bare funksjoner

som vi kan ta adressen til

Page 54: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

Funksjonspekere

• Ingen lambdafunksjoner.

• Ingen sammenligningsoperatorer eller funksjoner innebygdi objektene.

• bare funksjoner som vi kan ta adressen til

Page 55: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

hva ma compar() gjøre?

• parametre, to pekere

• returnerer et tall

Page 56: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

strcmp

int (*compar)(const void *, const void *);

int compar (const void *, const void *);

int strcmp(const char *s1, const char *s2);

Page 57: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

Men vi kan bruke strcmp

int strcmp_indirect( void **a, void **b){

return strcmp((char*)*a,(char*)*b);

}

Page 58: Introduction to Ctorjehoa/ckurs.pdf · Introduction to C Torje Ho˚as Digernes Comparison between Languages Python and C Java and C Why C IO Minneh˚andtering Sortering Outline 1

Introductionto C

Torje HoasDigernes

ComparisonbetweenLanguages

Why C

IO

Minnehandtering

SorteringFunksjonspekere

endelig sortering

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int strcmp_indirect( char **a, char **b){

return strcmp(*a,*b);

}

int main(){

char *words[]={"Ia!","shub-niggurath!","the","Black",

"Goat","in","the","Woods","with","a","Thousand",

"Young",NULL};

size_t length= 0;

while(NULL!=words[length]){length++;}

qsort(words,length,sizeof(char*),strcmp_indirect);

for (size_t index = 0; index < length; index++) {

printf("%s ",words[index]);

}

}