Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

Types and Variables. Computer Programming 2 C++ in one page!
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)
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.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Data types and variables
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 2 Data Types, Declarations, and Displays
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Programming III SPRING 2015
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
IT253: Computer Organization
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Data Representation.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Data Types. Data types Data type tells the type of data, that you are going to store in memory. It gives the information to compiler that how much memory.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Chapter 7: Basic Types Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Basic Types.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Simple Data Types Built-In and User Defined Chapter 10.
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.
Copyright © – Curt Hill Types What they do.
Introducing constants, variables and data types March 31.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Chapter 7: Basic Types Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Basic Types Last item in Page 89: check stdint.h.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
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.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Characters and Strings
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
C Formatted Input/Output
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Tokens in C Keywords Identifiers Constants
7. BASIC TYPES.
Fundamental Data Types
Data Type.
Data Type.
Rock, paper, scissors example
Lectures on Numerical Methods
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Fundamental Data Types
Data Type.
Variables and Constants
Presentation transcript:

Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types – contain fractional parts Integers are typically stored in either 16 bits or 32 bits. In a signed number, the left-most bit (the sign bit) is 0 if the number is positive or zero, 1 if it's negative. the largest 16-bit integer has the binary representation: = 2^15 - 1

short int unsigned short int = unsigned short int unsigned int long int = long unsigned long int Table 7.1TYPESmallest ValueLargest Value 16-bitshort int -32,76832,767 unsigned short int065,535 int -32,76832,767 unsigned int065,535 long int-2,147,483,6482,147,483,647 unsigned long int04,294,967,295 Table bit Macros that define the smallest and largest values of each integer type can be found in the header

For maximum portability, use int (or short int) for integers that won't exceed 32,767 and long int for all other integers. constants—numbers that appear in the text of a program, not numbers that are read, written, or computed. C allows integer constants to be written in decimal (base 10), octal (base 8), or hexadecimal (base 16). An octal number is written using only the digits 0 through 7. Each position in an octal number represents a power of 8 (just as each position in a decimal number represents a power of 10). The octal number 237 represents the decimal number = (2 *8^2) + (3*8^1) + (7x8^0) = = 159. A hexadecimal (or hex) number is written using the digits 0 through 9 plus the letters A through F, which stand for 10 through 15, respectively. Each position in a hex number represents a power of 16; the hex number 1AF has the decimal value = (1*16^2) + (10*16^1) + (15*16^0) = = 431.

Floating Types C provides three floating types, corresponding to different floating-point formats,: float single-precision floating-point32-bit double double-precision floating-point64-bit long double extended-precision floating-point Which type to use depends on the amount of precision required Numbers are stored in a form of scientific notation, with each number having three parts: a sign, an exponent, and a fraction. %e, %f, and %g are used for reading and writing single-precision floating-point numbers. Values of types double and long double require slightly different conversions: When reading a value of type double, put the letter l in front of e, f, or g: double d; scant("%lf", &d) ;Use l only in a scanf format string, not a print f string

When reading or writing a value of type long double, put the letter L in front of e, f,or g: long double Id; scanf("%Lf”, &ld) ; printf("%Lf”, Id) Character Types Today's most popular character set is ASCII (American Standard Code for Informa- tion Interchange), a 7-bit code capable of representing 128 characters. In ASCII, the digits 0 to 9 are represented by the codes , and the uppercase letters A to Z are represented by Some computers extend ASCII to an 8-bit code so that it can represent 256 characters. Other computers use entirely different character sets. IBM mainframes, for example, rely on an older code named EBCDIC. Future machines may use Unicode, a 16-bit code capable of representing 65,536 characters.

Character-Handling Functions ch = toupper(ch); /* converts ch to upper case */ Programs that call toupper need to have the following # include directive at the top: #include %c conversion specification allows scanf and printf to read and write single characters char ch; scanf("%c", &ch); /* reads a single character */ printf("%c", ch) ; /* writes a single character */ To force scanf to skip white space before reading a character, put a space in its format string just before %c: scanf(" %c", &ch); /* skips white space, then reads ch */

ch = getchar(); /* reads a character and stores it in ch */ putchar writes a single character: putchar(ch); Using getchar and putchar (rather than scanf and printf) saves time when the program is executed, getchar and putchar are faster than scanf and printf.

The sizeof Operator The sizeof operator allows a program to determine how much memory is required to store values of a particular type. sizeof (char) is always 1, but the sizes of the other types may vary. On a 16-bit machine, sizeof (int) is normally 2 On most 32-bit machines, sizeof (int) is 4.

Type Conversion Implicit conversions are performed on the following occasions: When the operands in an arithmetic or logical expression don't have the same type. (C performs what are known as the usual arithmetic conversions.) When the type of the expression on the right side of an assignment doesn't match the type of the variable on the left side. When the type of an argument in a function call doesn't match the type of the corresponding parameter. When the type of the expression in a return statement doesn't match the function's return type.

C allows the programmer to perform explicit conversions, using the cast operator. A cast expression has the form ( type-name) expression type-name specifies the type to which the expression should be converted. i = (int) f; /* f is converted to int */ Type Definitions Sec. 5.2, we used #define directive to create a macro that could be used as a Boolean type: #define BOOL int There's a better way to set up a Boolean type, though, using a feature known as a type definition: typedef int Bool; Using typedef to define Bool causes the compiler to add Bool to the list of type names that it recognizes. Bool can now be used the same way as built-in type names, in variable declarations and cast expressions. For example, use Bool to declare variables: Bool flag; /* same as int flag; */ The compiler treats Bool as a synonym for int; thus, flag is really nothing more than an ordinary int variable.

Type definitions can make a program more understandable For example, suppose that the variables cash_in and cash_out will be used to store dollar amounts. Declaring Dollars as typedef float Dollars; and then writing Dollars cash_in, cash_out; is more informative than just writing float cash_in, cash_out; Type definitions can also make a program easier to modify. If we later decide that Dollars should really be defined as double, all we need do is change the type definition: typedef double Dollars; The declarations of Dollars variables need not be changed. Without the type definition, we would need to locate all float variables that store dollar amounts and change their declarations. For greater portability, consider using typedef to define new names for integer types.