28
7. BASIC TYPES

7. BASIC TYPES

  • Upload
    tuan

  • View
    37

  • Download
    0

Embed Size (px)

DESCRIPTION

7. BASIC TYPES. Numeric Types. C’s basic types include integer types and floating types. Integer types can be either signed or unsigned. A value of a signed type can be either negative, zero, or positive. A value of an unsigned type must be zero or positive. Integer Types. - PowerPoint PPT Presentation

Citation preview

Page 1: 7. BASIC TYPES

7. BASIC TYPES

Page 2: 7. BASIC TYPES

Numeric Types• C’s basic types include integer types and floating types.

• Integer types can be either signed or unsigned.

• A value of a signed type can be either negative, zero, or positive.

• A value of an unsigned type must be zero or positive.

Page 3: 7. BASIC TYPES

Integer Types• C provides six integer types:

• C99 also supports the long long int type, whose values typically range from –263 to 263 – 1, and unsigned long long int, whose values typically range from 0 to 264 – 1.

Page 4: 7. BASIC TYPES

Integer Types

• Integer types can be abbreviated by omitting the word int:long i; /* same as long int i; */

• In general, avoid using unsigned types.

Page 5: 7. BASIC TYPES

Integer Constants• Integer constants may be written in decimal (base 10),

octal (base 8), or hexadecimal (base 16).

• Decimal integer constants contain digits, but must not begin with a zero:15 255 32767

• Octal integer constants contain only digits between 0 and 7, and must begin with a zero:017 0377 077777

Page 6: 7. BASIC TYPES

Integer Constants

• Hexadecimal integer constants contain digits between 0 and 9 and letters between A and F (or a and f), and always begin with 0x:0xf 0xff 0x7fff

The letters in a hexadecimal constant may be either upper or lower case.

Page 7: 7. BASIC TYPES

Reading and Writing Integers• When reading or writing an unsigned integer, use the

letter u (decimal), o (octal), or x (hex) in the conversion specification:unsigned int u;

printf("%u", u); /* writes u in base 10 */

printf("%o", u); /* writes u in base 8 */

printf("%x", u); /* writes u in base 16 */

• When reading or writing a short integer, put the letter h in front of d, o, x, or u:short int s;

printf("%hd", s);

Page 8: 7. BASIC TYPES

Reading and Writing Integers

• When reading or writing a long integer, put the letter l in front of d, o, x, or u:long int l;

printf("%ld", l);

Page 9: 7. BASIC TYPES

Floating Types• C supports three floating types:

(Ranges shown are typical.)

Page 10: 7. BASIC TYPES

Floating Constants• Floating constants can be written in either fixed-point

notation or scientific notation.

• Examples of fixed-point notation:57.0 57.

• Examples of scientific notation:57.0e0 57.0E0 57e0 5.7e1 5.7e+1 .57e2 570.e-1

Page 11: 7. BASIC TYPES

Reading and Writing Floating-Point Numbers

• When reading (not writing) a value of type double, put the letter l in front of e, f, or g:double d;

scanf("%lf", &d);

• Warning: double values must be written using e, f, or g, just like float values:double d;

printf("%f", d);

In C99, however, using %le , %lf, or %lg to print a double value is legal.

Page 12: 7. BASIC TYPES

Reading and Writing Floating-Point Numbers

• When reading or writing a value of type long double, put the letter L in front of e, f, or g:long double ld;

printf("%Lf", ld);

Page 13: 7. BASIC TYPES

The char Type• In addition to the numeric types, C provides one other

basic type: char, the character type.

• A variable of type char can be assigned any character in the underlying character set:char c;

c = 'a';

c = '0';

c = ' ';

• Character constants are written in single quotes, not double quotes.

Page 14: 7. BASIC TYPES

The char Type• C represents values of type char as integers. Any

operation that is allowed for integers is also legal for characters:if ('a' <= c && c <= 'z')

c = c - 'a' + 'A'; /* converts c to upper case */

• Also, characters and integers can be mixed in expressions:c = 'a';

c = c + 1; /* c now contains the letter 'b' */

Page 15: 7. BASIC TYPES

Numeric Escapes• Storing a special character in a variable requires the use

of an escape sequence: c = '\n'; /* store new-line character in c */

• Numeric escapes are used for characters that cannot be represented by character escapes.

• There are two kinds of numeric escapes:OctalHexadecimal

Page 16: 7. BASIC TYPES

Numeric Escapes• An octal escape sequence is written \ddd, where ddd is a one-,

two-, or threedigit octal number representing the position of the character in the underlying character set (usually ASCII). Examples:null character \0escape character \33

• A hexadecimal escape sequence consists of \x followed by a hexadecimal number. Examples:null character \x0escape character \x1b (or \x1B)

• A numeric escape is enclosed in single quotes when used as a character constant:c = '\0'; /* store null character in c */c = '\x1b'; /* store escape character in c */

Page 17: 7. BASIC TYPES

Universal Character Names• C99 also provides universal character names, which

have the form \udddd or \Udddddddd, where each d is a hexadecimal digit.

• Each dddd or dddddddd sequence is a UCS (Universal Character Set) code.

• Unicode is closely related to UCS, and its codes are compatible with those of UCS.

• Universal character names that represent letters and digits are allowed in identifiers.

Page 18: 7. BASIC TYPES

Reading and Writing Characters

• To read or write a character using scanf or printf, use the %c conversion specification:scanf("%c", &c);printf("%c", c);

• A faster alternative: Use getchar and putchar instead of scanf and printf:c = getchar();putchar(c);

Page 19: 7. BASIC TYPES

Reading and Writing Characters• Neither scanf nor getchar skips white space before

reading a character.

• To read the first nonblank character, use scanf in the following way:scanf(" %c", &c); /* skip white space, then read c */

• To detect the end of an input line, test whether the character read by scanf or getchar is a new-line character.

Page 20: 7. BASIC TYPES

Type Conversion• C provides two methods of changing the type of an

expression:Type conversion (done implicitly)Cast expressions (done explicitly)

• Examples of implicit conversion:float x;int i;if (x < i) ... /* Example 1 */x = i; /* Example 2 */

In each case, i is implicitly converted to float type.

Page 21: 7. BASIC TYPES

The Usual Arithmetic Conversions

• The usual arithmetic conversions are applied to operands in an arithmetic or logical expression. Operands are converted to the “smallest” type that will safely accommodate both values.

• This is often done by converting (promoting) the operand of the “smaller” type to the type of the other operand.

Page 22: 7. BASIC TYPES

The Usual Arithmetic Conversions• Examples of the usual arithmetic conversions:

char c;

short s;

int i;

long int l;

float f;

double d;

long double ld;

i = i + c; /* c is converted to int */

i = i + s; /* s is converted to int */

l = l + i; /* i is converted to long */

f = f + l; /* l is converted to float */

d = d + f; /* f is converted to double */

ld = ld + d; /* d is converted to long double */

Page 23: 7. BASIC TYPES

Conversion During Assignment• The usual arithmetic conversions do not apply to assignment.

Instead, the value on the right side of the assignment is converted to the type of the variable on the left side.

• Warning: Converting from a “large” type to a “small” type may not always produce a meaningful result:int i;

float f;

i = f;

This assignment is meaningful only if the value of f—when converted to an integer—lies between the smallest and largest values allowed for an int variable. If f is too large or too small, i will be assigned an apparently meaningless number.

Page 24: 7. BASIC TYPES

Conversion During Assignment

• The conversion of a floating-point number to an integer is done by dropping the fractional part of the number (not by rounding to the nearest integer):int i;

i = 842.97; /* i is now 842 */

i = -842.97; /* i is now -842 */

Page 25: 7. BASIC TYPES

Casting• Casting is used for explicit type conversion.

• A cast expression has the form( type-name ) expression

• Example 1:float f = 3.5, frac_part;

frac_part = f - (int) f; /* frac_part is now 0.5 */

• Example 2:int i = 5, j = 2;

float f;

f = i/j; /* f is now 2.0 */

f = ((float) i)/j; /* f is now 2.5 */

Page 26: 7. BASIC TYPES

The sizeof Operator• The sizeof operator can be used to determine how much

memory a particular type requires:printf("Size of char: %lu\n", (unsigned long) sizeof(char));

printf("Size of int: %lu\n", (unsigned long) sizeof(int));

printf("Size of long int: %lu\n", (unsigned long) sizeof(long));

sizeof returns its answer in bytes.

• sizeof returns an implementation-defined unsigned integer type. As a result, it is best to cast the value returned by sizeof to unsigned long when trying to print it.

Page 27: 7. BASIC TYPES

The sizeof Operator

• sizeof works with variables as well as types:int i;

printf("Size of i: %lu\n", (unsigned long) sizeof(i));

Page 28: 7. BASIC TYPES

Type Definitions• Type definitions create new names for types:

typedef float Dollars;

typedef int Bool;

A common convention is to capitalize the first letter of a typedef name.

• Once a type has been defined, it can be used in the same way as the built-in type names:Dollars cash_in, cash_out;

Bool flag;

• Advantages of type definitions:Can make programs more understandable.

Can make programs easier to modify.

Can make programs more portable.