Algorithms and Programming Chapter 2: Overview of C ... · Chapter 2: Overview of C General form of...

63
Chapter 2: Overview of C General form of a C program C language elements Maltepe University Computer Engineering Department Algorithms and Programming

Transcript of Algorithms and Programming Chapter 2: Overview of C ... · Chapter 2: Overview of C General form of...

Chapter 2: Overview of C

General form of a C program

C language elements

Maltepe University

Computer Engineering Department

Algorithms and Programming

http://www.ee.hacettepe.edu.tr/~alkar/ELE108

(Info only)

(Info only)

(Info only)

See L3_7_skeleton.C as a template for C programs.

13

16

Statement Comments - Do/Don’t

Say why, don’t paraphrase the code:

NO: /* subtract one from sheep */

sheep = sheep - 1;

YES: /* account for the sheep that

the big bad wolf just ate.*/

sheep = sheep - 1;

17

18

19

20

21

22

23

24

25

26

27

28

See L3_29_C Dilinde Kullanılan Karakter Tipleri.docx and L3_29_C Data Types_Range

of values.docx for more details.

Also see programs L3_29_DataTypesSizeRanges.c and L3_29_StackOverFlow.c

Not 5 but 5.0

Not “A”

29

30

Memory example

Variable declarations in C int i = 12; double gasPrice = 1.799; char bang = ‘!’;

Picture:

i

gasPrice

bang

12

1.799

‘!’

(int)

(double)

(char)

31

32

33

printf( "format-string", expression1, expression2 ... );

See L3_34_Printf_YerTutucuÖrnek.JPG

34

35

Escape Sequences (Character Constants)

Escape Sequence Represents

\a Bell (alert)

\b Backspace

\f Formfeed

\n New line

\r Carriage return

\t Horizontal tab

\v Vertical tab

\' Single quotation mark

\ " Double quotation mark

\\ Backslash

\? Literal question mark

\ ooo ASCII character in

octal notation

\x hh ASCII character in

hexadecimal notation

http://msdn.microsoft.com/en-us/library/h21280bw(v=vs.80).aspx

See L3_36_YertutucuOrnek.c 36

37

Examples

This code:

int x=3;

printf("%d is a prime number.\n", 43);

printf("43 plus 59 in decimal is %d.\n", 43+59);

printf(“Here %s go.", “you\n”);

printf(“\nA character %c a decimal number%d%s", ‘T’,x,”…”);

Produces this output:

43 is a prime number.

43 plus 59 in decimal is 102.

Here you

go.

A character T a decimal number 3... 38

See L3_38_printf1.c, L3_38_printf2.c

• %10.2f _ _ _ _ 1 2 3 . 5 5 double

• %10.4f _ _ 1 2 3 . 5 5 0 0

• %.2f 1 2 3 . 5 5

• %10d _ _ _ _ _ _ _ 4 7 5 int

• %-10d 4 7 5 _ _ _ _ _ _ _

• %10c _ _ _ _ _ _ _ _ _ a char

39

Output Format Examples

See L3_39_BiçimliÇıktı.c program

Do NOT forget & sign! 40

41

space (‘ ’), tab (‘\t’), newline (‘\n’) are “whitespace”

Whitespace is skipped by scanf for int (“%d”), and double

(“%lf”)

This means the user can type spaces before a number

and they are ignored

Not skipped for char input “%c”

each character typed, including spaces, is used

Whitespace

42

•Basic rule:

–% placeholders in the format must match variables

in the input list

– MUST! match one-for-one in number, order, and

type.

int studentID ;

double grade ;

scanf (“%d %lf”, &studentID , &grade ) ;

Multiple Inputs

43

44

#include <stdio.h>

int main()

{

int x;

float y;

double z;

printf("Bir tamsayi ve iki adet de reel sayi girin:");

scanf("%d%f%lf",&x,&y,&z);

printf("x=%d y=%f z=%f",x,y,z);

return 0;

}

Sample – (See L3_44_scanf1.c)

45

#include <stdio.h>

int main()

{

int x;

float y;

double z;

printf("Bir tamsayi ve iki adet de reel sayi girin:");

scanf("%d%f",&x,&y);

printf("x=%d y=%f z=%f",x,y,z);

printf("\nhafizadan aliyor kalan degerleri\n");

scanf("%lf",&z);

printf("x=%d y=%f z=%f",x,y,z);

return 0;

}

Sample – (See L3_44_scanf2.c)

46

Sample – (See L3_44_scanf3.c)

#include <stdio.h>

int main()

{

int sayi;

char harf;

printf("Bir tamsayi ve bir karakter girin: ");

scanf("%d%c",&sayi,&harf);

printf("sayi=%d harf=%c=\n",sayi,harf);

printf("sayi=%d harf=%d=",sayi,harf);

return 0;

}

Karakter okumadan önce sayı okunursa hafızada kalan whitespace

karakterlerini temizlemek gerekir.

47

Sample – (See L3_44_scanf4.c)

#include <stdio.h>

int main()

{

int sayi;

char harf;

printf("Bir tamsayi ve bir karakter girin: ");

scanf("%d %c",&sayi,&harf);//Karakter okumadan önce

//boşluk ekleyip hafiza temizlenir

printf("sayi=%d harf=%c",sayi,harf);

return 0;

}

See slide 10. 48

49

50

51

52

53

54

55

56

57

58

59

60

61

DO

Use plenty of comments - but not too many

Use white space

Use indentation

Choose descriptive names

Use named constants

DON’T

be terse, tricky

place speed above correctness, simplicity

use “magic numbers”

62

Style Summary: Clarity is Job #1

63