1 / 7

Lab 10

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. Representation And Conversion of Numeric Types.

Télécharger la présentation

Lab 10

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. 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 = mantissa*2**exponent. • 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 type’s 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 type’s values are meaningful for a particular application.

  4. Enumeration Example #include<stdio.h> typedef enum{Good, 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 k=5, m=4,n; • double x=1.5, y=2.1,z; • How are z=k/m and n=x*y 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 x=10.5,y=7.2, m=5, and n=2 • 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) ‘d’;ch<(int) ’n’ ;ch+=3) 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 enum{2,3,5,7,11,13} prime_t; • Consider the enumerated type definition : typedef enum{fresh,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)soph+1)

More Related