1 fundamental data types chapter 3 in abc. 2 #include int main(void) { int a, b, c; float x = 0.0f,...

29
1 Fundamental data types Chapter 3 in ABC

Post on 20-Dec-2015

230 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

1

Fundamental data types

Chapter 3 in ABC

Page 2: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

2

#include <stdio.h>

int main(void)

{

int a, b, c;

float x = 0.0f, y = 3.3f, z = -7.7f;

printf( “Input two integers: ” );

scanf( “%d%d”, &b, &c );

a = b + c;

x = y + z;

return 0;

}

DeclarationDeclarationDeclaration with initialization

always initialize!!!

Declaration with initialization always initialize!!!

Function callsFunction calls

Assignment statements

Assignment statements

Page 3: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

3

Fundamental data types

• Integral (signed or unsigned) char short int long

• Floating point types float double

Page 4: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

4

Long formLong form

char signed char unsigned char

signed short int signed int signed long int

unsigned short int unsigned int unsigned long int

float double long double

Common (short) formCommon (short) form

char signed char unsigned char

short int long

unsigned short unsigned unsigned long

float double long double

Fundamental Data Types

Page 5: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

5

Functionality Groups

Integral Types:Integral Types:

char signed char unsigned char

short int long

unsigned short unsigned unsigned long

Floating Types:Floating Types:

float double long double

Arithmetic Types:Arithmetic Types:

• integral types

• floating types

Page 6: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

6

Constants

char ‘a’, ‘x’, ‘0’, ‘&’, ‘\n’

int 1, 0, -54, 4234567

long 0l, 123l, –7766L

unsigned 0u, 23u, 3000000000U

float 1.2f, .2f, 1.f, 3.14159f

double 1.0, -3.1412

long double 1.0l, –2.4433l

Page 7: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

7

character: 'a' 'b' 'c' ... 'z'

integer value: 97 98 99 ... 112

character: 'A' 'B' 'C' ... 'Z'

integer value: 65 66 67 ... 90

character: '0' '1' '2' ... '9'

integer value: 48 49 50 ... 57

character: '&' '*' '+'

integer value: 38 42 43

Some Character Constants and their Integer Values

The character ‘0‘ has a value of 48

The character ‘0‘ has a value of 48

Page 8: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

8

Some Character Constants and their Integer Values

Name of Character Written in C Integer Value

alert ‘\a’ 7

backslash ‘\\’ 92

horizontal tab ‘\t’ 9

newline ‘\n’ 10

null character ‘\0’ 0

quote ‘\” 39

The character ‘\0‘ has a

value of 0

The character ‘\0‘ has a

value of 0

Page 9: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

9

Some Character Constants and their Integer Values

char c = 'a';

printf("%c", c);

printf("%d", c);

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

a is printeda is printed

97 is printed97 is printed

abc is printedabc is printed

Page 10: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

10

Some Character Constants and their Integer Values

character: 'A' 'B' 'C' ... 'Z'

integer value: 65 66 67 ... 90

char c = 0;

int i = 0;

for ( i = 'a'; i <= 'z'; ++i )

printf( “%c”, i );

for ( c = 65; c <= 90; ++c )

printf( “%c”, c );

for ( c = '0'; c <= '9'; ++c )

printf( “%d”, c );

abc ... z is printedabc ... z is printed

ABC ... Z is printedABC ... Z is printed

484950 ... 57 is printed484950 ... 57 is printed

Page 11: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

11

User Defined Constants

#include <stdio.h>

int main(void)

{

int c = 0;

while ( ( c = getchar() ) != EOF )

{

putchar( c );

putchar( c );

}

return 0;

}

Loop until the End Of the FileLoop until the End Of the File

in stdio.h appear the line: #define EOF (-1)

in stdio.h appear the line: #define EOF (-1)

Page 12: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

12

Constants

Type Description Example

char single character ‘g’

special character ‘\n’

hexadecimal code ‘\x1a’

int decimal -1, 2, 0, 123456

octal 010, -020, 080

hexadecimal 0xfffe, 0x1000, 0x1, 0x2, 0x4, 0x8

double int.frac .5, 1.0, -2.1712, 1.23

int.frac + e[+,-]exp 123e-2, 12.3e-2

Page 13: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

13

Decimal, Hexadecimal, Octal conversions

#include <stdio.h>

int main(void)

{

printf( “%d %x %o\n”, 19, 19, 19 );

printf( “%d %x %o\n”, 0x1c, 0x1c, 0x1c);

printf( “%d %x %o\n”, 017, 017, 017);

printf( “%d\n”, 11 + 0x11 + 011);

printf( “%x\n”, 2097151);

printf( “%d\n”, 0x1FfFFf);

return 0;

}

19 13 2319 13 23

28 1c 3428 1c 34

15 f 1715 f 17

3737

1fffff1fffff

20971512097151

Page 14: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

14

compute the size of some fundamental types

#include <stdio.h>

int main(void)

{

printf("The size of some fundamental types" " is computed.\n\n");

printf("char: %3d byte \n", sizeof(char) );

printf("short: %3d bytes\n", sizeof(short) );

printf("int: %3d bytes\n", sizeof(int) );

printf("long: %3d bytes\n", sizeof(long) );

printf("unsigned: %3d bytes\n", sizeof(unsigned) );

printf("float: %3d bytes\n", sizeof(float) );

printf("double: %3d bytes\n", sizeof(double) );

printf("long double:%3d bytes\n",sizeof(long double) );

return 0;

}

sizeof returns the number of bytes

reserved for a variable type.

sizeof returns the number of bytes

reserved for a variable type.

Page 15: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

15

compute the size of some fundamental types

run on “PC Pentium 3":

The size of some fundamental types is computed.

char: 1 byte

short: 2 bytes

int: 4 bytes

long: 4 bytes

unsigned: 4 bytes

float: 4 bytes

double: 8 bytes

long double: 8 bytes

what is the output of the program on nova?

what is the output of the program on nova?

Page 16: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

16

compute the size of some fundamental types

sizeof(char) == 1

sizeof(short) <= sizeof(int) <= sizeof(long)

sizeof(signed) == sizeof(unsigned) == sizeof(int)

sizeof(float) <= sizeof(double) <= sizeof(long double)

Page 17: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

17

Going over the Limit

#include <stdio.h>

#include <limits.h>

int main( void )

{

int i = 0;

unsigned u = UINT_MAX;

for ( i = 0; i < 5; ++i )

printf( "%u + %d = %u\n", u, i, u + i );

for ( i = 0; i < 5; ++i )

printf( "%u * %d = %u\n", u, i, u * i );

return 0;

}

Typically equal to 0xFFFFFFFF == 232-1 == 4294967295

Typically equal to 0xFFFFFFFF == 232-1 == 4294967295

Page 18: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

18

Going over the Limit

run on "scorpio":

4294967295 + 0 = 4294967295

4294967295 + 1 = 0

4294967295 + 2 = 1

4294967295 + 3 = 2

4294967295 + 4 = 3

4294967295 * 0 = 0

4294967295 * 1 = 4294967295

4294967295 * 2 = 4294967294

4294967295 * 3 = 4294967293

4294967295 * 4 = 4294967292

The variable restarts itselfThe variable restarts itself

Page 19: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

19

Integers' Representation

Type value Binary valueunsigned char vvvv vvvvunsigned char 0 0000 0000unsigned char 255 1111 1111unsigned char 127 0111 1111unsigned char 128 1000 0000signed char svvv vvvvsigned char 0 0000 0000signed char -1 1111 1111 (2's complement)

signed char 127 0111 1111signed char -128 1000 0000 (2's complement)

signed short svvv vvvv vvvv vvvvsigned int svvv vvvv vvvv vvvv vvvv vvvv vvvv vvvv

v means valuev means values means sign

negative integers -in "2's complement"

s means signnegative integers -in

"2's complement"

Page 20: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

20

Integers' Representation (2)

signed char sc = -128;

unsigned char uc = 255;

sc = sc - 1;

uc = uc + 1;

uc = 255;

uc = uc*2;

1000 00001000 0000

1111 11111111 1111

0111 1111 = 127 Underflow0111 1111 = 127 Underflow

0000 0000 = 0 Overflow0000 0000 = 0 Overflow

1111 11111111 1111

1111 1110 = 2541111 1110 = 254

Page 21: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

21

Float representationDecimal base: int.frac*10exp

3/16 = 0.1875 = 1.875*10-1

1000/3 = 333.33333... =3.333...*102

Binary base: 1.frac*2exp

3/16 = 3*2-4 = 1.5*2-3 = 1.1*2-11

1/10 = 1.1001100110011...*2-100

Fixed size, limited accuracy. float = 4 bytes:

seee eeee efff ffff ffff ffff ffff ffff

double uses 8 bytes (64 = 1+11+52) http://steve.hollasch.net/cgindex/coding/ieeefloat.html

exp+127[8] frac (mantissa)[23]sign[1]

base 2

Page 22: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

22

Float representation - limited accuracy

#include <stdio.h>

int main()

{

int i;

float f = 0;

for (i = 0; i < 100; ++i )

f += 0.01;

printf( "%f\n", f );

return 0;

}Output: 0.999999Output: 0.999999

Page 23: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

23

Special float values

• NaN – Not a Number. Represents an illegal value– printf("%f\n", sqrt(-1)); will print -1.#IND00 or nan

• INF – infinity– printf("%f\n", 1/0.0);

will print 1.#INF00 or inf

Page 24: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

24

Mathematical Functions

sqrt(x)

pow(x,y) = xy

exp(x) =eX

log(x)

sin(x)

cos(x)

tan(x)

The functions are declared in <math.h>The functions are declared in <math.h>

All the functions use doubles(floats: sqrtf, powf,expf,...)

All the functions use doubles(floats: sqrtf, powf,expf,...)

Requires linking with math library: gcc -lm

Requires linking with math library: gcc -lm

Page 25: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

25

#include <stdio.h>

#include <math.h>

int main(void)

{

double x = 0;

printf( "\nThe square root of x and x raised"

"\nto the x power will be computed.\n---\n\n" );

while ( 1 )

{

printf("Input x: ");

scanf("%lf", &x );

if ( x >= 0.0 )

printf("\n%15s%22.15e\n%15s%22.15e\n%15s%22.15e\n\n"

,"x = ", x, "sqrt(x) = ", sqrt(x),

"pow(x, x) =", pow(x, x) );

else

printf("\nSorry, your number must be nonnegative.\n\n" );

}

return 0;

}

Infinite LoopInfinite Loop

Page 26: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

26

The Result of the ProgramThe square root of x and x raisedThe square root of x and x raised

to the x power will be computed.to the x power will be computed.

------

Input x: 2Input x: 2

x = 2.000000000000000e+00x = 2.000000000000000e+00

sqrt(x) = 1.414213562373095e+00sqrt(x) = 1.414213562373095e+00

pow(x, x) = 4.000000000000000e+00pow(x, x) = 4.000000000000000e+00

Input x:Input x:

Page 27: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

27

The usual arithmetic conversion (promotion)

• If either operand is of type long double, double, float or unsigned long, the other operand is converted to long double, double, float or unsigned long appropriately.

• Otherwise, the "integral promotions" are performed on both operands, and the following rules are applied:

• If one operand has type long and the other operand has type unsigned then one of two possibilities occurs:

– If a long can represent all the values of an unsigned, then the operand of type unsigned is converted to long.

– If a long cannot represent all the values of an unsigned, then both operands are converted to unsigned long.

• Otherwise, if either operand is of type long, the other operand is converted to long.

• Otherwise, if either operand is of type unsigned, the other operand is converted to unsigned.

• Otherwise, both operands have type int.

Page 28: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

28

Expressions and Types

char c; short s; int i; unsigned u; unsigned long ul;float f; double d; long double ld;

Expression Type Expression Type

c – s / i int u * 7 – i unsigned

u * 2.0 – i double f * 7 – i float

c + 3 int 7 * s * ul unsigned long

c + 5.0 double ld + c long double

d + s double u – ul unsigned long

2 * i / l long u - l system dependent

Page 29: 1 Fundamental data types Chapter 3 in ABC. 2 #include int main(void) { int a, b, c; float x = 0.0f, y = 3.3f, z = -7.7f; printf( “Input two integers:

29

Castdouble d = 3.3;int i = 0;unsigned ui = 0;char c;

i = (int)d;

d = (double)i / 2;

ui = (unsigned)i;

c = 882;

i == trunc(d) = 3i == trunc(d) = 3

d == 1.5d == 1.5

882 = 0x372 overflow c == 0x72=114(behavior in case of overflow is undefined ANSI C)

882 = 0x372 overflow c == 0x72=114(behavior in case of overflow is undefined ANSI C)