data types - university of maltastaff.um.edu.mt/mvel3/files/cprog/3_datatypes.pdf · 2016-08-30 ·...

19
Data Types CPS1011 {C};

Upload: others

Post on 15-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

Data Types

CPS1011

{C};

Page 2: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

CPS1011 2/19

Content

● Data types● Categories

– Integer types– Floating­point types

● Input/Output data objects of different types

Page 3: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

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

Page 4: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

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

??!!

Page 5: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

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

Page 6: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

CPS1011 6/19

Intro to Data Types (iv)

● Floating­point representation● 1.23456789

| + | .12345789 | 1 |

Sign Significand/Mantissa

Exponent

(  .123456789 x 101  ) ­ but base 2

Page 7: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

CPS1011 7/19

Integers (i)

● Size:– short ≤ int ≤ long ≤ long long– Typically int is 4 bytes (32­bits/double word)

● Range– unsigned: 0   2→ b­1– signed (default): ­ 2b­1   2→ b­1 – 1

● Expressed as:– decimal = 100 (%d) octal = 0144 (%#o) hex = 0x64 (%#x)

● Listings 3.3 3.4<<<

Page 8: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

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?

Page 9: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

CPS1011 9/19

Integers (iii)

● Character sets – single byte indexes (char)

Page 10: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

CPS1011 10/19

Integers (iv)

● Table look­up– 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<<<

Page 11: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

CPS1011 11/19

Integers (v)

● Escape sequences– Non­printable or conflicting characters but still use single quote syntax

● Booleans– Only require 1­bit– _Bool / stdbool.h's bool

Page 12: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

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;}

Page 13: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

CPS1011 13/19

Floats (i)

● Expressed as:– Decimal point notation (%e): 3.14159 .2 0.2 100. 100.0

– Exponential notation (%f): .8E­5 ­1.56E+12 4e16– (%g): Auto choose most suitable

● float   double                         long double (%Le %Lf %Lg)

– Min. range for all 10­37   10+37→

– So what is the difference?

Page 14: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

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 sub­set of them; If a particular float­point number cannot be 

stored, then store its nearest counterpart! Round­off errors● Different floating­point types mainly differ in terms of precision

– Number of significant figures that are accurately represented

Page 15: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

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);

Page 16: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

CPS1011 16/19

Floats (iv)

● Precision underflow– Subnormal values: loss of minimal precision, 

possibly even getting 0 for a non­0 value!!

    float afloat = 1.234;

    printf("%f can be written %e\n", afloat/10E900, afloat/10E900);

Page 17: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

CPS1011 17/19

Further data types  (i)

● sizeof

– Useful to write cross­platform 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));

Page 18: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

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 round­off but an all­out chop off!!

● Notes– Rounding­off possible – format modifiers– Some safe type mixing possible – type casting– Further detail later on ...

Page 19: Data Types - University of Maltastaff.um.edu.mt/mvel3/files/cprog/3_DataTypes.pdf · 2016-08-30 · C Primer Plus (6th edition). Stephen Prata. Addison Wesley, 2013. ISBN 9780321928429

CPS1011 19/19

CPS1011 Reading List

Textbook:● C Primer Plus (6th edition). Stephen Prata. Addison­

Wesley, 2013. ISBN 978­0321928429

Supplementary reading:● Expert C Programming: Deep C Secrets. Peter van der 

Linden. Prentice Hall, 1994. ISBN 978­0131774292● The C Programming Language. BW Kernighan, DM 

Ritchie. Prentice­Hall, 1988. ISBN 0­13­110362­8