1 Review of Chapter 6: The Fundamental Data Types.

Slides:



Advertisements
Similar presentations
Kernighan/Ritchie: Kelley/Pohl:
Advertisements

POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 CSE1301 Computer Programming Lecture 16 Pointers.
1 Chapter 6 The Fundamental Data Types. 2 Outline  Declarations and expressions  Fundamental data types  Characters and the data type char  The Data.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Chapter 4 (II) Functions and Structured Programming.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1 CSE1301 Computer Programming Lecture 16 Pointers.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
1 Structured Programming in C Welcome to CPSC 206.
Data Types.
Basic Elements of C++ Chapter 2.
Pointers CSE 2451 Rong Shi.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Data Types Declarations Expressions Data storage C++ Basics.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers *, &, array similarities, functions, sizeof.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Basic Data Types & Memory & Representation
The Machine Model Memory
CSE 220 – C Programming Pointers.
BASIC ELEMENTS OF A COMPUTER PROGRAM
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Pointers.
Pointers and Pointer-Based Strings
INC 161 , CPE 100 Computer Programming
Lecture 6 C++ Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Fundamental Data Types
Outline Defining and using Pointers Operations on pointers
Fundamental Data Types
Pointers and Pointer-Based Strings
C++ Programming Basics
Data Type.
DATA TYPES There are four basic data types associated with variables:
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

1 Review of Chapter 6: The Fundamental Data Types

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  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, ……, Range of int: -2 31, ……, -2, -1, 0, 1, 2, ……,

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 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 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 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 Chapter 8 Functions, Pointers, and Storage Classes

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 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 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: , size: 4 [int b] address: , size: 4 Memory address a b

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 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 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 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 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 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 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 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 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: , value: 1 [int b] address: , 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 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 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 #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= , Size=4 [b]: Addr= , Size=4 [p]: Addr= , Size= address Memory a b p

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); } address Memory a b p % a.out Memory Allocation: [a]: Addr= , Size=4 [b]: Addr= , Size=4 [p]: Addr= , Size=4 *p = *p: the value of the variable to which p points

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); } address Memory a b p % a.out Memory Allocation: [a]: Addr= , Size=4 [b]: Addr= , Size=4 [p]: Addr= , Size=4 *p = 7 a = *p: the value of the variable to which p points 3

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); } address Memory a b p % a.out Memory Allocation: [a]: Addr= , Size=4 [b]: Addr= , Size=4 [p]: Addr= , Size=4 *p = 7 a = 3 b = *p: the value of the variable to which p points

#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); } address Memory a b p % a.out Memory Allocation: [a]: Addr= , Size=4 [b]: Addr= , Size=4 [p]: Addr= , Size=4 *p = 7 a = 3 b = 11 Input an integer: 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 and the special address NULL. ostdio.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 occurs scanf(“%d”, p);  Dereferencing: access the value of the variable to which the pointer points by *: *pointer *p = 3;

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 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 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 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 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

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 , main: 3 7 swap: 7 3 main: 7 3 Memory a b 7 3 swap: a swap: b swap: tmp

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 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