Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes – 209.602.

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

18-May-15 Numbers. 2 Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish.
COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
Integer Types. Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Structure of a C program
Introduction to C Programming CE Lecture 2 Basics of C programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© 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.
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.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Chapter 2 Simple Data Types By C. Shing ITEC Dept Radford University.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Copyright © – Curt Hill Types What they do.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Variables Symbol representing a place to store information
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
 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
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
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.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
+ 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.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Numbers Mar 27, 2013.
CSE 220 – C Programming Bitwise Operators.
Formatted Input and Output
The Machine Model Memory
User Interaction and Variables
Input/output.
TMF1414 Introduction to Programming
EPSII 59:006 Spring 2004.
Variable Symbol represents a place to store information
Fundamental Data Types
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 08- printf and scanf
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 9 - Formatted Input/Output
Storing Information Each memory cell stores a set number of bits (usually 8 bits, or one byte) (byte addressable)
Conversion Check your class notes and given examples at class.
Conversion Check your class notes and given examples at class.
Fundamental Data Types
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Numbers 6-May-19.
Presentation transcript:

Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes –

Integer types by size char – Holds single characters short – Holds small numbers int – The usual integer long – When 2,000,000,000 isn’t enough

What you can say about size? short is at least as big as char int is at least as big as short long is a least as bit as int I.E., not much

On my office PC char 8 bits short 16 bits int 32 bits long 32 bits

On my web server char 8 bits short 16 bits int 32 bits long 64 bits

Max and min – twos complement bits bits bits bits

C Portability Problem It’s hard to write portable programs – When you don’t know what an int will hold More likely to be a problem with small chips – Such as embedded processors sizeof(int) will give the size in bytes – But this is hard too use without macros

C99 as problem and solution C99 adds an additional integer type – long long At least as big as long Provides bit-length specific integers – int32_t i ; Declares a 32-bit integer If is included

Unsigned Useful when you know number is positive – As in counts – Gives you twice as many positive values For 8 bit numbers which is it? – > – <

Floating point types by size float – Usually IEEE bit single precision double – Usually IEEE bit double precision long double – Often same as double – Could be IEEE bit “quad” precision – Could be Intel 80-bit “extended” precision

On my office PC typeprecisionrange float ~ 7 digits10 38 double ~ 15 digits long double ~ 17 digits

Integer literals Decimal – 209 Octal – 0321 Hexadecimal – 0xD1 Suffixes – U for unsigned – L for long

Floating point literals With a dot but no exponent – 10.5, 1.,.2 With an exponent and possibly a dot – 2e20,.2e20, 2.e20, 2.2e20, 7e-15 With possible suffixes – F for float – L for long

Default argument promotion Evil in C If a char or short is an argument to printf – It is promoted to an int If a float is an argument to printf – It is promoted to a double printf cannot tell signed from unsigned The above statements aren’t quite true, but they are good enough for now. In a later chapter, you’ll learn about prototypes and all of this may become a little clearer.

Therefore … printf “thinks” the following are identical – printf(" %c\n", 'A') ; – printf(" %c\n", 65) ; As are the following – printf(" %25.20f\n", f) ; – printf(" %25.20f\n", ) ; You may need to “tell” printf more or less than you think necessary

However Assume – int and long int are different – double and long double are different Because – They often take up more space on the stack Unless they are the same length – Which often is the case » Isn’t this confusing

Conversion specifiers for integers %c Prints as a character %d Prints as a decimal number %i Prints as a decimal number %o Prints as an unsigned octal number %x Prints as an unsigned hexadecimal number Letters in lower case %X Prints as an unsigned hexadecimal number Letters in upper case %u Prints as an unsigned decimal number

Length modifiers for integers Can proceed the integer conversion specifier – Except for %c Only useful one is l, as in printf("Big number is %ld\n", L) ; There is also largely unneeded h and hh – Prints the short and char part of an int

Conversion specifiers for floating point %e Prints in “scientific” notation e-4 %f Prints with fixed decimal %g Decides which of %e and %f look better %E %e with a big E %G %g with a big E

Length modifiers for floating point Use L for long double as in printf("Big number is %Lf\n", 2e2010L) ;

Input with scanf To read numbers use scanf scanf("%d", &courseNumber) ; scanf("%f", &temperature) ; Notice the & – It will be explained later in the course It means “address of”

Conversion specifiers for scanf %c Read the next character %d Read decimal integer %o Read octal integer %x Read hexadecimal integer %i Read integer – with 0 or 0x as prefix %u Read unsigned integer %f Read floating point %X is the same as %x %F, %e, %E, %g, %G are the same as %f

Length modifiers for scanf %l for long int or double %L for long double %h for short %hh for char given as integer

Matching scanf("%d,%d", &i, &j) ; – A comma must be between the numbers scanf("%d%%d", &i, &j) ; – A percent sign must be between the numbers