1 / 29

Fundamental data types

Fundamental data types. Chapter 3 in ABC. #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.

louie
Télécharger la présentation

Fundamental data types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • charsigned charunsigned char • signed short int signed int signed long int • unsigned short int unsigned int unsigned long int • floatdoublelong double • Common (short) form • char signed char unsigned char • short int long • unsigned short unsigned unsigned long • float double long double

  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

  7. Some Character Constants and their Integer Values • 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 The character ‘0‘ has a value of 48

  8. Some Character Constants and their Integer Values 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: 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 printed ABC ... Z is printed 484950 ... 57 is printed

  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 File in stdio.h appear the line: #define EOF (-1)

  12. Constants

  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 23 28 1c 34 15 f 17 37 1fffff 2097151

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

  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?

  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. 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

  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 itself

  19. Integers' Representation s means signnegative 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; 1000 0000 1111 1111 0111 1111 = 127 Underflow 0000 0000 = 0 Overflow 1111 1111 1111 1110 = 254

  21. Float representation • Decimal 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 base 2 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; • } Output: 0.999999

  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 print1.#INF00 or 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. #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 = 2.000000000000000e+00 • sqrt(x) = 1.414213562373095e+00 • pow(x, x) = 4.000000000000000e+00 • Input x:

  27. The usual arithmetic conversion (promotion) • If eitheroperand 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 longcannot 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 • char c; short s; int i; unsigned u; unsigned long ul;float f; double d; long double ld;

  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)

More Related