Data Types
CPS1011
{C};
CPS1011 2/19
Content
● Data types● Categories
– Integer types– Floatingpoint types
● Input/Output data objects of different types
CPS1011 3/19
Intro to Data Types (i)
● Data objects for different value types require different declarations– float weight; vs int weight; – float value; vs int value; – scanf("%f", &weight); vs scanf("%d", &weight);
– Listing 3.1<<<– Data types define
● Size● Value range● Representation● Precision
CPS1011 4/19
Intro to Data Types (ii)
#include <stdio.h>
int main(void)
{
int myint = 4294967295; //0xFFFFFFFF all 1's!
printf("Myint: %d\n", myint);
printf("Myint: %u\n", myint);
printf("Myint: %f\n", myint);
return 0;
}
Output
Myint: 1
Myint: 4294967295
Myint: 0.000000
??!!
CPS1011 5/19
Intro to Data Types (iii)
● It is a question of representation● Unsigned integer
27 26 25 24 23 22 21 20
1 1 1 1 1 1 1 1 = 25510
● Signed integer (2's complement)
27 26 25 24 23 22 21 20
1 1 1 1 1 1 1 1 = 110
CPS1011 6/19
Intro to Data Types (iv)
● Floatingpoint representation● 1.23456789
| + | .12345789 | 1 |
Sign Significand/Mantissa
Exponent
( .123456789 x 101 ) but base 2
CPS1011 7/19
Integers (i)
● Size:– short ≤ int ≤ long ≤ long long– Typically int is 4 bytes (32bits/double word)
● Range– unsigned: 0 2→ b1– signed (default): 2b1 2→ b1 – 1
● Expressed as:– decimal = 100 (%d) octal = 0144 (%#o) hex = 0x64 (%#x)
● Listings 3.3 3.4<<<
CPS1011 8/19
Integers (ii)
● Only integers and floats!– How about alphabetic data ? (a, b, c, d, e …)– Integers!! . indexing a character set
● char
– 1 Byte– Range 0 255→
● How does it work exactly?
CPS1011 9/19
Integers (iii)
● Character sets – single byte indexes (char)
CPS1011 10/19
Integers (iv)
● Table lookup– Single quote syntax– char grade = 'A';
– Stores: 010000012 (6510)
● Output– printf("The code for %c is %d.\n", ch, ch);
– >> The code for A is 65.
● Listing 3.5<<<
CPS1011 11/19
Integers (v)
● Escape sequences– Nonprintable or conflicting characters but still use single quote syntax
● Booleans– Only require 1bit– _Bool / stdbool.h's bool
CPS1011 12/19
Integers (vi)
● Size matters: (range) under/overflows
#include <stdio.h>#include <limits.h>
int main(void){ int x = INT_MAX; int y = UINT_MAX;
printf("%d + 1 =%d\n", x, x+1); printf("%u + 1 =%u\n", y, y+1);
return 0;}
CPS1011 13/19
Floats (i)
● Expressed as:– Decimal point notation (%e): 3.14159 .2 0.2 100. 100.0
– Exponential notation (%f): .8E5 1.56E+12 4e16– (%g): Auto choose most suitable
● float double long double (%Le %Lf %Lg)
– Min. range for all 1037 10+37→
– So what is the difference?
CPS1011 14/19
Floats (ii)
● Precision (or lack of!)– How many real numbers in the range from 0 to 1?
● 0 … 0.001 ….0.01 … 0.1 … 0.1 … 0.12 … 0.21 ... 0.211 ….
– Infinite!! That required infinite h/w resources to store– Impractical– Solution:
● Give up precision● Only store a subset of them; If a particular floatpoint number cannot be
stored, then store its nearest counterpart! Roundoff errors● Different floatingpoint types mainly differ in terms of precision
– Number of significant figures that are accurately represented
CPS1011 15/19
Floats (iii)
● Precision– float ≤ double ≤ long double
● (Range) overflow
● Listing 13.7<<<
float afloat = FLT_MAX;
printf("%f can be written %e\n", afloat, afloat);
printf("%f can be written %e\n", afloat*10, afloat*10);
float afloat = 1.23456789;
printf("%f can be written %e\n", afloat, afloat);
CPS1011 16/19
Floats (iv)
● Precision underflow– Subnormal values: loss of minimal precision,
possibly even getting 0 for a non0 value!!
float afloat = 1.234;
printf("%f can be written %e\n", afloat/10E900, afloat/10E900);
CPS1011 17/19
Further data types (i)
● sizeof
– Useful to write crossplatform code
printf("Type char has a size of %zd bytes.\n", sizeof(char));
printf("Type long has a size of %zd bytes.\n", sizeof(long));
CPS1011 18/19
Further data types (ii)● Data types do not mix● Sometimes not to serious
– int myint = 5.0;
– Stores 5
● Other times could be a disaster– int myint = 12.99;
– Stores 12 – Note: not a roundoff but an allout chop off!!
● Notes– Roundingoff possible – format modifiers– Some safe type mixing possible – type casting– Further detail later on ...
CPS1011 19/19
CPS1011 Reading List
Textbook:● C Primer Plus (6th edition). Stephen Prata. Addison
Wesley, 2013. ISBN 9780321928429
Supplementary reading:● Expert C Programming: Deep C Secrets. Peter van der
Linden. Prentice Hall, 1994. ISBN 9780131774292● The C Programming Language. BW Kernighan, DM
Ritchie. PrenticeHall, 1988. ISBN 0131103628