1 review of chapter 6: the fundamental data types
Post on 22-Dec-2015
221 Views
Preview:
TRANSCRIPT
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 typesDeclarations enable the compiler to instruct the machine to perform specified operations correctly.
Declarations and Expressions Declarations:
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
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
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
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
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, ……, 232-1 Range of int: -231, ……, -2, -1, 0, 1, 2, ……,
231-1
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
The sizeof Operatorsizeof(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 functionIf sizeof is being applied to a type,
parentheses are required, o example: sizeof(int)
Otherwise, parentheses are optional,o example: sizeof a+b
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 <math.h>Compile: gcc –lm f.c
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+fo i is promoted to a float o The expression i+f as a whole has type float.
Cast operator (type)Example:
int a; double b;b = (double) a;
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
Introduction
#include <stdio.h>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
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
Introduction#include <stdio.h>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
42907052714290705270429070526942907052684290705267429070526642907052654290705264429070526342907052624290705261
address
a
b
17
Introduction#include <stdio.h>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
Outline
PointerPointers to voidCall-by-ReferenceScope RulesStorage ClassesStatic External VariablesDefault InitializationFunction Declarations and DefinitionsThe Type Qualifiers const and volatile
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”:
o the address stored in p is an address to an int variable
p = &i;o The compiler decides what address to use to store
the value of the variable i.o p contains the address of i; p points to i,
20
Pointer#include <stdio.h>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
Pointer
How to declare a pointer?
Examples: int *pi1, *pi2, i;
o pi1: a pointer to into pi2: a pointer to into i: int
How to declare a pointer named pb to double?double *pb;
DataType *pointer1, *pointer2;
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
#include<stdio.h>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.out5 5 5
24
Pointer#include <stdio.h>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
Pointer#include <stdio.h>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
Pointer
Pointers: Pointer variables can be declared in programs
and then used to take addresses as valuesDeclaration
o DataType *pointer1, *pointer2; double *pb, x;
Assignmento pointer = address;
pb = &x;Dereferencing
o Access the value of the variable to which the pointer points by dereference operator *: *pointer
*pb = 3; What is the value of x?
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, andthe special address 0
o stdio.h: #define NULL 0o NULL can be used to represent false
while(NULL) printf(“loop\n”);
28
#include <stdio.h>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.outMemory Allocation:[a]: Addr=4290705268, Size=4[b]: Addr=4290705264, Size=4[p]: Addr=4290705260, Size=4
4290705268
4290705264
4290705260
addressMemory
a
b
p
29
#include <stdio.h>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
addressMemory
a
b
p
% a.outMemory 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
#include <stdio.h>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
addressMemory
a
b
p
% a.outMemory Allocation:[a]: Addr=4290705268, Size=4[b]: Addr=4290705264, Size=4[p]: Addr=4290705260, Size=4*p = 7a = 3
7
7
4290705268
*p: the value of the variable to which p
points
3
31
#include <stdio.h>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
addressMemory
a
b
p
% a.outMemory Allocation:[a]: Addr=4290705268, Size=4[b]: Addr=4290705264, Size=4[p]: Addr=4290705260, Size=4*p = 7a = 3b = 11
7
7
4290705268
*p: the value of the variable to which p
points
3
4290705264
11
32
4290705264
#include <stdio.h>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
addressMemory
a
b
p
% a.outMemory Allocation:[a]: Addr=4290705268, Size=4[b]: Addr=4290705264, Size=4[p]: Addr=4290705260, Size=4*p = 7a = 3b = 11Input an integer:
7
7
4290705268
3
11
2
scanf(format, addr): the input is placed at the address specified by addr.
2
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 andthe special address NULL.
o stdio.h: #define NULL 0
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 occursscanf(“%d”, p);
Dereferencing: access the value of the variable to which the pointer points by *: *pointer*p = 3;
35
Outline
PointerPointers to voidCall-by-ReferenceScope RulesStorage ClassesStatic External VariablesDefault InitializationFunction Declarations and DefinitionsThe Type Qualifiers const and volatile
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 *//* legal *//* legal *//* legal *//* illegal *//* illegal */
37
Outline
PointerPointers to voidCall-by-ReferenceScope RulesStorage ClassesStatic External VariablesDefault InitializationFunction Declarations and DefinitionsThe Type Qualifiers const and volatile
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
Call-by-Reference#include <stdio.h>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.outmain: 3 7swap: 7 3main: 3 7
Memory
a
b 7
3
swap: a
swap: b
swap: tmp
7
3
3
7
3
40
Call-by-Reference#include <stdio.h>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.outAddr 4290705268, 4290705264main: 3 7swap: 7 3main: 7 3
Memory
a
b 7
3
swap: a
swap: b
swap: tmp 3
4290705268
4290705264
4290705268
4290705264
7
3
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
pointerUsing the pointer in the function bodyPassing an address as an argument when
the function is called
42
Call-by-Reference#include <stdio.h>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 pointerUsing the pointer in the function bodyPassing an address as an argument when the function is called
top related