Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.

Similar presentations


Presentation on theme: "Chapter 7 Simple Data Types and Function Calls Alkar / Demirer."— Presentation transcript:

1 Chapter 7 Simple Data Types and Function Calls Alkar / Demirer

2 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-2 Representation and Conversion of Numeric Types Simple data typeSimple data type is the data type used to store a single value. –e.g., int, double, and char. Operations involving integer are faster than those involving double. Operations with integer are always precise, whereas loss of accuracy may occur when dealing with double.

3 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-3 Internal Formats of Type int and Type double IntegersIntegers are represented by standard binary numbers. –e.g., 7 = 01101. Doubles mantissaexponentDoubles are represented by two sections: mantissa and exponent. –real number = mantissa * 2 exponont –4.0 = 0010 * 2 0001 = 2 * 2 1

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-4 Integer Types in C TypeRange short -32767.. 32767 unsigned short 0.. 65535 int -32767.. 32767 unsgined 0.. 65535 long -2147483647.. 2147483647 unsgined long 0.. 4294967295

5 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-5 Floating-Point Types in C TypeRange float 10 -37.. 10 38 double 10 -307.. 10 308 long double 10 -4931.. 10 4932

6 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-6 An Example to Print Ranges for Positive Numeric Data You can determine the maximal or minimal int and double values of your C environment by the following program. %e is the format in scientific notation

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-7 Numerical Inaccuracies representation errorround-off errorThe representation error (or round-off error) is an error due to coding a real number by a finite number of digits. –e.g., the faction of 1/3 is 0.3333… –It is impossible to code the precise value of 1/3. mantissaThe representation error depends on the number of digits used in the mantissa. –The more bits, the smaller the error.

8 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-8 An Example of the Representation Error for (trial = 0.0; trial != 10.0; trial = trial + 1) { … } The result of adding 0.1 for one hundred times is not exactly 10.0. fail to terminate –The above loop may fail to terminate on some computers.

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-9 Other Problems When Manipulating Numbers Cancellation errorCancellation error is an error resulting from applying an operation to a large number and a small number, and the effect of the smaller number is lost. –e.g., 1000.0 + 0.0000001234 is equal to 1000.0. Arithmetic underflowArithmetic underflow is an error in which a very small computational result is represented as zero. –e.g., 0.00000001 * 10 -1000000 is equal to 0.

10 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-10 Other Problems When Manipulating Numbers Arithmetic overflowArithmetic overflow is an error in which the result is too large to be represented. –The error may be detected in compiling time or run time. –Some machine may let the arithmetic overflow occur and the computational result is unexpected. –e.g., 999999999 * 10 9999999 may become a negative value in some machine.

11 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-11 Automatic Conversion of Data Types (1/2) The data of one numeric type may be automatically converted to another numeric type. e.g., int k = 5, m = 4, n; double x = 1.5, y = 2.1, z; ContextExampleExplanation Expression with binary operator and operands of different types k + x value is 6.5. Value of k is converted to double before the operation.

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-12 Automatic Conversion of Data Types (2/2) ContextExampleExplanation Assignment with type double target variable and type int expression z = k / m; expression value is 1.25; z is 1.0. Expression is evaluated first. Then the result is converted to double. Assignment with type int target variable and type double expression n = x * y; expression value is 3.15; n is 3. Expression is evaluated first. Then the result is converted to int.

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-13 Explicit Conversion of Data Types castC also provides an explicit type conversion operation called cast. For example, frac = n1 / d1; if n1 = 2 and d1 = 4, frac would be 0. CastCast: place the name of the desired type in parentheses immediately before the value to be converted. –e.g., frac = (double)n1 / (double)d1; –frac is evaluated to 0.5.

14 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-14 Representation and Conversion of Type char (1/2) equality operators relational operatorsCharacter values can be compared by the equality operators == and !=, or by the relational operators, and >=. e.g., letter = ‘A’; if (letter < ‘Z’) … Character values may also be compared, scanned, printed, and converted to type int.

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-15 Representation and Conversion of Type char (2/2) Each character has its own unique numeric code. American Standard Code for Information Interchange (ASCII)A widely used standard is called American Standard Code for Information Interchange (ASCII). (See Appendix A in the textbook) printable characters control characters –The printable characters have codes from 32 to 126, and others are the control characters. –For example, the digit characters from ‘0’ to ‘9’ have code values from 48 to 57 in ASCII. The comparison of characters (e.g., ‘a’<‘c’) depends on the the corresponding code values in ASCII.

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-16 An Example of the Conversion between char and int C permits conversion of type char to type int and vice versa. Convert char to int Convert int to char

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-17 Enumerated Types (1/2) Enumerated typeEnumerated type is a data type whose list of values is specified by the programmer. Syntax: typedef enum {identifier_list} enum_type; e.g., typedef enum {entertainment, rent, food, clothing} expense_t;

18 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-18 Enumerated Types (2/2) In the above example, a new data type expense_t is created, and we can declare a variable with this type: e.g., expense_t expense_kind ; The variable expense_kind can be manipulated as any other integer. e.g., switch(expense_kind){ case entertainment: printf(“entertainment”); break; case rent: case food: …

19 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-19 Functions: Arguments are passed by values Argument lists are used to communicate information from the main function to its function subprograms. –Arguments make functions more versatile because they allow us to execute the same function with different sets of data. Return values are used to communicate information from the function subprogram back to the main program. We can use output parameters to return multiple results from a function. When a function is called, it is given a copy of the values that are passed in as arguments. –If you manipulate the value of an argument, it has no impact on its value in the main function –Therefore, these are called input parameters, because they can only bring information into the function, and not back out.

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-20 Function Parameters (1/2) function parametersWhen invoking a function call, we can include function parameters in the parameter list. function parameter prototype of the functionDeclaring a function parameter is accomplished by simply including the prototype of the function in the parameter list. A function parameter

21 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-21 Function Parameters (2/2) double sqrt(double) double sin(double)Suppose there are two existed functions: double sqrt(double), and double sin(double). evaluateevaluate is a function that includes a function parameter as shown in the previous slide. evaluateWe can invoke evaluate as follows. evaluatesqrt evaluate(sqrt, 0.25, 25.0, 100.0); evaluatesin evaluate(sin, 0.0, 3.14159, 0.5*3.14159);

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-22 Example with pass by value void myFunc(int arg); int main(void) { int x = 5; myFunc(x); printf(“%d\n”, x); } void myFunc(int arg) { arg = 4; } /* Output */ 5

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-23 In main: int x = 5; In main, x is assigned the value 5. This places the value 5 in the memory cell reserved for x In this case, it is at address 0x234

24 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-24 In main: myFunc(x); We call the function myFunc and pass it the value of x myFunc allocates a new memory cell for its formal parameter arg The value 5 (a copy of the value in x) is placed in arg

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-25 In myFunc: arg = 4; In myFunc, arg is assigned the value of 4 This places the value 4 in the memory cell for arg This is not the same cell as x

26 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-26 In main: printf(“%d\n”, x); Back in main, when we print out x, the value it points to is still 5.

27 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-27 Functions: Arguments are passed by Reference What if we want our changes to the value in the function to affect the value in the main function? We can accomplish that by passing the address of a variable as argument to a function and manipulate that variable inside the function. In the formal parameter list, we put a * in front of the parameter name. –This defines a pointer, which means that we will be passing the address of the value, rather than the value itself. In the function call, we put an & in front of the argument name. –The & tells the compiler to pass the address of the variable, not its value. When we need to access the value of the argument in the function, we put a * in front of the variable name –This * tells the compiler to access the value pointed to by the address in the variable.

28 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-28 Example with pass by reference void myFunc(int* arg); int main(void) { int x = 5; myFunc(&x); printf(“%d\n”, x); } void myFunc(int* arg) { *arg = 4; } /* Output */ 4

29 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-29 In main: int x = 5; In main, x is assigned the value 5, just like before. The address of the memory cell for x is 0x234 The value of &x is 0x234

30 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-30 In main: myFunc(&x); When we call myFunc, we pass it &x, the address of x This value is stored in the memory cell for arg. arg == 0x234, *arg == 5

31 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-31 In myFunc: *arg = 4; When we set the value of *arg, we are setting the value pointed to by arg – the value at 0x234

32 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-32 In main: printf(“%d\n”, x); Back in main, the value of x is now 4

33 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-33 #include void separate(double num, char *signp, int *wholep, double *fracp); int main(void) { double value, fr; char sn; int whl; printf("Enter a value to analyze> "); scanf("%lf", &value); separate(value, &sn, &whl, &fr); printf("Parts of %.4f\n sign: %c\n", value, sn); printf(" whole number magnitude: %d\n", whl); printf(" fractional part: %.4f\n", fr); return 0; }

34 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-34 void separate(double num, char *signp, int *wholep, double *fracp) { double magnitude; if (num < 0) *signp = ’-’; else if (num == 0) *signp = ’ ’; else *signp = ’+’; magnitude = fabs(num); *wholep = floor(magnitude); *fracp = magnitude - *wholep; } Parts of 35.8170 sign: + whole number magnitude: 35 fractional part: 0.8170

35 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-35 Figure 6.4 Parameter Correspondence for separate(value, &sn, &whl, &fr);

36 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-36 Common Errors with Pointers Note that this is a run-time error. It will not cause a compiler error. Therefore, make sure you set a pointer equal to the address of some variable (of the correct type!) before you attempt to use it. For example: double x = 10.0, y = 20.0, *p_x, *p_y; p_x = &x; printf(“x = %f and y = %f\n”, *p_x, *p_y); ERROR! This will cause the program to crash as p_y has not been initialized.

37 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-37 The Conditional Expression Operator Condition ? expression 1: expression 2 Max_value = (a>b) ? a : b; S = (x <0) ? -1 : x*x

38 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-38 Scope of Names The scope of a name refers to the region of a program where a particular meaning of a name is visible or can be referenced. #define variables scope begins at their definition and ends at the end of the source file. All functions can “see” these variables. The scope of the name of a function begins with the function prototype and ends with the end of the source file. All formal parameter names and local variables are visible only from their declarations to the closing brace of the function in which they are declared.


Download ppt "Chapter 7 Simple Data Types and Function Calls Alkar / Demirer."

Similar presentations


Ads by Google