Lab 10 - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Lab 10

Description:

Type int and double have different internal representations. ... While type double are represented by a binary exponent and mantissa. ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 8
Provided by: davidls4
Category:
Tags: lab | mantissa

less

Transcript and Presenter's Notes

Title: Lab 10


1
Lab 10
  • Representation And Conversion of Numeric Types
  • Difference between Numeric Types
  • Automatic conversion of Data types
  • Explicit Conversion of Data Types
  • Enumeration
  • Note
  • Read the whole Chapter 7.

2
Representation And Conversion of Numeric Types
  • Type int and double have different internal
    representations. Type int values are represented
    as binary numbers with the leftmost bit
    containing of the sign of the number. While type
    double are represented by a binary exponent and
    mantissa. Real number mantissa2exponent.
  • Additional integer types are short, and long.
    Type short represents a smaller range than type
    int. Long represents a larger range than type
    int. (sizeof() can be used to know the size of
    memory block).
  • Additional floating point types are float that
    represents a smaller range than type double and
    long double that represents a larger range than
    type double.
  • Arithmetic with floating-point data may not be
    precise, because not all real numbers can be
    represented exactly. Other types of numerical
    errors include cancellation error, arithmetic
    overflow error and underflow error.

3
  • Type char data are represented by storing a
    binary code value for each symbol. ASCII is a
    commonly used character code.
  • A variable or expression can be explicitly
    converted to another type by writing the new
    types name in parentheses before the value to
    convert. Such a cast is very high precedence
    operation. average(double)sum/N given sum and N
    are declared as int and average as double.
  • You can declare your own data types in C using
    the reserved work typedef
  • Syntax typedef enum identifier_list enum_type
  • Example typedef enum Sunday, Monday, Tuesday,
    Wednesday, Thursday, Friday, Saturday day_t
  • Defining an enumerated type requires listing the
    identifiers that are the values of the type. Each
    value is represented by an integer. Using the
    enumerated types makes the programs more readable
    because the types values are meaningful for a
    particular application.

4
Enumeration Example
  • includeltstdio.hgt
  • typedef enumGood, Bad result
  • int main()
  • result x
  • printf("Enter the result ")
  • scanf("d",x)
  • switch(x)
  • case Good
  • printf("Good \n")
  • break
  • case Bad
  • printf("Bad \n")
  • break
  • default printf("Wrong input \n")
  • return 0

5
Exercises
  • write a program that display the ranges of
    numeric data of type int and long. (Hints use
    limits.h)
  • int k5, m4,n
  • double x1.5, y2.1,z
  • How are zk/m and nxy are evaluated ?
  • What is the difference between the following
    statements given frac is of type double and n1
    and d1 are of type int.
  • frac n1/d1
  • frac (double) n1/ (double) d1
  • frac (double) n1/d1

6
Exercises
  • If squaring 10-20 gives a result of 0, the type
    of error that has occurred is called ..
  • Evaluate the following expressions if
    x10.5,y7.2, m5, and n2
  • x/(double) m
  • (double) (n/m) y
  • Write a c program to display the characters from
    character to the character Z.
  • Evaluate (int) D (int) A, (char) ((int)
    C 2), (int) 6 (int) 7
  • Write a C program to accumulate the weekday hours
    worked.
  • You need to use an enum type loop counter day_t.











  • Use a function print_day to display a string
    corresponding to a value of type day_t.

7
Exercises
  • What does the segment print ?
  • For(ch(int) dchlt(int) n ch3)
  • printf(c,(char)ch)
  • printf(\n)
  • Which of the following can be an enumeration
    constant ? 1-an integer 2-a floating-point
    number, an identifier, a string value.
  • What is wrong with the following enumerated type
    definition ? typedef enum2,3,5,7,11,13
    prime_t
  • Consider the enumerated type definition
  • typedef enumfresh,soph,jr,sr class_t
  • What is the value of each of the following ?
  • A-(int) sr, B-(class_t) 0, C- (class_t)
    ((int)soph1)
Write a Comment
User Comments (0)
About PowerShow.com