+ All Categories
Home > Documents > Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating...

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

Date post: 04-Jan-2016
Category:
Upload: noah-cox
View: 219 times
Download: 3 times
Share this document with a friend
24
Numeric types of C • Integers – Signed and unsigned – In a few different sizes – 209 • Floating point – In different sizes – 209.602
Transcript
Page 1: Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes – 209.602.

Numeric types of C

• Integers– Signed and unsigned– In a few different sizes– 209

• Floating point– In different sizes– 209.602

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

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

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

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

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

On my office PC

char 8 bits

short 16 bits

int 32 bits

long 32 bits

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

On my web server

char 8 bits

short 16 bits

int 32 bits

long 64 bits

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

Max and min – twos complement

-128127 8 bits

-3276832767 16 bits

-21474836482147483647 32 bits

-92233720368547758089223372036854775807 64 bits

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

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

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

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 <inttype.h> is included

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

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?– 00000001 > 11111111– 00000001 < 11111111

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

Floating point types by size

• float– Usually IEEE 754 32-bit single precision

• double– Usually IEEE 754 64-bit double precision

• long double– Often same as double– Could be IEEE 754-2008 128-bit “quad” precision– Could be Intel 80-bit “extended” precision

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

On my office PC

type precision range

float ~ 7 digits 1038

double ~ 15 digits 10300

long double ~ 17 digits 104900

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

Integer literals

• Decimal– 209

• Octal– 0321

• Hexadecimal– 0xD1

• Suffixes– U for unsigned– L for long

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

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

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

Default argument promotionEvil 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 enoughfor now. In a later chapter, you’ll learn about prototypes and all ofthis may become a little clearer.

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

Therefore …

• printf “thinks” the following are identical– printf(" %c\n", 'A') ;– printf(" %c\n", 65) ;

• As are the following– printf(" %25.20f\n", 3.1416f) ;– printf(" %25.20f\n", 3.1416) ;

• You may need to “tell” printf more or less than you think necessary

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

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

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

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

%xPrints as an unsigned hexadecimal numberLetters in lower case

%XPrints as an unsigned hexadecimal numberLetters in upper case

%u Prints as an unsigned decimal number

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

Length modifiers for integers

• Can proceed the integer conversion specifier– Except for %c

• Only useful one is l, as inprintf("Big number is %ld\n", 20000000000000L) ;

• There is also largely unneeded h and hh– Prints the short and char part of an int

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

Conversion specifiersfor floating point

%ePrints in “scientific” notation5.03567e-4

%fPrints with fixed decimal50356.7

%g Decides which of %e and %f look better%E %e with a big E%G %g with a big E

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

Length modifiers for floating point

• Use L for long double as inprintf("Big number is %Lf\n", 2e2010L) ;

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

Input with scanf

• To read numbers use scanfscanf("%d", &courseNumber) ;

scanf("%f", &temperature) ;

• Notice the &– It will be explained later in the course

• It means “address of”

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

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

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

Length modifiers for scanf

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

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

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


Recommended