cs115 fall 2006-2007 senem kumova-metİn1 the fundamental data types chapter 3

32
CS115 FALL 2006-2007 Senem KU MOVA-METİN 1 The Fundamental Data Types CHAPTER 3

Upload: barbra-lindsey

Post on 14-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

1

The Fundamental Data Types

CHAPTER 3

Page 2: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

2

Variables and constant

Variables and constant are the objects that program manipulates…

In C, all variables must be declared, i.e., their data type,  before they can be used

int main(void){ int a,b,c ; /* declaration*/

float x= 3.14; /* declaration with initialization*/

… }

Page 3: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

3

Declarations and Expressions Declaration: tell the compiler to set aside an appropriate

amount of space in memory to hold values associated with variable

int x=1;

float y=1;

Expression: meaningful combinations of constants , variables, operators and function calls

a+b

sqrt(3.444),

i=7

1

1.0

Page 4: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

4

AssignmentSOME ASSIGNMENT EXAMPLES :

i=7;

x=x+1; /* x is assigned the old value of x plus 1 */

y+=4;

x +2 =0; /* WRONG */

0=1; /* WRONG */

Page 5: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

5

FUNDAMENTAL DATA TYPES

1character

charsigned char

unsigned char

2integer

short int long

unsigned short

unsignedunsignedlong

3 float

float double long double

Page 6: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

6

FUNDAMENTAL DATA TYPES

1character

charsigned char

unsigned char

2integer

short int long

unsigned short

unsignedunsignedlong

3 float

float double long double

Page 7: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

7

1. Characters and the char Data Type (1/4)

“char” is a 1-byte (1byte= 8 bits)

28=256 distinct values

Mostly used for representing characters Can also be used to represent also integers !!

signed char : have values between -128 and 127 ( in a two's complement machine)

unsigned char : have values between 0 and 255

Printable characters are always positive !!

Page 8: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

8

Some char constant and their integer values (ASCII Table in appendix D )

Character constant ‘a’ ‘b’ ‘c’ … ‘z’

Corresponding value 97 98 99 … 112

Character constant ‘A’ ‘B’ ‘C’ … ‘Z’Corresponding value 65 66 67 … 90

Character constant ‘0’ ‘1’ ‘2’ … ‘9’

Corresponding value 48 49 50 … 57Character constant ‘&’ ‘*’ ’+’

Corresponding value 38 42 43

1. Characters and the char Data Type (2/4)

Page 9: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

9

1. Characters and the char Data Type (3/4)

Page 10: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

10

EXAMPLE: char  c = 'a'; 

/* ASCII code for 'a' is

(01100001)2 = (97)10*/

printf("%c", c); /* a is printed   */

printf("%d", c); /* 97 is printed */

printf("%c%c%c",c,c+1,c+2); 

/* abc is printed */ HOW CAN WE CONVERT a CHAR FROM LOWER CASE TO UPPER CASE???

1. Characters and the char Data Type (4/4)

Page 11: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

11

FUNDAMENTAL DATA TYPES

1character

charsigned char

unsigned char

2integer

short int long

unsigned short

unsignedunsignedlong

3 float

float double long double

Page 12: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

12

Represents integer values

Typically an int is stored in 2 bytes (16 bits) or in 4 bytes (32 bits)

Use sizeof() function

2. The Data Type int (1/4)

#include<stdio.h>

main()

{ int x=350000;

printf("x= %d\n”, x);

printf("sizeof(x)= %d\n", sizeof(x));}

Page 13: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

13

EXAMPLE :IF a computer has 4 bytes for int THEN

There are 232 distinct states for integers

Half of the states can be used for positive numbers, the other half for negative numbers

-231,-231+1, … , -2, -1, 0 , 1 , 2 , … , 231-1

In this machine integers can vary between -2(~-231 )billion to +2 (~231-1) billion

IF an integer is assigned a value greater than 2 billions or less than -2 billions an integer overflow occurs..

2. The Data Type int (2/4)

Page 14: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

14

2. The Data Type int (3/4)

long 4 bytes (232 distinct values) short 2 bytes (216 distinct

values) int

On machines with 4byte words int has the same size with long

On machines with 2byte words int has the same size with short

unsigned has no sign (always positive and has the same size with int)

Page 15: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

15

Suffixes are used to specify type of integer

EXAMPLE long int x = 37L; unsigned long int y= 37UL;

2. The Data Type int (4/4)

Page 16: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

16

FUNDAMENTAL DATA TYPES

1character

charsigned char

unsigned char

2integer

short int long

unsigned short

unsignedunsignedlong

3 float

float double long double

Page 17: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

17

3. The floating data types (1/4)

3 floating types :

float (mostly 4 bytes) double (mostly 8 bytes) long double

Some examples

-3.14159f =  -314.159e-2F  /* float */ 0e0     /* floating point zero 0.0 of type double */ 1.        /* double 1.0 */ 0.1908 /* double 0.1908 */

Suffix Type Example

f or F float 3.7F

l or Llong

double 3.7L

Page 18: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

18

222.3333 e-10 = 222.3333 * 10-10

Floating point constant parts for 222.3333e-10

Integer Fraction Exponent

222 3333 e-10

3. The floating data types (2/4)

Page 19: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

19

Precision : Number of significant decimal places a floating value carries

Range : Limits of the largest & smallest positive floating values that can be

represented by a variable of that type

------------------------------------------------------------

EXAMPLE : IF precision is 6 and range is 10-38 to 10+38 for type X THEN X can be

0.d1d2d3d4d5d6 * 10n and -38 < = n <=38

3. The floating data types (3/4)

Page 20: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

20

For double floating numbers : precision = 6, range = 10-38 to 10+38

0.d1d2d3d4d5d6 * 10n and -38 < = n <=38

For long floating numbers : precision = 15, range = 10-308 to 10+308

0.d1d2d3….d15 * 10n and -308 < = n <=308

3. The floating data types (4/4)

Page 21: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

21

The use of typedef

Programmer can explicitly associate a type with an identifier

#include<stdio.h>

main()

{ typedef char uppercase;/* associate type char with identifier “uppercase”

*/

uppercase u='A'; /* declare character “u” */

typedef int myint;/* associate type int with identifier “myint” */

myint x =90; /* declare integer “myint” */

printf("%c %d \n" , u , x); }

Page 22: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

22

The sizeof operator

sizeof(object) Returns an integer that represent the number of bytes needed to store an object in memory

EXAMPLE:int x=4;printf(“%d”, sizeof(x));printf(“%d”, sizeof(x+3));

Check what sizeof() will return for int, double, char, unsigned … etc. ????

Page 23: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

23

getchar( ) & putchar( ) (1/2)

Macros defined in “stdio.h” that are used to read characters from keyboard and to print characters on screen

#include <stdio.h>int main(void) { int c; if((c = getchar())!= EOF) /* scanf(“%c”, &c);*/

{ putchar(c); /* printf(“%c”, c);*/ putchar(c); }

return 0;/* EOF = end-of-file =-1 */

}

Page 24: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

24

getchar( ) & putchar( ) (2/2)

#include <stdio.h>int main(void) { int c; while((c = getchar())!=EOF)/* scanf(“%c”, &c);*/

{ putchar(c); /* printf(“%c”, c);*/ putchar(c); }

return 0;/* EOF = end-of-file =-1 *//* EOF CTRL +Z

}

Page 25: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

25

Mathematical functions, Printing tricks

math.h sqrt(), pow(), exp(),log(), sin() …

#include <stdio.h>

#include<math.h>

int main(void)

{ double x =40000.0;

printf("x=%12.4e\n", x);

/* 4 digits for fraction part, totally 12 characters*/

printf("square root of x=%10.1e", sqrt(x));

/* 1 digits for fraction part, totally 10 characters*/

}

OUTPUT :x= 4.0000e+004square root of x= 2.0e+002

Page 26: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

26

Arithmetic Conversions : Review

any_type op long double -> long double any_type op double -> double

int x; double y; (x+y) double

any_type op float -> float any_type op unsigned long -> unsigned long

……

Check the table (pg.133)

Page 27: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

27

CASTS : Forces explicit converions#include <stdio.h>

void main(void)

{ int x=3, y=10; double divv;

divv=10/3;

printf("10/3 = %f\n", divv);

divv =y/x;

printf("y/x = %f\n", divv);

divv=(double)10/3; //FORCE TO STORE 10/3 IN DOUBLE

printf("(double)10/3 = %f\n", divv);}

OUTPUT : 10/3 = 3.000000y/x = 3.000000(double)10/3 = 3.333333

Page 28: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

28

Hexadecimal and Octal constants (1/4)

hexadecimal digit 1 2 3 … 9 10 11 12 13 14 15

decimal value 1 2 3 … 9 A B C D E F

Octal digit 1 2 3 4 5 6 7

decimal value 1 2 3 4 5 6 7

Page 29: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

29

EXAMPLE :A0F3C = A * 164

+ 0 * 163

+ F * 162

+ 3 * 161

+ C * 160

= 10 * 164 +0 * 163 + 15 * 162 +3 * 161 + 12 * 160

= 659260

Hexadecimal and Octal constants (2/4)

Page 30: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

30

In C source code , positive integer constants prefaced with 0 are integers in octal notation 0x are integers in hexadecimal notation

In printf() function %d --- decimals %o --- octals %x --- hexadecimals

Hexadecimal and Octal constants (3/4)

Page 31: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

31

#include <stdio.h>

void main(void) { int x=056; int y= 0xA01

printf("%d \n", x); // 46printf("%o \n", x); // 56printf("%x \n\n", x); // 2e

printf("%d \n", y); // 2561printf("%o \n", y); // 5001printf("%x \n", y); // a01

}

Hexadecimal and Octal constants (4/4)

Page 32: CS115 FALL 2006-2007 Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3

CS115 FALL 2006-2007 Senem KUMOVA-METİN

32

Exercise

Write a program that prints the values of each digit in a 3-digit integer number.

Sample output:

input a positive integer number: 376first digit of the number is: 3second digit of the number is: 7third digit of the number is: 6