C Data Types Chapter 7 And other material. Representation long (or int on linux) Two’s complement representation of value. 4 bytes used. (Where n = 32)

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Lecture 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Instructor: Sanchita Mal-Sarkar Course: CIS 265
Chapter 2 types, variables, methods Pages Horstmann.
Basic Elements of C++ Chapter 2.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Introduction to Programming
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on.
Copyright © – Curt Hill Types What they do.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Unit 6 Data Types and Arrays. Key Concepts Explicit and automatic conversion ASCII Enumerated types Function parameters Arrays Loops and arrays Passing.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CSCI206 - Computer Organization & Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 7: User-Defined Functions II
Chapter 7: Expressions and Assignment Statements
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Chapter 7: Expressions and Assignment Statements
Representation of data types
Chapter 2 - Introduction to C Programming
CSCI206 - Computer Organization & Programming
Basic Elements of C++ Chapter 2.
Introduction to Abstract Data Types
Review for Final Exam.
Introduction to C Programming
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Review for Final Exam.
Presentation transcript:

C Data Types Chapter 7 And other material

Representation long (or int on linux) Two’s complement representation of value. 4 bytes used. (Where n = 32) [ , ] INT_MAXINT_MIN #include limits.h

Representation (cont.) float 4 bytes used. #include float.h On my machine, linux: FLT_MIN= FLT_MAX= On my laptop, Windows Xp Pro: FLT_MIN= FLT_MAX=

Representation (cont.) double 8 bytes used. #include float.h On my machine, linux: DBL_MIN= e-308 DBL_MAX= e+308 On my laptop, Windows Xp Pro: DBL_MIN= e-308 DBL_MAX= e+308

C Scalar Types Simple types char int float double Scalar, because only one value can be stored in a variable of each type.

Check Inside Your Program Don’t depend on your assumptions for size. Use the internal variables INT_MAX, INT_MIN to verify what you believe to be true. Otherwise, you’ll overflow a variable. i = INT_MAX; printf(“%d %d\n”, i, i+1); // What prints?

Check Inside Your Program Don’t depend on your assumptions for size. Use the internal variables INT_MAX, INT_MIN to verify what you believe to be true. Otherwise, you’ll overflow a variable. i = INT_MAX; printf(“%d %d\n”, i, i+1); // What prints?

Numerical Inaccuracies int sum = 0; for(i=0; i<1000; i++) sum = sum ; printf("sum times = %f\n", sum); What prints?

Numerical Inaccuracies float sum = 0.0; for(i=0; i<1000; i++) sum = sum ; printf("sum times = %f\n", sum); What prints? sum times = ???

Floating Point Must contain a decimal point (0.0, 12.0, -0.01) Can use scientific notation e e-18

char data type One byte per character. Collating sequence ‘a’ < ‘b’ < ‘c’ < ‘d’ < … ‘A’ < ‘B’ < ‘C’ < ‘D’ < … ‘0’ < ‘1’ < ‘2’ < ‘3’ < … But ‘a’ < ‘A’ or ‘A’ < ‘a’ ??? Not for sure!

User Defined Types (typedef) This is how you can expand the types available to a particular program. typedef type-declaration; E.g. typedef int count; Defines a new type named count that is the same as int. count flag = 0; <- legal int flag = 0; <- same as

User Defined Types (typedef) Many more uses (later)

Enumerated Types In the old days, we would make an assignment like 1 means Monday, 2 means Tuesday, 3 means Wednesday… But this way, you could have Sunday+1 and this would be meaningless. A better way is using enumerated types.

Enumerated Types (cont.) Example: typedef enum {monday, tuesday, wednesday, thursday, friday, saturday, sunday} DayOfWeek_t Some default identification for user defined types _t Explicitly specify the values!

Enumerated (cont.) Now, you can define a new variable DayOfWeek_t WeekDays; WeekDays = monday; <- legal WeekDays = 12; <- illegal WeekDays = someday; <- illegal Now, internally, the computer associates 0,1,2,… with monday, tuesday,… But you don’t have to worry!

Enumerated rules Enumerated constants must be identifiers, NOT numeric (1,3,-4), character (‘s’, ‘t’, ‘p’), or string (“This is a string”) literals. An identifier cannot appear in more than one enumerated type definition. Relational, assignment, and even arithmetic operators can be used.

Enumerated (cont.) if(today == saturday) tomorrow = sunday; else tomorrow = (DayOfWeek_t)(today+1);

Enumerated (cont.) for(today=monday; today <= friday; ++today) { … }

Passing a Function Name as a Parameter In C it is possible to pass a function name as a parameter. Gives the called function the ability to do something using different functions each time it’s called. Let’s look at a simple example similar to the evaluate example in the text.

E.G. Passing a function #include double evaluate(double f( ), double); int main (void) { double sqrtvalue, sinvalue; sqrtvalue = evaluate(sqrt, 12.5); printf("%f \n", sqrtvalue); sinvalue = evaluate(sin, 0.5); printf("%f \n", sinvalue); } double evaluate ( double f(double f_arg), double pt1) { return (f(pt1)); }

E.G. Passing a function #include double evaluate(double f( ), double); int main (void) { double sqrtvalue, sinvalue; sqrtvalue = evaluate(sqrt, 12.5); printf("%f \n", sqrtvalue); sinvalue = evaluate(sin, 0.5); printf("%f \n", sinvalue); } double evaluate ( double f(double f_arg), double pt1) { return (f(pt1)); }

E.G. Passing a function #include double evaluate(double f( ), double); int main (void) { double sqrtvalue, sinvalue; sqrtvalue = evaluate(sqrt, 12.5); printf("%f \n", sqrtvalue); sinvalue = evaluate(sin, 0.5); printf("%f \n", sinvalue); } double evaluate ( double f(double f_arg), double pt1) { return (f(pt1)); }

E.G. Passing a function #include double evaluate(double f( ), double); int main (void) { double sqrtvalue, sinvalue; sqrtvalue = evaluate(sqrt, 12.5); printf("%f \n", sqrtvalue); sinvalue = evaluate(sin, 0.5); printf("%f \n", sinvalue); } double evaluate ( double f(double f_arg), double pt1) { return (f(pt1)); }

Lab #6 : Trapezoidal Rule Write a program to solve for the area under a curve y = f(x) between the lines x=a and x=b. (See figure 7.13 on page 364. Approximate this area by summing trapezoids (Formed by a line from x0 vertical up to the function, to f(x0), then straight line to f(x1), back down to the x-axis, and left to original.)

Simple version of fig 7.13 y x (x0,y0) (x1,y1)(x2,y2) (x3,y3) (x4,y4) y = f(x) x0=ax1x2x3X4 n = 4

Lab #6 : assumptions Function is positive over the interval [a,b]. (for n subintervals of length h) h=(b-a)/n Trapezoidal rule is:

Lab #6 (cont.) Write a function trap with input parameters a,b,n and f that implements the trapezoidal rule. Call trap with values for n of 2,4,8,16,32,64, and 128 on functions

Lab #6 : (cont.) Function h defines a half-circle of radius 2. Compare your approximation to the actual area of this half-circle. Note: the trapezoidal rule approximates

Exam #1 On Wednesday Closed Book! One 8-1/2x11 paper, both sides allowed. Sit with a space on either side of you. Only 4 function calculators allowed. Chapters 1-6. Linux. Makefiles. Introduction to Pointers.