7. BASIC TYPES - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

7. BASIC TYPES

Description:

7' BASIC TYPES – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 29
Provided by: Ritwik3
Learn more at: https://www.cise.ufl.edu
Category:
Tags: basic | types | double

less

Transcript and Presenter's Notes

Title: 7. BASIC TYPES


1
7. BASIC TYPES
2
Numeric Types
  • Cs 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.

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

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

5
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

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

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

8
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)

9
Floating Types
  • C supports three floating types
  • (Ranges shown are typical.)

10
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.7e1 .57e2 570.e-1

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

12
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)

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

14
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' lt c c lt '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' /

15
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
  • Octal
  • Hexadecimal

16
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 \0
  • escape character \33
  • A hexadecimal escape sequence consists of \x
    followed by a hexadecimal number. Examples
  • null character \x0
  • escape 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 /

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

18
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)

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

20
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 lt i) ... / Example 1 /
  • x i / Example 2 /
  • In each case, i is implicitly converted to float
    type.

21
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 accomodate both values.
  • This is often done by converting (promoting) the
    operand of the smaller type to the type of the
    other operand.

22
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 /

23
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 fwhen converted to an integerlies 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.

24
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 /

25
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 /

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

27
The sizeof Operator
  • sizeof works with variables as well as types
  • int i
  • printf("Size of i lu\n", (unsigned long)
    sizeof(i))

28
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.
Write a Comment
User Comments (0)
About PowerShow.com