50
Copyright ©: Nahrstedt, Angrave, Abdelzaher 1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Embed Size (px)

Citation preview

Page 1: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher 1

C Basics

Tarek Abdelzaher and Vikram Adve

Page 2: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

2

The C Language Spirit

Made by professional programmers for professional programmers

Very flexible, very efficient, very liberal Does not protect the programmers from

themselves. Rationale: programmers know what they are doing even if looks bad enough to deserve a “Darwin award” (see http://www.darwinawards.com/)

UNIX and most “serious” system software (servers, compilers, etc) are written in C.

Can do everything Java and C++ can. It’ll just look uglier in C

Page 3: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

3

Compiler gcc See manual “man” for options man gcc

Preprocessor Compiler Linker

C89 versus C99 C99: inline functions, variable length arrays,

… make – a compilation utility

Google for make files (or GNU Make)

Page 4: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

4

Programming in C

C = Variables + Instructions

Page 5: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

5

Programming in C

C = Variables + Instructions

intchar

float

pointerarray

struct { …}

Page 6: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

6

Programming in C

C = Variables + Instructions

intchar

float

struct { …}…

pointerarray

function callassignment

if

switch…

forwhile

Page 7: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

7

10,000

10,004

10,012

10,016

10,020

Variables

Value1

Value2

Value3

Value4

Value5

x

y

z

p

d

Name (in program text)

Value

Memory

int x;double y;float z;double* p;int d;

Type of each variable(also determines size)

Page 8: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

8

10,000

10,004

10,012

10,016

10,020

Variables

Value1

Value2

Value3

Value4

Value5

x

y

z

p

d

MemoryAddress(“name”at run-time)

Name (in program text)

Value

Memory

int x;double y;float z;double* p;int d;

Type of each variable(also determines size)

Page 9: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

9

10,000

10,004

10,012

10,016

10,020

The “&” Operator:Reads “Address of”

Value1

Value2

Value3

Value4

Value5

x

y

z

p

d

Name

Value

&y

Memory

Page 10: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

10

10,000

10,004

10,012

10,016

10,020

Pointers

Value1

Value2

Value3

10,004

Value5

x

y

z

p

d

Name

Value

A pointer is a variable whose value is a memory address:

p = &y;

Memory

Page 11: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

11

10,000

10,004

10,012

10,016

10,020

The “*” OperatorReads “Location pointed to by”

Value1

Value2

Value3

10,004

Value5

x

y

z

p

d

Name

Value

A pointer is a variable whose value is the address of another:

p = &y;

*p

Memory

Page 12: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

12

What is the Output?

main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);

}

Page 13: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

13

What is the Output?

main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);

}

#@*%!

#@%$!

@*%^

p

q

x

Page 14: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

14

What is the Output?

main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);

}

#@*%!

#@%$!

10

p

q

x

Page 15: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

15

What is the Output?

main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);

}

#@%$!

10

p

q

x

Page 16: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

16

What is the Output?

main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);

}

#@%$!

11

p

q

x

Page 17: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

17

What is the Output?

main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);

}

11

11

p

q

x

Page 18: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

18

Cardinal Rule: Must Initialize Pointers before Using them

int *p;*p = 10;

BAD

Page 19: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

19

Cardinal Rule: Must Initialize Pointers before Using them

int *p;*p = 10;

#@*%!p??

Pointing somewhererandom

Page 20: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

20

Cardinal Rule: Must Initialize Pointers before Using them

int *p;*p = 10;

#@*%!p

#@*%!10

Page 21: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

21

How to Initialize Pointers

Page 22: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

22

How to Initialize Pointers Set pointer equal to address of

known variable

int *p;int x;…p=&x;

Page 23: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

23

How to Initialize Pointers Use malloc()

int *p;…p=(int*) malloc (sizeof (int));

malloc() allocates memory dynamically: “heap”sizeof(T) gives the size of a program type T.

Page 24: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

24

How to Initialize Pointers Copy another initialized pointer

value

int *p, *q;…q = (int*) malloc(sizeof(int));p = q;

Page 25: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

25

How to Initialize Pointers Create an Array

int p[10];

Same as:int *p;p=(int*) malloc (10*sizeof (int));

Page 26: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

26

Arrays

int p[5];

p[0]

p[1]

p[2]

p[3]

p[4]

Name of array (is a pointer)

p

Shorthand:*(p+1) is called p[1]*(p+2) is called p[2]etc..

Page 27: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

27

Example

int y[4];

y[1]=6;

y[2]=2; 62

y[0]

y[1]

y[2]

y[3]

y

Page 28: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

28

Array Name as Pointer What’s the difference between the examples below:

Example 1:int z[8];int *q;q=z;

Example 2:int z[8];int *q;q=&z[0];

Page 29: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

29

Array Name as Pointer What’s the difference between the examples below:

Example 1:int z[8];int *q;q=z;

Example 2:int z[8];int *q;q=&z[0];

NOTHING!!

z (the array name) is a pointer to the beginning of the array, which is &z[0]

Page 30: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

30

Example:

How much is y at the end:int y, x, *p;

x = 20;

*p = 10;

y = x + *p;

Page 31: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

31

Example:

How much is y at the end:int y, x, *p;

x = 20;

*p = 10;

y = x + *p;

BAD!! Dereferencing an unitialized pointerwill likely segfault or overwrite something!

Segfault = unauthorized memory access

Page 32: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

32

Question: What’s the difference betweenint* q;

int q[5];

What’s wrong with:int ptr[2];

ptr[1] = 1;

ptr[2] = 2;

Page 33: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

33

Question: What is the value of b[2] at the end?

int b[3];int* q;

b[0]=48; b[1]=113; b[2]=1;q=b;*(q+1)=2;b[2]=*bb[2]=b[2]+b[1];

Page 34: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

34

Strings (Null-terminated Arrays of char)

Examplechar s[5]; Strings are arrays that contain byte values

ending in a “Null” character to indicate end of string. Do not forget to leave room for the null

character

s[0]

s[1]

s[2]

s[3]

s[4]

s

Page 35: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

35

String and char constants

String constants “wassup” // how many bytes? “c” // how many bytes?

Character constants ‘c’ ‘\0’ // the Null character

Page 36: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

36

String Operations

strcpy strlen strcat strcmp

Page 37: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

37

strcpy, strlenSyntax: strcpy(ptr1, ptr2);

    where ptr1 and ptr2 are pointers to char value = strlen(ptr);

    where value is an integer and    ptr is a pointer to char

Example:int len; char str[15]; strcpy (str, "Hello, world!"); len = strlen(str);

Page 38: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

38

strcpy, strlen

What’s wrong with

char str[5];

strcpy (str, "Hello");

Page 39: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

39

strncpy

Syntax: strncpy(ptr1, ptr2, num);

    where ptr1 and ptr2 are pointers to char num is the number of characters to be copied

Example:int len; char str1[15], str2[15]; strcpy (str1, "Hello, world!"); strncpy (str2, str1, 5);

Page 40: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

40

strncpy

Syntax: strncpy(ptr1, ptr2, num);

    where ptr1 and ptr2 are pointers to char num is the number of characters to be copied

Example:int len; char str1[15], str2[15]; strcpy (str1, "Hello, world!"); strncpy (str2, str1, 5);

Caution: strncpy blindly copies the characters. It does not voluntarily append the string-terminating null character.

Page 41: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

41

strcat Syntax: strcat(ptr1, ptr2);

    where ptr1 and ptr2 are pointers to char

Concatenates the 2ndargument to the 1st (both must be null terminated strings or havoc results!).

char S[25] = "world!"; char D[25] = "Hello, "; strcat(D, S);

Page 42: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

42

strcatExample

What’s wrong with:

char S[25] = "world!";

strcat(“Hello, ”, S);

Page 43: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

43

strcmp Syntax: diff = strcmp(ptr1, ptr2);

    where diff is an integer and     ptr1 and ptr2 are pointers to char

Returns zero if strings are identical (or >0 or <0)

int diff;char s1[25] = "pat"; char s2[25] = "pet"; diff = strcmp(s1, s2);

Page 44: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Formatted I/O

printf, fprintf: Write data to fileprintf( “Lose %d pounds in %d %s\n”,

N, D, “weeks”);

scanf, fscanf: Read data from filescanf( “%d %f %80s\n”,

&N, &F, buf); // buf must be char*

Copyright ©: Nahrstedt, Angrave, Abdelzaher

44

Page 45: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

45

Math: Increment and Decrement Operators

Example 1:int x, y, z, w;y=10; w=2;x=++y;z=--w;

Example 2:int x, y;y=10; w=2;x=y++;z=w--;

Page 46: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

46

Math: Increment and Decrement Operators

Example 1:int x, y, z, w;y=10; w=2;x=++y;z=--w;

Example 2:int x, y;y=10; w=2;x=y++;z=w--;

First increment/decrement then assign resultx is 11, z is 1

First assign then increment/decrementx is 10, z is 2

Page 47: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

47

Math: Increment and Decrement Operators on Pointers

Example 1:int a[2];int number1, number2, *p;a[0]=1; a[1]=10; a[2]=100; p=a;number1 = *p++;number2 = *p;

What will number1 and number2 be at the end?

Page 48: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

48

Math: Increment and Decrement Operators on Pointers

Example 1:int a[2];int number1, number2, *p;a[0]=1; a[1]=10; a[2]=100; p=a;number1 = *p++;number2 = *p;

What will number1 and number2 be at the end?

Hint: ++ increments pointer p not variable *p

Page 49: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

49

Logic: Relational (Condition) Operators

== equal to != not equal to > greater than < less than >= greater than or equal to <= less than or equal to

Page 50: Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve

Copyright ©: Nahrstedt, Angrave, Abdelzaher

50

Logic Example

if (a == b) printf (“Equal.”);

else printf (“Not Equal.”);

Question: what will happen if I replaced the above with:

if (a = b) printf (“Equal.”);

else printf (“Not Equal.”);