1 / 11

Numeric Types in C: Integer and Floating Point

This chapter in C programming discusses the two fundamental types of numeric data in C: integer types (signed and unsigned) and floating point types. It covers the sizes and ranges of different integer types, as well as the representation of numbers in decimal, octal, and hexadecimal formats. It also explains the different floating point types and their precision. Additionally, it introduces the character types and character handling functions in C, and discusses type conversion in C.

alejandre
Download Presentation

Numeric Types in C: Integer and Floating Point

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. Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types – contain fractional parts Integers are typically stored in either 16 bits or 32 bits. In a signed number, the left-most bit (the sign bit) is 0 if the number is positive or zero, 1 if it's negative. the largest 16-bit integer has the binary representation: 0111111111111111 = 2^15 - 1

  2. short int unsigned short int = unsigned short int unsigned int long int = long unsigned long int Table 7.1 TYPE Smallest Value Largest Value 16-bit short int -32,768 32,767 unsigned short int 0 65,535 int -32,768 32,767 unsigned int 0 65,535 long int -2,147,483,648 2,147,483,647 unsigned long int 0 4,294,967,295 Table 7.2 32-bit Macros that define the smallest and largest values of each integer type can be found in the < limits.h> header

  3. For maximum portability, use int (or short int) for integers that won't exceed 32,767 and long int for all other integers. constants—numbers that appear in the text of a program, not numbers that are read, written, or computed. C allows integer constants to be written in decimal (base 10), octal (base 8), or hexadecimal (base 16). An octal number is written using only the digits 0 through 7. Each position in an octal number represents a power of 8 (just as each position in a decimal number represents a power of 10). The octal number 237 represents the decimal number = (2 *8^2) + (3*8^1) + (7x8^0) =128+24+7= 159. A hexadecimal (or hex) number is written using the digits 0 through 9 plus the letters A through F, which stand for 10 through 15, respectively. Each position in a hex number represents a power of 16; the hex number 1AF has the decimal value = (1*16^2) + (10*16^1) + (15*16^0) = 256 + 160 + 15 = 431.

  4. Floating Types C provides three floating types, corresponding to different floating-point formats,: float single-precision floating-point 32-bit double double-precision floating-point 64-bit long double extended-precision floating-point Which type to use depends on the amount of precision required Numbers are stored in a form of scientific notation, with each number having three parts: a sign, an exponent, and a fraction. %e, %f, and %g are used for reading and writing single-precision floating-point numbers. Values of types double and long double require slightly different conversions: When reading a value of type double, put the letter l in front of e, f, or g: double d; scant("%lf", &d) ; Use l only in a scanf format string, not a print f string

  5. When reading or writing a value of type long double, put the letter L in front of e, f,or g: long double Id; scanf("%Lf”, &ld) ; printf("%Lf”, Id) Character Types Today's most popular character set is ASCII (American Standard Code for Informa- tion Interchange), a 7-bit code capable of representing 128 characters. In ASCII, the digits 0 to 9 are represented by the codes 0110000-0111001, and the uppercase letters A to Z are represented by 1000001-1011010. Some computers extend ASCII to an 8-bit code so that it can represent 256 characters. Other computers use entirely different character sets. IBM mainframes, for example, rely on an older code named EBCDIC. Future machines may use Unicode, a 16-bit code capable of representing 65,536 characters.

  6. Character-Handling Functions ch = toupper(ch); /* converts ch to upper case */ Programs that call toupper need to have the following # include directive at the top: #include <ctype.h> %c conversion specification allows scanf and printf to read and write single characters char ch; scanf("%c", &ch); /* reads a single character */ printf("%c", ch) ; /* writes a single character */ To force scanfto skip white space before reading a character, put a space in its format string just before %c: scanf(" %c", &ch); /* skips white space, then reads ch */

  7. ch = getchar(); /* reads a character and stores it in ch */ putchar writes a single character: putchar(ch); Using getchar and putchar (rather than scanf and printf) saves time when the program is executed, getchar and putchar are faster than scanf and printf.

  8. The sizeof Operator The sizeof operator allows a program to determine how much memory is required to store values of a particular type. sizeof (char) is always 1, but the sizes of the other types may vary. On a 16-bit machine, sizeof (int) is normally 2 On most 32-bit machines, sizeof (int) is 4.

  9. Type Conversion Implicit conversions are performed on the following occasions: When the operands in an arithmetic or logical expression don't have the same type. (C performs what are known as the usual arithmetic conversions.) When the type of the expression on the right side of an assignment doesn't match the type of the variable on the left side. When the type of an argument in a function call doesn't match the type of the corresponding parameter. When the type of the expression in a return statement doesn't match the function's return type.

  10. C allows the programmer to perform explicit conversions, using the cast operator. A cast expression has the form ( type-name) expression type-name specifies the type to which the expression should be converted. i = (int) f; /* f is converted to int */ Type Definitions Sec. 5.2, we used #define directive to create a macro that could be used as a Boolean type: #define BOOL int There's a better way to set up a Boolean type, though, using a feature known as a type definition: typedef int Bool; Using typedef to define Bool causes the compiler to add Bool to the list of type names that it recognizes. Bool can now be used the same way as built-in type names, in variable declarations and cast expressions. For example, use Bool to declare variables: Bool flag; /* same as int flag; */ The compiler treats Bool as a synonym for int; thus, flag is really nothing more than an ordinary int variable.

  11. Type definitions can make a program more understandable For example, suppose that the variables cash_in and cash_out will be used to store dollar amounts. Declaring Dollars as typedef float Dollars; and then writing Dollars cash_in, cash_out; is more informative than just writing float cash_in, cash_out; Type definitions can also make a program easier to modify. If we later decide that Dollars should really be defined as double, all we need do is change the type definition: typedef double Dollars; The declarations of Dollars variables need not be changed. Without the type definition, we would need to locate all float variables that store dollar amounts and change their declarations. For greater portability, consider using typedef to define new names for integer types.

More Related