1 / 53

The Fundamental Data Types

The Fundamental Data Types. Outline. Declarations, Expressions, and Assignment The Fundamental Data Types Characters and the data Type char The data Type int The Integral Types short, long, and unsigned The Floating Type. Outline (continueb). The Use of typedef The sizeof Operator

bishop
Download Presentation

The 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. The Fundamental Data Types

  2. Outline • Declarations, Expressions, and Assignment • The Fundamental Data Types • Characters and the data Type char • The data Type int • The Integral Types short, long, and unsigned • The Floating Type

  3. Outline (continueb) • The Use of typedef • The sizeof Operator • The Use of getchar() anb putchar() functions • Mathematical Functions • Conversions and Casts • Hexadecimal and Octal Constants

  4. beclarations, Expressions, anb Assignment • In C, all variables should be declareb at the beginning of the files, functions, and block. #include<stbio.h> int main(void) { int a , b, c; float x, y=3.3, z=-7.7; …

  5. beclarations, Expressions, anb Assignment • In C, block is identified by a curly brace { } • declared variables are only valid within the scope or the block. • Expressions are meaningful combinations of constants, variable, operators, and function calls. • Most expression has a value

  6. a+b; sqrt(7.33333); 5.0*x-tan(9.0/x); Expr can be assigned as a value to a variable expr = expr; b=(3.123*a+b); x=x+1;

  7. The Fundamental Data Types • List of fundamental data type: char [signed] char unsigned char [signed ] short int [signed] int [signed] long int (short) (int) (long) unsigned short int unsigned int unsigned long int float double long double

  8. The Fundamental Data Types • Commonly used list of fundamental data type: char signed char unsigned char short int long unsigned short unsigned unsigned long float double long double

  9. Characters and the Data Type char • char can be used to hold small range of integer • char is interpreted as 1 byte (most machines, 1 byte is implemented as 8 bits) • Therefore, it has 28=256 combinations or can hold for 256 distinct values. • People use char (1 byte) to hold for alphabetic letters (lower and upper cases), digits, punctuations, and special characters such as % and +. • Most machines use ASCII character codes (see Append D)

  10. 'a' 'b' 'c' … 'z' 97 98 99 … 122 'A' 'B' 'C' … 'Z' 65 67 68 … 90 '0' '1' '2' … '9' 48 49 50 … 57 a =A+32 b=B+32 … … z=Z+32 '&' '*' '+' 38 42 43

  11. Characters and the Data Type char • Single quote (')is used to identify a individual character constant • Escape characters

  12. Alert \a 7 Backslash \\ 92 Backspace \b 8 Carriage return \r 13 Double quote \" 34 Form-feed \f 12 Horizontal tab \t 9 New-line \n 10 Null character \0 0 Single quote \' 39 Vertical tab \v 11 Question mark \? 63

  13. printf("%c", '\a'); printf("\"Book on C\" is published"); printf("\'Book on C\' is published"); Octal character expression: printf("%c", '\a'); printf("%c", '\007'); Printf("%c",'\07'); printf("%c", '\7'); But not printf("%c", '7');

  14. char c='a'; printf("%c", c); print out character a printf("%b", c); print out 97 printf("c%c%c, c, c+1, c+2); print out a, b, c char c; int i; for (i='a'; i<='z';++i) print out abc…z printf("%c", i); for (c=65; c<=90;++c) print out ABC .. Z printf("%c", c); for (c='0'; c<='9';++c) print out 48, 48, …57 printf("%c", c);

  15. Characters and the Data Type char • decimal digits 20753 2x104 + 0x103 +7x102 +5x101+3x100 dndn-1…d2d1d0 dnx10n + dn-1x10n-1 +dn-2x10n-2 +…+d2x102+d1x101 +d0x100

  16. Characters and the Data Type char • binary digits (=64+32+1=97) 01100001 0x27 + 1x26 +1x25 +0x24 +0x23 +0x22 +0x21 +1x20 bnbn-1…b2b1b0 bnx2n + bn-1x2n-1 +bn-2x2n-2 +…+b2x22+b1x21 +b0x20

  17. Characters and the Data Type char • The value range of char, signed char, and unsigned char (all 1 byte) • signed char (-128, 127) • unsigned char (0, 255) • char, either (-128, 127) or (0, 255) depending on compiler

  18. The Data Type int • Implemented as either 2 bytes or 4 bytes For a 4 byte system, The total combination is 232 and the value range is -231, -231+1, … -3, -2, -1, 0, 1,2, 3, … 232-1 min max For a 2 byte system, The total combination is 216 and the value range is min -215, -215+1, … -3, -2, -1, 0, 1,2, 3, … 215-1 max

  19. The Data Type int • For 4 byte systems • For 2 byte systems The min value is –231 = –2147483648 about 2 billion The max value is +231-1 = + 2147483647 The min value is –215 = –32768 about 32 thousands The max value is +215-1 = + 32767

  20. The Data Type int • On any machines, we can define a big number #define BIG 2000000000 #define SMALL -2000000000 int main(void) { int a, b=BIG, c=BIG, d=SMALL, e=SMALL; a=b+c; /* out of range , or called overflow */ a=d+e; /* out of range, or called underflow */ …

  21. The Integral Types short, long, and unsigned • Need long, if int is not enough to handle large value of integer • Need short, if we want to save storage space • The range of short and long depend on the implementation, 2 bytes or 4 bytes. • short (2 bytes) The min value is –215 = –32768 about 32 thousands The max value is +215-1 = + 32767

  22. The Integral Types short, long, and unsigned • long (4 bytes) • unsigned The min value is –231 = –2147483648 about 2 billion The max value is +231-1 = + 2147483647 4 bytes: The min value is 0 and the max value is +232-1 = + 4294967295 (4 billion) 2 bytes: The min value is 0 and the max value is +216-1 = + 65535 (2 billion)

  23. The Integral Types short, long, and unsigned • Suffix for literal integer Suffix type example u or U unsigned 37u u or L long 37L ul or UL unsigned long 37UL

  24. The Floating Type • Suffix for floating-point numbers • Floating-point number is composed of three parts: integer, fraction, and exponent Suffix type example f or F float 3.7f l or L long double 3.7L

  25. The Floating Type • Suffix for floating-point numbers • Floating-point number is composed of four parts: integer, decimal point, fraction, and exponent Suffix type example f or F float 3.7f l or L long double 3.7L 33.777e-22

  26. The Floating Type • examples 3.14158 314.149e-2f 0e0 1. Not 3.14,159 314159 .e0 -3.14159

  27. The Floating Type • Storage of floating-point variable • float holds 4 bytes • double holds 8 bytes • Precision: describe the nunber of significant decimal places • Range: describe the limitation of floating –point value, the type of variable can hold

  28. The Floating Type • float has 10-38 to 10+38 • double has 10-308 to 10+308

  29. The Use of typedef • typedef allows programmer to explicitly redefine the identifier of data type • The purpose of introducing typedef is • Abbreviating long declaration • Make portable code by voiding different platform implementation of type size

  30. The Use of typedef • Example: typedef char uppercase; typedef int INCHES, FEET; typedef unsigned long size_t; uppercase u; INCHES length, width;

  31. The sizeof Operator • In C, sizeof() can be used to find out the details about the implementation of data type • Since the different platform implement data type differently, the results should be different.

  32. The sizeof Operator • Standard: sizeof(char)=1 sizeof(char)<=sizeof(short)<=sizeof(int)<=sizeof(long) sizeof(signed)=sizeof(unsigned)=sizeof(int) sizeof(float)<=sizeof(double)<=sizeof(long double)

  33. /* 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; }

  34. 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: 16 bytes

  35. The Use of getchar() anb putchar() • Besides of printf() and scanf() functions, you can use getchar() and putchar() to input characters from keyboard and output characters on screen. • Example:

  36. #include <stdio.h> int main(void) { int c; while ((c = getchar()) != EOF) { putchar(c); putchar(c); } return 0; } -1, read to the end of file or CTR-C I would like to learn C. II wwoouulldd lliikkee ttoo lleeaarrnn CC..

  37. The Use of getchar() anb putchar() • Alphabetic letters either in lowercase or in uppercase are all in a range, as specified in ASCII code. • Therefore, you can use >='a' && <='z' (>=97 && <=122) to check the character is in the range of lowercase >=65 && <=90 to check the character is in the range of uppercase

  38. #include <stdio.h> int main(void) { int c; while ((c = getchar()) != EOF) if (c >= 'a' && c <= 'z') putchar(c + 'A' - 'a'); else putchar(c); return 0; }

  39. Mathematical Functions • There is a standard mathematical functions built in the math.h library, such as sqrt(), pow(), exp(), log(), sin(), cos(), tan(), … • You can find out math functions by reading the math.h file • In UNIX, you need to link math.h while submitting compilation command, using Cc Cfile.c -lm

  40. #include <math.h> #include <stdio.h> int main(void) { double x; printf("\n%s\n%s\n%s\n\n", "The square root of x and x raised", "to the x power will be computed.", "---");

  41. while (1) { /* do it forever, endless loop */ 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; }

  42. Result: The square root of x and x raised to the x power will be computed. --- Input x: 3 x = 3.000000000000000e+00 sqrt(x) = 1.732050807568877e+00 pow(x, x) = 2.700000000000000e+01

  43. Input x: 2 x = 2.000000000000000e+00 sqrt(x) = 1.414213562373095e+00 pow(x, x) = 4.000000000000000e+00 Input x: -1 Sorry, your number must be nonnegative. Input x: 0 x = 0.000000000000000e+00 sqrt(x) = 0.000000000000000e+00 pow(x, x) = 1.000000000000000e+00 Input x: ^C

  44. Mathematical Functions • Use abs() to obtain an absolute value of an integer, and fabs() to obtain an absolute value of a floating-point number

  45. Conversions and Casts • For an expression which has mixed data type, what we expected. • Integral Promotions Store 65 to c Convert to char, while using %c char c='A'; printf("%c\n",c);

  46. Conversions and Casts • In arithmetic expression, automatic conversion occurs char c; short s; int i; long l; unsighed u; unsigned long ul; float f; double d; long double ld; c-s/i return int u*7-i return unsigned u*2.0-i return double f*7-i return float

  47. Conversions and Casts c+3 return int 7*s*ul return unsigned long c+5.0 return double ld+c return long double d+s return double u-ul return unsigned long s*i/l return long u-l return system-dependent

  48. Conversions and Casts • Casting (explicitly converting) (double) i Do not change the type of variable i; It just generates a double value based on the value of I; Example: if i is 4, it generates 4.0 l=(long) ('A'+1.0) f=(float) ((int)d+1); d=(double) i/3; d=(double) (x=77);

  49. Hexadecimal and Octal Constants • Based on base of 16 Hexadecimal digit: 0 1 2 3 4 5 6 7 8 9 A B C D E F Decimal digit: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 hnhn-1…h2h1h0 hnx16n + hn-1x16n-1 +hn-2x16n-2 +…+h2x162+h1x161 +h0x160

  50. Hexadecimal and Octal Constants • Example A0F3C = Ax164 + 0x163 +Fx162 +3x161+Cx160 =659260

More Related