Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3.

Slides:



Advertisements
Similar presentations
Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
CSE202: Lecture 10AThe Ohio State University1 Numerical Error.
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Note that the methods we used to convert from decimal.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
CENG 311 Machine Representation/Numbers
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Chapter 2 Basic Data Types.
Computer Science 111 Fundamentals of Programming I Number Systems.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
First Program.  The type of all data used in a C program must be specified  A data type is a description of the data being represented ▪ That is, a.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Floating-Point (real) numbers Consider the Number:4,
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 2: Basic C program structure Data in C: Data types,
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Java Data Types Assignment and Simple Arithmetic.
CSC 107 – Programming For Science. The Week’s Goal.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Characters vs. Numbers Adding in Binary: There are only.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Strings Chapter 5.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1.2 Primitive Data Types and Variables
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Structured Data Objects (Structs). array Recall our definition of an array: fixedcontiguousall of the same data type an array is a fixed number of contiguous.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Structured Data Objects (Structs) Chapter 7.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User Interaction and Variables
Chapter 2 Basic Computation
EPSII 59:006 Spring 2004.
Numeric Arrays Numeric Arrays Chapter 4.
Other Kinds of Arrays Chapter 11
One-Dimensional Array Introduction Lesson xx
Introduction to Abstract Data Types
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Overloading functions
B065: PROGRAMMING Variables 1.
B065: PROGRAMMING Variables 2.
Presentation transcript:

Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3

Page 2 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation What you MUST know before we start: What basic data types there are: Signed/Unsigned Characters Signed/Unsigned Shorts/Integers/Longs The difference between characters & numbers How the sign-bit is used How negative values are stored What precision and magnitude are and how they affect real numbers (Remember: The topics in this course build on each other) float/double How many bits each data type requires

Page 3 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Storing Characters in RAM We know that characters are really numbers: signed char (or char, by default) on 8-bits: 8-bits = 1-byte = X X X X X X X X Sign bit Value unsigned char Value Regardless of whether signed or unsigned, the data type char requires 1-byte of storage per variable

Page 4 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Consider the following C Declaration char = ‘f’, c = 87; char a, b = ‘f’, c = 87; What is the effect of the command ??? 1. We are reserving 3-bytes of storage 2. We are associating each location with a variable name (LOCATIONS a, b, and c) 3. We are initializing location b with the Character ‘f’ = ASCII 102 = We are placing the value 87 (= ) into location c

Page 5 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Where in RAM will we find locations a, b, and c ??? We Don’t Know: Address allocations are made at RUN-TIME and are based on available locations. Assume that at run-time, we find that Addresses 5010, 5014, and 5015 are Available: Variable a will be assigned to address 5010 Variable b will be assigned to address 5014 Variable c will be assigned to address 5015 How Will this appear in RAM ???

Page 6 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation (a) (b) (c) Given: char = ‘f’, c = 87; char a, b = ‘f’, c = 87; Where: Variable a => 5010: Unassigned Variable b => 5014: ‘f’ = = Variable c => 5015: =

Page 7 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Why Does Location a (address 5010) contain something?? We did NOT initialize the variable. previously Whatever was previously stored at that location is still there. If we were now (after variable allocation) to issue the command: printf (“%c %d”, a, a); We would produce the output:  -31 That Makes NO Sense at ALL !! Let’s Examine why this would occur

Page 8 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Location 5010 (variable a) contains: Because location 5010 contains a (signed) char, the numeric value is: Left-most bit = ‘1’: Value is negative: COMPLIMENT Since we are using 2’s Compliment: ADD 1: = -( ) = -( ) = -31

Page 9 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation That still doesn't explain why the ASCII character  is printed !!! Even though the numeric value is -31, The value of location a is checked against the ASCII table as if it were an unsigned char: Evaluates to = = 225 Which corresponds to the ASCII Character 

Page 10 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation I don’t know if I believe – I know about your propensity to lie to us !!! OK – here’s a program I wrote: And here’s the output I received:

Page 11 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation What if we try to store an illegal number, say 837, into a character location in RAM ??? char = 837; char illegal = 837; printf (“%c %d”, illegal, illegal); If we were to issue the commands: We would produce the output: E = WHY ??? BUT BUT requires 10-bits of storage WE RESERVED ONLY 8-BITS !!!

Page 12 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Because we reserved only 1-byte, ONLY the right- most 8-bits will be stored: = = = 69 Which Corresponds to the ASCII Character ‘E’

Page 13 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Integers (c data type int) Integers require 2 CONTIGUOUS bytes (16-bits) of storage Consider the c declaration: int int x = ‘W’, y, z = 5487; In Fact we are (once again): (LOCATIONS x, y, and z) 3. Initializing location x with ‘W’ (= ASCII 87 = = on 16-bits) 4. Initializing location z with 5487 ( = = on 16-bits) (of course we all know it is really 4 Contiguous bytes) 1. Requesting 6-bytes (2 per variable) of RAM be reserved 2. Associating each variable name with a reserved location

Page 14 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Let’s Assume locations 7212, 7213, 7214, 7216, and 7219 through 7250 Are available: Variable x is assigned address 7212 (and 7213) Variable y is assigned address 7219 (and 7220) Variable z is assigned address 7221 (and 7222) Why aren’t addresses 7214 and 7216 used??? contiguous Because we need 2 contiguous bytes of storage for integers: We can’t use location 7214 because location 7215 is NOT available We can’t use location 7216 because location 7217 is NOT available Looking at RAM, We might see:

Page 15 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation int int x = ‘W’, y, z = 5487; x = y is unassigned z = x stored at address 7212 (and 7213) y stored at address 7219 (and 7220) z stored at address 7221 (and 7222) (x) (x) (y) (y) (z) (z)

Page 16 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Once again, Notice that there is ‘garbage’ in location y (Addresses 7219 & 7220): = = = 6,502 Which is the output we would obtain if we issued the command: printf (“%d”, y);

Page 17 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Lest we forget, since we know that integers are really stored on 32-bits: int int x = ‘W’, y, z = 5487; x = y is unassigned z = x stored at address 7212 (to 7215) y stored at address 7219 (to 7222) z stored at address 7223 (to 7226) (x) (x) (x) (x) (y) (y) (y)7222 (y) (x) (x) (x) (x)

Page 18 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation What would happen if we tried to print an integer as as an ASCII character ??? Depends: int = 104; int anumber = 104; printf (“%c”, anumber); If we issue the commands: The output will be: h Which is the ASCII character for decimal value 104 int = 6502; int anumber = 6502; printf (“%c”, anumber); If we issue the commands: The output will be: f WHAT ???

Page 19 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Don’t forget how the integer 6502 was stored (on 16-bits): right-most When we try to print out a character according to the ASCII tables, we consider ONLY 8-bits (the right-most 8-bits) = = = 102 Which corresponds to the ASCII character ‘f’

Page 20 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation What if an illegal value is entered?? The same situation occurs as did when we entered an illegal value for the data type char: If we make the declaration: int = ; int badnumber = ; Since: 52, = Then: - 52, = ’s Comp = = = 1 3,102 Which is the output produced by the Statement: printf (“%d”, badnumber); 2’s Comp.

Page 21 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation I don’t know if I believe anymore -- About anything!!! OK – let’s write another program – but instead of using the data type int, let’s use the data type short WHY ???? Because, as we know, the data type short still requires 16-bits (2-bytes) of storage (As integers used to) We will NOT have to change our illustration

Page 22 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Once again, here is the program I wrote: And here is the output I received:

Page 23 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation One more quick example: Suppose we make the declaration: int = 72925; int badnumber = 72925; Where: = Requiring 17-bits Since an integer is stored on 16-bits: = = = 7,389 Which is the output produced by the Statement: printf (“%d”, badnumber);

Page 24 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Other Integers: Data Type unsigned int Bits Required 16 (2-bytes) Legal Values 0 through 65,535 (No sign bit) long Or signed long 32 (4-bytes) -2,147,483,648 through 2,147,483,647 unsigned long 32 (4-bytes) 0 through 4,294,967,295 How would the data type long appear in RAM ???

Page 25 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Long Integers (c data type long) Integers require 4 CONTIGUOUS bytes (32-bits) of storage Consider the c declaration: long long l1 = ‘3’, l2; In Fact we are (once again): 1. Requesting 8-bytes (4 per variable) of RAM be reserved 2. Associating each variable name with a reserved location (LOCATIONS l1, and l2) 3. Initializing location l1 with ‘3’ ( = ASCII 51 = = on 32-bits) (REMEMBER: The data types int and long are now the same)

Page 26 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Let’s again assume locations 9011 through 9017, and 9021 through 9076 Are available: Variable l1 is assigned address 9011 (through 9014) Variable l2 is assigned address 9021 (through 9024) Why aren’t addresses 9015 through 9017 used??? contiguous Because we need 4 contiguous bytes of storage for longs: If we were to try and store variable l2 at location 9015, we would need addresses 9016 through 9018 to be available also Looking at RAM, We might see:

Page 27 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation long long l1 = ‘3’, l2; l1 = l2 is unassigned l1 stored at address 9011 (through 9014) l2 stored at address 9021 (through 9024)

Page 28 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation What ‘Garbage’ will we find at location l1 ??? At addresses 9021 through 9024, we find: (On 32-bits) Since the left-most bit = ‘1’ ( ==> the number is negative) we must compliment: = -( )21) = -(8,388, ,048, , , , , , ) = 9,493,870 -9,493,870 (1’s Comp.) (2’s Comp.)

Page 29 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Floating-point numbers (real numbers) data type float: CONTIGUOUS 4 CONTIGUOUS bytes (32-bits) per variable 7 decimals of precision Sometimes also referred to as single precision reals data type double: CONTIGUOUS 8 CONTIGUOUS bytes (64-bits) per variable ANSI: 10 decimals of precision data type long double: CONTIGUOUS 16 CONTIGUOUS bytes (128-bits) per variable ANSI: 10 decimals of precision

Page 30 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation float float f1 = , f2; f1 stored at address 8910 (through 8913) f2 stored at address 8914 (through 8917) RAM Storage ??? Available: Addresses 8910 through 8993

Page 31 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation So what do we need to do ?? Make sure you THOUROGHLY understand ALL of the concepts covered in these slides Answer ALL of the relevant questions on the Review Page Submit your References Submit your Question(s) Look at the Bits/Bytes/ASCII C/C++ Programming Assignment (it’s not due yet, but it can’t hurt to look at it) ??? Any Questions ??? (Please!!) Get ready for Quiz It’s coming

Page 32 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation