Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental data types

Similar presentations


Presentation on theme: "Fundamental data types"— Presentation transcript:

1 Fundamental data types
Chapter 3 in ABC

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; } Declaration Declaration with initialization always initialize!!! Function calls Assignment statements

3 Fundamental data types
Integral (signed or unsigned) char short int long Floating point types float double

4 Fundamental Data Types
Long 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) form short int long unsigned short unsigned unsigned long

5 Functionality Groups Integral Types: char signed char unsigned char
short int long unsigned short unsigned unsigned long Floating Types: float double long double Arithmetic Types: integral types floating types

6 Constants ‘a’, ‘x’, ‘0’, ‘&’, ‘\n’ char 1, 0, -54, 4234567 int
0l, 123l, –7766L long 0u, 23u, U unsigned 1.2f, .2f, 1.f, f float 1.0, double 1.0l, –2.4433l long double

7 Some Character Constants and their Integer Values
character: 'a' 'b' 'c' 'z' integer value: character: 'A' 'B' 'C' 'Z' integer value: character: '0' '1' '2' '9' integer value: character: '&' '*' '+' integer value: The character ‘0‘ has a value of 48

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’ quote ‘\” 39 The character ‘\0‘ has a value of 0

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 printed 97 is printed abc is printed

10 Some Character Constants and their Integer Values
character: 'A' 'B' 'C' 'Z' integer value: 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 printed ABC ... Z is printed is printed

11 User Defined Constants
#include <stdio.h> int main(void) { int c = 0; while ( ( c = getchar() ) != EOF ) putchar( c ); } return 0; Loop until the End Of the File in stdio.h appear the line: #define EOF (-1)

12 Constants Type Description Example char single character ‘g’
special character ‘\n’ hexadecimal code ‘\x1a’ int decimal -1, 2, 0, octal 010, -020, 080 hexadecimal 0xfffe, 0x1000, 0x1, 0x2, 0x4, 0x8 double int.frac .5, , , int.frac + e[+,-]exp 123e-2, 12.3e-2

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”, x ); printf( “%x\n”, ); printf( “%d\n”, 0x1FfFFf); return 0; } 28 1c 34 15 f 17 37 1fffff

14 compute the size of some fundamental types
sizeof returns the number of bytes reserved for a variable type. #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; } The difference between char, short, int, long is the number of bits they hold sizeof operator measures the number of bytes required to store an object. the size of variables are machine dependent!!!

15 compute the size of some fundamental types
run on “PC Pentium 3": The size of some fundamental types is computed. char: byte short: bytes int: bytes long: bytes unsigned: bytes float: bytes double: bytes long double: 8 bytes what is the output of the program on nova?

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)

17 Typically equal to 0xFFFFFFFF == 232-1 == 4294967295
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 ); printf( "%u * %d = %u\n", u, i, u * i ); return 0; } Typically equal to 0xFFFFFFFF == ==

18 The variable restarts itself
Going over the Limit run on "scorpio": = = 0 = 1 = 2 = 3 * 0 = 0 * 1 = * 2 = * 3 = * 4 = The variable restarts itself

19 Integers' Representation
Binary value value Type vvvv vvvv unsigned char 255 127 128 svvv vvvv signed char (2's complement) -1 (2's complement) -128 svvv vvvv vvvv vvvv signed short svvv vvvv vvvv vvvv vvvv vvvv vvvv vvvv signed int s means sign negative integers -in "2's complement" v means value

20 Integers' Representation (2)
signed char sc = -128; unsigned char uc = 255; sc = sc - 1; uc = uc + 1; uc = 255; uc = uc*2; = 127 Underflow = 0 Overflow = 254

21 Float representation seee eeee efff ffff ffff ffff ffff ffff
Decimal base: int.frac*10exp 3/16 = = 1.875*10-1 1000/3 = = *102 Binary base: 1.frac*2exp 3/16 = 3*2-4 = 1.5*2-3 = 1.1*2-11 1/10 = *2-100 Fixed size, limited accuracy. float = 4 bytes: seee eeee efff ffff ffff ffff ffff ffff double uses 8 bytes (64 = ) base 2 standardized sign[1] exp+127[8] frac (mantissa)[23]

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; } Learn more about in numerical analysis course (and numeric stability in geometric computing) Output:

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 Use some special bit sequences to represent NaN and INF.

24 Mathematical Functions
sqrt(x) pow(x,y) = xy exp(x) =eX log(x) sin(x) cos(x) tan(x) All the functions use doubles (floats: sqrtf, powf,expf,...) The functions are declared in <math.h> Requires linking with math library: gcc -lm

25 Infinite Loop #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 Loop

26 The Result of the Program
The square root of x and x raised to the x power will be computed. --- Input x: 2 x = e+00 sqrt(x) = e+00 pow(x, x) = e+00 Input x:

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.

28 Expressions and Types Expression Type c – s / i int u * 7 – i unsigned
char c; short s; int i; unsigned u; unsigned long ul; float f; double d; long double ld; Expression Type c – s / i int u * 7 – i unsigned u * 2.0 – i double f * 7 – i float c + 3 7 * s * ul unsigned long c + 5.0 ld + c long double d + s u – ul 2 * i / l long u - l system dependent

29 Cast double 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) = 3 d == 1.5 882 = 0x372  overflow  c == 0x72=114 (behavior in case of overflow is undefined ANSI C)


Download ppt "Fundamental data types"

Similar presentations


Ads by Google