Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Review of Chapter 6: The Fundamental Data Types.

Similar presentations


Presentation on theme: "1 Review of Chapter 6: The Fundamental Data Types."— Presentation transcript:

1 1 Review of Chapter 6: The Fundamental Data Types

2 2  Declarations  All variables must be declared before they can be used. Tell the compiler to set aside an appropriate amount of space in memory to hold values of variables. Different machine instructions are used when an operator applied on different data types  Declarations enable the compiler to instruct the machine to perform specified operations correctly. Declarations and Expressions Declarations:

3 3  Expressions  Meaningful combinations of constants, variables, and function calls.  Most expressions have both a value and a type  Examples: Declarations: int a =12, b=10, c, d; Function min(int x, int y) exp1, function call min(var1, var2):min(a,b) exp2, var * constant:a*12 exp3, function call min(exp1, exp2):min(min(a,b), a*12) Declarations and Expressions Declarations:

4 4 The Fundamental Data Types  Integral types  The types that can be used to hold integer values. Characters: char, signed char, unsigned char short, int, long, unsigned short, unsigned, unsigned long  Floating types  The types that can be used to hold real values. float, double, long double

5 5 Characters and the data Type char  Characters:  Each char is stored in one byte of memory  Variables of any integral type can be used to represent characters. char, int  When a variable is used to read in characters and a test must be must be made for EOF, while ((c=getchar())!=EOF){ putchar(c); } The variable should be of type int, not char

6 6 The Fundamental Data Types  Integral types  The types that can be used to hold integer values. Characters: char, signed char, unsigned char short, int, long, unsigned short, unsigned, unsigned long  Floating types  The types that can be used to hold real values. float, double, long double

7 7 The Data Type int and the integral types  short, int, long  storage provided for each type: short <= int <= long  signed/unsigned  The integer values stored in an unsigned variable have no sign.  Example, If a variable of type unsigned int is stored in 4 bytes, Range of unsigned: 0, 1, ……, 2 32 -1 Range of int: -2 31, ……, -2, -1, 0, 1, 2, ……, 2 31 -1

8 8 The Fundamental Data Types  Integral types  The types that can be used to hold integer values. Characters: char, signed char, unsigned char short, int, long, unsigned short, unsigned, unsigned long  Floating types  The types that can be used to hold real values. float, double, long double

9 9 The sizeof Operator  sizeof(object)  Returns an integer that represents the number of bytes needed to store the object in memory.  An object can be a type, sizeof(int) an expression, sizeof(a+b) an array or structure type (introduced later)  sizeof(….) is an operator, not a function If sizeof is being applied to a type, parentheses are required, oexample: sizeof(int) Otherwise, parentheses are optional, oexample: sizeof a+b

10 10 Mathematical Functions  Mathematical Functions  There are no built-in mathematical functions in C language  Functions are available in the mathematics library.  How to use the functions in the mathematics library? In the code: #include Compile: gcc –lm f.c

11 11 Conversions and Casts  Conversions and Casts  Conversions can occur when the operands of a binary operator are evaluated. Example: int i, float f; expression i+f oi is promoted to a float oThe expression i+f as a whole has type float.  Cast operator (type) Example: int a; double b; b = (double) a;

12 12 Chapter 8 Functions, Pointers, and Storage Classes

13 13 Introduction Declarations tell the compiler to set aside an appropriate amount of space in memory to hold values associated with variables. Question: Write a code to check the memory allocated to a variable?  The address of the memory allocated to the variable.  The size of the memory allocated to the variable.

14 14 Introduction #include int main(void){ int a, b; /************************************************ print out the address and size of memory allocated to a ************************************************/ /************************************************ print out the address and size of memory allocated to b ************************************************/ }

15 15 Given a variable a, how to check the memory allocated to a?  The address of the memory allocated to a.  The size of the memory allocated to a. Introduction Example: scanf(“%d”, &a);  %d: format  &a: the address of variable a Address operator: &a sizeof(a) : returns an integer that represents the number of bytes needed to store variable a in memory.

16 16 Introduction #include int main(void){ int a,b; /* print out address and size of memory allocated to a */ /*print out address and size of memory allocated to b */ } printf("[int a] address: %u, size: %d\n", &a, sizeof(a)); printf("[int b] address: %u, size: %d\n", &b, sizeof(b)); % a.out [int a] address: 4290705268, size: 4 [int b] address: 4290705264, size: 4 Memory 4290705271 4290705270 4290705269 4290705268 4290705267 4290705266 4290705265 4290705264 4290705263 4290705262 4290705261 address a b

17 17 Introduction #include int main(void){ int a,b; /* print out address and size of memory allocated to a */ /*print out address and size of memory allocated to b */ } printf("[int a] address: %u, size: %d\n", &a, sizeof(a)); printf("[int b] address: %u, size: %d\n", &b, sizeof(b)); Can we use a variable p to store the address, such as &a and &b? If yes, how?  What is the data type of p?  How to declare variable p?  How to assign an address to p?  How to get the value at the address stored in p? Yes, pointer

18 18 Outline  Pointer  Pointers to void  Call-by-Reference  Scope Rules  Storage Classes  Static External Variables  Default Initialization  Function Declarations and Definitions  The Type Qualifiers const and volatile

19 19 Pointer  Pointers:  Pointer variables can be declared in programs and then used to take addresses as values  Example: int *p; int i; declares p to be type “pointer to int”: othe address stored in p is an address to an int variable p = &i; oThe compiler decides what address to use to store the value of the variable i. op contains the address of i; p points to i,

20 20 Pointer #include int main(void){ int a,b; /* print out address and size of memory allocated to a */ /*print out address and size of memory allocated to b */ } printf("[int a] address: %p, size: %u\n", &a, sizeof(a)); printf("[int b] address: %p, size: %u\n", &b, sizeof(b)); Can we use a variable p to store the address, such as &a and &b? If yes, how?  What is the data type of p?  How to declare variable p?  How to assign an address to p?  How to get the value at the address stored in p? pointer

21 21 Pointer  How to declare a pointer? Examples: int *pi1, *pi2, i; opi1: a pointer to int opi2: a pointer to int oi:int How to declare a pointer named pb to double? double *pb; DataType *pointer1, *pointer2;

22 22 Pointer  How to assign address to a pointer? where address can be the address of a variable or another pointer. int *pi1, *pi2, i; pi1 = &i; /* pi1 contains the address of variable i */ /* pi1 points to i */ pi2 = pi1; /* pi2 is assigned the value of pi1, */ /* that is, the address of variable i */ /* pi2 points to i */ pointer = address;

23 23 #include int main(){ int *pi1, *pi2, i; pi1 = &i;/* pi1 points to i */ pi2 = pi1; /* pi2 points to i */ i = 5; printf("%d %d %d\n", i, *pi1, *pi2); } Pointer  How to access the value stored at the address contained in a pointer? Dereference, or indirection, Operator * o*p returns the value of the variable to which p points *pointer *pi1: the value of the variable to which pi1 points *pi2: the value of the variable to which pi2 points % a.out 5 5 5

24 24 Pointer #include int main(void){ int a,b; /* print out address and size of memory allocated to a */ /*print out address and size of memory allocated to b */ } printf("[int a] address: %p, size: %u\n", &a, sizeof(a)); printf("[int b] address: %p, size: %u\n", &b, sizeof(b)); Can we use a variable p to store the address, such as &a and &b? If yes, how?  What is the data type of p?  How to declare the p?  How to store the address in p?  How to get the value located at the address stored p?

25 25 Pointer #include int main(void){ int a =1,b =2; int *pa, *pb; pa = &a; pb = &b; printf("[int a] address: %u, value: %d \n", pa, *pa); printf("[int b] address: %u, value: %d \n", pb, *pb); } %a.out [int a] address: 4290705268, value: 1 [int b] address: 4290705264, value: 2  Declaration of a pointer: DataType *pointer;  Assign address to a pointer: pointer = address;  Access the value of the variable to which the pointer points: *pointer

26 26 Pointer  Pointers:  Pointer variables can be declared in programs and then used to take addresses as values Declaration oDataType *pointer1, *pointer2;  double *pb, x; Assignment opointer = address;  pb = &x; Dereferencing oAccess the value of the variable to which the pointer points by dereference operator *: *pointer  *pb = 3;  What is the value of x?

27 27 Pointer  Pointers:  What is the legal range of values of any pointer? a set of positive integers that are interpreted as machine addresses on a particular C system, and the special address 0 ostdio.h: #define NULL 0 oNULL can be used to represent false while(NULL) printf(“loop\n”);

28 28 #include int main(void){ int a, b; int *p; printf("Memory Allocation:\n"); printf("[a]: Addr=%u, Size=%d\n", &a, sizeof(a)); printf("[b]: Addr=%u, Size=%d\n", &b, sizeof(b)); printf("[p]: Addr=%u, Size=%d\n", &p, sizeof(p)); a = b = 7; p = &a; printf("*p = %d\n", *p); *p = 3; printf("a = %d\n", a); p = &b; *p = 2 * *p - a; printf("b = %d\n", b); p = &a; printf("Input an integer: "); scanf("%d", p); } % a.out Memory Allocation: [a]: Addr=4290705268, Size=4 [b]: Addr=4290705264, Size=4 [p]: Addr=4290705260, Size=4 4290705268 4290705264 4290705260 address Memory a b p

29 29 #include int main(void){ int a, b; int *p; printf("Memory Allocation:\n"); printf("[a]: Addr=%u, Size=%d\n", &a, sizeof(a)); printf("[b]: Addr=%u, Size=%d\n", &b, sizeof(b)); printf("[p]: Addr=%u, Size=%d\n", &p, sizeof(p)); a = b = 7; p = &a; printf("*p = %d\n", *p); *p = 3; printf("a = %d\n", a); p = &b; *p = 2 * *p - a; printf("b = %d\n", b); p = &a; printf("Input an integer: "); scanf("%d", p); } 4290705268 4290705264 4290705260 address Memory a b p % a.out Memory Allocation: [a]: Addr=4290705268, Size=4 [b]: Addr=4290705264, Size=4 [p]: Addr=4290705260, Size=4 *p = 7 7 7 4290705268 *p: the value of the variable to which p points

30 30 #include int main(void){ int a, b; int *p; printf("Memory Allocation:\n"); printf("[a]: Addr=%u, Size=%d\n", &a, sizeof(a)); printf("[b]: Addr=%u, Size=%d\n", &b, sizeof(b)); printf("[p]: Addr=%u, Size=%d\n", &p, sizeof(p)); a = b = 7; p = &a; printf("*p = %d\n", *p); *p = 3; printf("a = %d\n", a); p = &b; *p = 2 * *p - a; printf("b = %d\n", b); p = &a; printf("Input an integer: "); scanf("%d", p); } 4290705268 4290705264 4290705260 address Memory a b p % a.out Memory Allocation: [a]: Addr=4290705268, Size=4 [b]: Addr=4290705264, Size=4 [p]: Addr=4290705260, Size=4 *p = 7 a = 3 7 7 4290705268 *p: the value of the variable to which p points 3

31 31 #include int main(void){ int a, b; int *p; printf("Memory Allocation:\n"); printf("[a]: Addr=%u, Size=%d\n", &a, sizeof(a)); printf("[b]: Addr=%u, Size=%d\n", &b, sizeof(b)); printf("[p]: Addr=%u, Size=%d\n", &p, sizeof(p)); a = b = 7; p = &a; printf("*p = %d\n", *p); *p = 3; printf("a = %d\n", a); p = &b; *p = 2 * *p - a; printf("b = %d\n", b); p = &a; printf("Input an integer: "); scanf("%d", p); } 4290705268 4290705264 4290705260 address Memory a b p % a.out Memory Allocation: [a]: Addr=4290705268, Size=4 [b]: Addr=4290705264, Size=4 [p]: Addr=4290705260, Size=4 *p = 7 a = 3 b = 11 7 7 4290705268 *p: the value of the variable to which p points 3 4290705264 11

32 32 4290705264 #include int main(void){ int a, b; int *p; printf("Memory Allocation:\n"); printf("[a]: Addr=%u, Size=%d\n", &a, sizeof(a)); printf("[b]: Addr=%u, Size=%d\n", &b, sizeof(b)); printf("[p]: Addr=%u, Size=%d\n", &p, sizeof(p)); a = b = 7; p = &a; printf("*p = %d\n", *p); *p = 3; printf("a = %d\n", a); p = &b; *p = 2 * *p - a; printf("b = %d\n", b); p = &a; printf("Input an integer: "); scanf("%d", p); } 4290705268 4290705264 4290705260 address Memory a b p % a.out Memory Allocation: [a]: Addr=4290705268, Size=4 [b]: Addr=4290705264, Size=4 [p]: Addr=4290705260, Size=4 *p = 7 a = 3 b = 11 Input an integer: 7 7 4290705268 3 11 2 scanf(format, addr): the input is placed at the address specified by addr. 2

33 33 Pointer  Summary  Pointer variables can be declared in programs and then used to take addresses as values  Legal range of values of any pointer: a set of positive integers that are interpreted as machine addresses and the special address NULL. ostdio.h: #define NULL 0

34 34 Pointer  Summary (cont’d)  Declaration: dataType *pointer1, *pointer2; double *pb, x;  Assignment: pointer = address; pb = &x;  Addressing: a pointer can be used in a place where address occurs scanf(“%d”, p);  Dereferencing: access the value of the variable to which the pointer points by *: *pointer *p = 3;

35 35 Outline  Pointer  Pointers to void  Call-by-Reference  Scope Rules  Storage Classes  Static External Variables  Default Initialization  Function Declarations and Definitions  The Type Qualifiers const and volatile

36 36 Pointers to void  In ANSI C, one pointer can be assigned to another only when  they both have the same type,  or when one of them is of type pointer to void void * can be considered as a generic pointer type  NULL can be assigned to any pointer.  Example: int *p; double *q; void *v; p = 0; p = NULL; p = (int *) 1; p = v = (int *) q; p = q; p =1; /* legal */ /* illegal */

37 37 Outline  Pointer  Pointers to void  Call-by-Reference  Scope Rules  Storage Classes  Static External Variables  Default Initialization  Function Declarations and Definitions  The Type Qualifiers const and volatile

38 38 Call-by-Reference  Call-by-value  Function Invocation fun_name(exp1, exp2);  All arguments are passed call-by-value Each argument is evaluated, and its value is used locally in place of the corresponding formal parameter. If a variable is passed to a function, the stored value of that variable in the calling environment is not changed.

39 39 Call-by-Reference #include void swap(int, int); int main(void){ int a=3, b=7; printf("main: %d %d\n", a, b); swap(a,b); printf("main: %d %d\n", a, b); return 0; } void swap(int a, int b){ int tmp; tmp = a; a = b; b = tmp; printf("swap: %d %d\n", a, b); } % a.out main: 3 7 swap: 7 3 main: 3 7 Memory a b 7 3 swap: a swap: b swap: tmp 7 3 3 7 3

40 40 Call-by-Reference #include void swap(int*, int* ); int main(void){ int a=3, b=7; printf("Addr %u, %u\n", &a,&b); printf("main: %d %d\n", a, b); swap(&a,&b); printf("main: %d %d\n", a, b); return 0; } void swap(int *a, int *b){ int tmp; tmp = *a; *a = *b; *b = tmp; printf("swap: %d %d\n", *a, *b); } % a.out Addr 4290705268, 4290705264 main: 3 7 swap: 7 3 main: 7 3 Memory a b 7 3 swap: a swap: b swap: tmp 3 4290705268 4290705264 4290705268 4290705264 7 3

41 41 Call-by-Reference  Call-by-reference  How to modify the values of the variables referred to in the argument list? Declaring a function parameter to be a pointer Using the pointer in the function body Passing an address as an argument when the function is called

42 42 Call-by-Reference #include void swap(int*, int* ); int main(void){ int a=3, b=7; printf("Addr %u, %u\n", &a,&b); printf("main: %d %d\n", a, b); swap(&a,&b); printf("main: %d %d\n", a, b); return 0; } void swap(int *a, int *b){ int tmp; tmp = *a; *a = *b; *b = tmp; printf("swap: %d %d\n", *a, *b); } How to modify the values of the variables referred to in the argument list?  Declaring a function parameter to be a pointer  Using the pointer in the function body  Passing an address as an argument when the function is called


Download ppt "1 Review of Chapter 6: The Fundamental Data Types."

Similar presentations


Ads by Google