) causes a search for files in certain implementation- specific (include) directories, for example /usr/local/lib The second form ( include””) searches in local directories including the current directory. You can give absolute or relative paths."> ) causes a search for files in certain implementation- specific (include) directories, for example /usr/local/lib The second form ( include””) searches in local directories including the current directory. You can give absolute or relative paths.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.

Similar presentations


Presentation on theme: "CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004."— Presentation transcript:

1 CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004

2 C Pre-processor C has a pre-processor which runs prior to compilation. Directives to the pre- processor are prefixed with the character # which must appear in the first column of text. The directive #include is used for including text that is contained in another file. Two notations are available, one for including system files and one for including user files. Other uses of the pre-processor are for declaring constants and for macros.

3 include directories Examples of each notation are given in turn below: #include #include "MyHeaderFile.h“ The form ( include<> ) causes a search for files in certain implementation- specific (include) directories, for example /usr/local/lib The second form ( include””) searches in local directories including the current directory. You can give absolute or relative paths.

4 Macros The pre-processor also supplies a mechanism for macro substitution with the #define directive. The general form is: #define [ ( ) ] Simple examples are given below: #define GOLDEN_RATIO 1.618 #define ADD(x, y) (x)+(y) #define SWAP(type, num1, num2){ \ type temp; \ temp = num1; \ num1 = num2; \ num2 = temp; \ }

5 Macro Example Notes: It is conventional to use uppercase letters for macro names. The \ character is needed at the end of every line to extend the macro over more than one line. Here we can see the use of a macro: int a=5,b=7; SWAP(int,a,b) The pre-processor will replace the source code line SWAP(int,a,b) with the block of code above substituting int for type, a for num1, and b for num2.

6 Macros v. Functions Macros replace the short hand text with the longer definition. A function definition occurs only once whereas if a macro is used twenty times in your code, in all twenty instances the longer definition will be substituted in, and thus your code will grow. A small macro is convenient for getting a basic piece of functionality implemented. A rule of thumb is to employ macros for small tasks (like SQUARE, MIN, and MAX), but use functions for anything complex. If a macro is used frequently in your code, it would also result in slower compile times, because the pre-processor has to perform all those text replacements.

7 The typedef facility C has a facility for naming types with user-defined identifiers. For example we could declare two long int variables l1 and l2 as follows: typedef long int lint; lint l1, l2; Thus the typedef mechanism allows a programmer to associate a known type with a more meaningful identifier, e.g. typedef int speed; speed max_speed_kph= 205;

8 Explicit type conversions - casts If you want to convert a value from a variable of one type to a variable of another type you can use a cast. For example, to convert an int into a float, you can use the following code: int anInteger = 6; float aFloat; aFloat = (float)anInteger; The general form is to give the target type in parentheses before the source variable.

9 Implicit type conversions An implicit type conversion is a type conversion that is performed by the compiler. C had an extensive set of rules for implicit type conversion. Here is a simple example: int i = 2; float d = 1.32; printf(“Result is %f”, d + i); In the expression above, as in all arithmetical expressions, the target type becomes the largest type in the expression, thus the target type is float and the 2 is converted to 2.0. The result of the expression is 3.32.

10 Intro. to Standard Library We’ve made use of the standard C library already by including the appropriate header files (names end in.h). Here are listed some of the header files. stdio.h - standard input and output functions (like printf, scanf, etc.) stdlib.h - memory management (see later) and communication with environment (abort, exit) limits.h - compiler imposed limits on various data types math.h - all sorts of maths functions, sin, cos, tan, logarithms, powers, square- roots, etc. Employs type double throughout. string.h - functions for manipulating strings, searching for things in strings etc. (see later) ctype.h - useful functions for testing characters, e.g. “is it alphabetic?” etc. (see later)

11 Integer Limits The header file limits.h contains constants defined to show what limits a particular compiler has on the size of integer types, e.g. it contains: INT_MAX, INT_MIN, ULONG_MAX, etc. Consider this situation: int a = 800, b = 700, c; c = a * b; Clearly the arithmetic result is 560,000 but an int may not be big enough to hold this resulting in an overflow. C has no clear rules for this. It is necessary to raise the size of the receiving location (‘c’) to, say, type long int and one of the operands to ensure the calculation is done with enough space. long int c; int a=800, b = 700; c = (long int)a * b;

12 Divide By Zero Consider division by zero: int a = 1, b = 0, c; c = a/b; Such a computation will result in a run time error: It is therefore sensible to look out for attempts to divide by zero and block them: int a = 0, b = 800, c; if (a == 0){ printf("divide by zero error!"); exit(1); } c = b/a; note: exit() is from


Download ppt "CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004."

Similar presentations


Ads by Google