Title: Distance Learning Center
1Distance Learning Center
- Lecture 13
- C Programming Language with Applications
- Melissa Lin, IT Consultant
- HenEm, Inc. Parkville, Missouri
- linm_at_ipfw.edu
- http//www.etcs.ipfw.edu/linm
2Lecture 13
- Preprocessor Directives
- include, define, undefine, etc
- ANSI ltlimits.hgt
- ANSI ltfloat.hgt
- Macros
3The C Preprocessor
- A Macro Processor for Improving Programs
- Readability
- Maintainability
- Flexibility
- Portability
- C Processor Directives Examples define, ifdef
4The C Preprocessor
- C Preprocessor Performs
- Macro Substitution
- Conditional compilation
- Conditional Inclusion
- File inclusion
5C Preprocessor Commands or Directives
- Preprocessor Commands or Directives
- define - Define a macro or a symbolic constant
- undef - Remove a previously defined macro
- include - Insert text from another source file
- if - if condition test, for use in conditionally
include - ifdef - if defined test, for use in
conditionally include - ifndef - if not defined test, for use in
conditionally include - else - For use as alternative include
- endif - Terminate conditional if
- line - Supply a line number for compiler
messages - elif - For use in conditionally include, else
if - pragma - Supply implementation dependent info
to the compiler - error - Produce a compile-time error with a
given message
6C Preprocessor Commands or Directives
- Correct Way of Using Preprocessor Commands or
Directives - Place in the 1st column position for
portability - No space between sign and the preprocessor
directives
7Defining Symbolic Constants
- An Example - Defining Symbolic Constants
- define NL '\n
- define EOS '\0
- define BEEP '\a
- define AND
- define OR
- define NOT !
- define PI 3.1416159
- define e 2.718281828
- define FALSE 0
- define TRUE 1
- define YES TRUE
- define NO FALSE
- define INTEGER int
- define REAL float
8Implementation-Defined limits
- ltlimits.hgt - the limits for integer types
- Defines constants for different sizes of
integer types - ANSI C ltlimits.hgt
- define CHAR_BIT 8
- define CHAR_MAX 127
- define CHAR_MIN -128
- define INT_MAX 32767
- define INT_MIN -32768
- define LONG_MAX 2147483647L
- define LONG_MIN -2147483648L
9Implementation-Defined limits (continue)
- ANSI C ltlimits.hgt (continue)
- define SCHAR_MAX 127
- define SCHAR_MIN -128
- define SHRT_MAX 32767
- define SHRT_MIN -32768
- define UCHAR_MAX 255U
- define UINT_MAX 65536U
- define USHRT_MAX 65535U
- define ULONG_MAX 4294967295UL
10Implementation-Defined float
- ltfloat.hgt
- Defines floating point number constants
- The minimum magnitude and maximum finite floating
numbers can be found in the header file called
ltfloat.hgt - To avoid overflow and underflow problems in
numerical calculation, we should make sure that
the results are within these constraints.
11Implementation-Defined float (continue)
- ltfloat.hgt (continue)
- Some predefine constants are
- FLT_MAX 1E37 float type maximum
- DBL_MAX 1E37 double float type
- maximum
- FLT_MIN 1E-38 float type minimum
- DBL_MIN 1E-38 double float type
- minimum
12Example 13 - 1
- Problem Statement
- Write a program to check the sizes of primitive
data types. Uses the system defined header file
ltlimits.hgt to see the maximum and minimum values
of these types. - Analysis
- We need to be able to see what are defined in
the ltlimit.hgt to get a better idea on the correct
symbolic names for use in our program.
13Example 13 - 1 (continue)
- / limits_size.c - Test under Visual Studio.NET
2003, Windows XP PC system / - include ltstdio.hgt
- include ltlimits.hgt
- include ltfloat.hgt
- void main()
-
- puts("\t\tVisual Studio.NET, Windows XP
Environment") - puts("-------------------------------------------
------") - printf("sizeof(char) d bytes\n",
sizeof(char)) - printf("Number of bits for the character object
d\n", CHAR_BIT) - printf("Minimum value for a signed character
object d\n", SCHAR_MIN)
14Example 13 - 1 (continue)
- printf("Maximum value for a signed character
object - d\n", SCHAR_MAX)
- printf("sizeof(int) d bytes\n", sizeof(int))
- printf("Minimum value for the signed integer
object - d\n", INT_MIN)
- printf("Maximum value for the signed integer
object - d\n", INT_MAX)
- printf("sizeof(unsigned) d bytes\n",
sizeof(unsigned)) - printf("Maximum value for the unsigned integer
object - d\n", UINT_MAX)
- printf("sizeof(long) d bytes\n",
sizeof(long)) - printf("Minimum value for the long integer
object d\n", - LONG_MIN)
- printf("Maximum value for the long integer
object d\n", - LONG_MAX)
15Example 13 - 1 (continue)
- printf("sizeof(short) d bytes\n",
sizeof(short)) - printf("Minimum value for the short integer
object - d\n", SHRT_MIN)
- printf("Maximum value for the short integer
object - d\n", SHRT_MAX)
- printf("sizeof(float) d bytes\n",
sizeof(float)) - printf("Minimum value for the float object
e\n", - FLT_MIN)
- printf("Maximum value for the float object
e\n", - FLT_MAX)
- printf("sizeof(double) d bytes\n",
sizeof(double)) - printf("Minimum value for the float object
e\n", - DBL_MIN)
- printf("Maximum value for the float object
e\n", - DBL_MAX)
16Example 13 1 Output (continue)
- Visual Studio.NET, Windows XP Environment
- -------------------------------------------------
- sizeof(char) 1 bytes
- Number of bits for the character object 8
- Minimum value for a signed character object
-128 - Maximum value for a signed character object 127
- sizeof(int) 4 bytes
- Minimum value for the signed integer object
-2147483648 - Maximum value for the signed integer object
2147483647 - sizeof(unsigned) 4 bytes
- Maximum value for the unsigned integer object
-1 - sizeof(long) 4 bytes
- Minimum value for the long integer object
-2147483648 - Maximum value for the long integer object
2147483647 - sizeof(short) 2 bytes
- Minimum value for the short integer object
-32768 - Maximum value for the short integer object
32767 - sizeof(float) 4 bytes
- Minimum value for the float object
1.175494e-038
17Macros
- Macros
- A define directive with a macroname() that uses
dummy parameters - An example define sum(x,y) ((x)(y))
- Perform same tasks as function
- int sum(int x, int y) return xy or
- float sum(float x, float y)return xy
- Duplication of codes (code expansion for every
macro call), no run time overhead - When to use macros
- The task must not too complex
- To minimize program execution time
- Small block of codes
- To run a more predictable program
18Macros vs Functions
- Macros
- define sum_Macro(x,y) ((x)(y))
- void main()
- int a float b
- a sum_Macro(1 , 2) // Expand to a 1 2
3 - b sum_Macro(3.0, 4.0) // Expand to b 3.0
4.0 7.0 -
-
- Functions
- int sum(int x, int y) return xy
- void main()
- int a float b
- a sum(1 ,2) // Call and return a 3
- b sum(3.0, 4.0) // Compiler error type
mismatch -
-
- Overhead (run time) of calls, Such as remembering
the return address, and jump to function make it
slower to execute a program with functions - Memory efficient because functions are not
expandable (only one piece of function code, no
duplication)
19Example 13-2 Macros and Symbolic Constants
- / example.h /
- / define constants /
- define NL '\n'
- define EOS '\0'
- define BEEP '\a'
- define AND
- define OR
- define NOT !
- define PI 3.1416159
- define e 2.718281828
- define FALSE 0
- define TRUE 1
- define YES TRUE
- define NO FALSE
- define INTEGER int
- define REAL float
20Example 13-2Macros and Symbolic Constants
When cube(3) called, define cube(x)
((sqr(x)) (x)) will be substituted with
sqr(x), and then become ((x)(x)), and finally
(xxx) by the C Preprocessor which is cube(3)
-gt ((sqr(3))(3)) -gt ((3)(3)) (3)) Or if we
plug in a different argument 3y, we can see that
the macro cube(3y) -gt ((sqr(3y)))(3y)) -gt
((3y)(3y))(3y)) can be expanded property
with the help parenthesis.
- / example.h /
- / define constants /
- define MOV(AX, BX) ((AX) (BX))
- / define macros /
- define abs(x) ((x) gt 0? (x)(-x))
- define sqr(x) ((x) (x))
- (NOT define sqr(x) (x x))
- define cube(x) ((sqr(x)) (x) )
- define max(a,b) ((a) gt (b)?(a)(b))
- define min(a,b) ((a) lt (b)?(a)(b))
- define PRINT(exp) printf(exp " d\n", exp)
- define token(x,y) xy
21Example 13-2Macros and Symbolic Constants
- / preproc.c /
- include ltstdio.hgt
- include "example.h"
- / define PRINT(exp) printf(exp " d\n",
exp) / - define PRINTF(exp) printf(exp " f\n", exp)
- define PRTALL(x,y,z) PRINT(x) PRINT(y)
PRINT(z) - undef PI
- define PI1 3.14159
- define PI PI1
22Example 13-2Macros and Symbolic Constants
- / preproc.c /
- void main()
-
- int a, b, c2, d1
- float x
- x PI 2
- PRINTF(x) // printf(x f\n", x)
- a d1 10
- PRINT(a) // printf(exp " d\n", exp)
- PRINT(cube(a)) //printf(cube(a) " d\n",
cube(a)) - // then printf(cube(a) d\n",
- (a)(a)(a))
- b c0 c1 2 a PRINT(b)
- .
23Example 13-2Macros and Symbolic Constants
- / preproc.c /
- void main()
-
- ..
- PRINT(max(a,b)) //printf(max(a,b) "
d\n", max(a,b)) - // printf(max(a,b) " d\n", ((a) gt
(b)?(a)(b))) - PRINT(a - b)
- PRINT(a b)
- PRINT(a / b)
- PRTALL(a,b,c0) // PRINT(a) PRINT(b)
PRINT(c0) - // printf(a d\n", a) printf(b d\n",
b) - // printf(c0 d\n", c0)
- PRINT(token(d,1)) // printf(token(d,1)
d\n", d1) - printf(" d1 d\n", token(d,1)) //
printf(" d1 d\n", d1) -
24Example 13-2Macros and Symbolic Constants
- Output
- x 6.280000
- a 10
- cube(a) 1000
- b 20
- max(a,b) 20
- a - b -10
- a b 200
- a / b 0
- a 10
- b 20
- c0 20
- token(d,1) 10
- d1 10
25Example 13-3 Performance Evaluation Run Time
Measurement
- Program run-time efficiency. It is sometimes
refers to as Elapse Time. - Utilities for estimating timing related tasks.
From the help menu in Microsoft Visual Studio.NET
that lttime.hgt utility functions and constants are
available for use to compare the run time of
macros and functions. - The headers file needed are
- include ltstdio.hgt, include ltlimits.hgt, include
ltfloat.hgt - include lttime.hgt
- Other functions and constants for use are
- define ULONG_MAX 4294967295UL
- clock_t - clock_t type
- clock() - clock function
- We look through the HELP menu and is able to see
the following info regarding to CLOCKS_PER_SEC - The clock function tells how much time the
calling process has used. A timer tick is
approximately equal to 1/CLOCKS_PER_SEC second.
26Example 13-3 Performance Evaluation Run Time
Measurement
- / clocktick.c /
- include ltstdio.hgt
- include ltlimits.hgt
- include ltfloat.hgt
- include lttime.hgt
- define ULONG_MAX 4294967295UL
- // Running time 15.281 sec, and is machine
dependent - void main()
-
- unsigned long n 6000000
- double t
- clock_t start, finish //clock_t type
- start clock() // clock starting time
- while(n--) // Empty loop
-
- finish clock() // clock ending time
- printf(CLOCKS_PER_SEC f, CLOCKS_PER_SEC)
- t (double) (finish - start)/CLOCKS_PER_SEC
- printf("Elapsed time f", t)
27Example 13-4 Performance Comparison Macros vs.
Function
- / macro_vs_function_perf.c /
- / Performance evaluation of a function /
- include ltstdio.hgt
- include lttime.hgt
- define MAX 10000000
- define cube(x) ((x)(x)(x))
- unsigned long cubeF(unsigned long)
- void main()
-
- unsigned long i, n
- float t
- clock_t start, finish
- start clock()
- for(i 0 i lt MAX i)
- n cube(i)
- finish clock()
- t (double) (finish - start)/CLOCKS_PER_SEC
- printf("Macro Call Elapsed time lf sec\n",
t) - .
28Example 13-4 Performance Comparison Macros vs.
Function
- / macro_vs_function_perf.c /
- .
- start clock()
- for(i 0 i lt MAX i)
- n cubeF(i)
- finish clock()
-
- t (double) (finish - start)/CLOCKS_PER_SEC
- printf("Function Call Elapsed time lf sec\n",
t) -
- unsigned long cubeF(unsigned long i)
-
- return(i i i)
-
29Example 13-4 Performance Comparison Macros vs.
Function
- OUTPUTS
- Test 1 For MAX 1000000
- Macro Call Elapsed time 0.010000 sec
- Function Call Elapsed time 0.070000 sec
- Test 2 For MAX 10000000
- Macro Call Elapsed time 0.060000 sec
- Function Call Elapsed time 0.620000 sec
- Test 3 For MAX 100000000
- Macro Call Elapsed time 0.510000 sec
- Function Call Elapsed time 5.959000 sec
30Summary
- Standard limits ltlimits.hgt
- Standard floating-point ltfloat.hgt
- C Preprocessor Directives
- Macros
- Macros vs Functions
- Next - C Array Applications
31Question?