Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.

Similar presentations


Presentation on theme: "Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education."— Presentation transcript:

1 Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.

2 OBJECTIVES In this chapter, you will learn: How to use #include for developing a large program. How to use #define to create macros and macros with arguments. To use conditional compilation to specify portions of a program that should not always be compiled (such as code that assists you in debugging). To display error messages during conditional compilation. To use assertions to test if the values of expressions are correct.

3 Introduction Preprocessing  Occurs before a program is compiled  Inclusion of other files  Definition of symbolic constants and macros  Conditional compilation of program code  Conditional execution of preprocessor directives Format of preprocessor directives  Lines begin with #  Only whitespace characters and comments may appear before a preprocessor directive on a line

4 The #include Preprocessor Directive #include  Copy of a specified file included in place of the directive  #include Search standard library for file Use for standard library files  #include "filename" Search current directory, then standard library Use for user-defined files  Used for: Programs with multiple source files to be compiled together Header file – has common declarations and definitions (classes, structures, function prototypes)

5 The #define Preprocessor Directive: Symbolic Constants #define  Preprocessor directive used to create symbolic constants and macros  Symbolic constants When program compiled, all occurrences of symbolic constant replaced with replacement text  Format #define identifier replacement-text  Example: #define PI  Everything to right of identifier replaces text #define PI = Replaces “ PI ” with " = "  Cannot redefine symbolic constants once they have been created

6 The #define Preprocessor Directive: Macros Macro  Operation defined in #define  A macro without arguments is treated like a symbolic constant  A macro with arguments has its arguments substituted for replacement text, when the macro is expanded  Performs a text substitution – no data type checking  The macro #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) would cause area = CIRCLE_AREA( 4 ); to become area = ( * ( 4 ) * ( 4 ) );

7 The #define Preprocessor Directive: Macros Use Parentheses  Without them the macro, i.e., #define CIRCLE_AREA( x ) PI * x * x area = CIRCLE_AREA(c+2) would lead to area = * c + 2 * c + 2; Multiple arguments #define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) ) would cause rectArea = RECTANGLE_AREA( a + 4, b + 7 ); to become rectArea = ( ( a + 4 ) * ( b + 7 ) );

8 The #define Preprocessor Directive: Macros #undef  Undefine a symbolic constant or macro  If a symbolic constant or macro has been undefined it can later be redefined

9 Conditional Compilation Conditional compilation  Control the execution of preprocessor directives and the compilation of program code  Cast expressions, sizeof, enumeration constants cannot be evaluated in preprocessor directives  Structure similar to if #if !defined( NULL ) #define NULL 0 #endif Determines if symbolic constant NULL has been defined  If NULL is defined, defined( NULL ) evaluates to 1  If NULL is not defined, this function defines NULL to be 0  Every #if must end with #endif  #ifdef short for #if defined (name)  #ifndef short for #if !defined (name)

10 Conditional Compilation Other statements  #elif – equivalent of else if in an if statement  #else – equivalent of else in an if statement "Comment out" portions of code  If the code contains comments, cannot use /*... */ for this purpose  Instead, we should use #if 0 code commented out #endif  To enable code, change 0 to 1

11 Conditional Compilation Debugging #define DEBUG 1 #ifdef DEBUG printf("Variable x = %d\n“, x); #endif  Defining DEBUG to 1 enables code  After code corrected, remove #define statement  Debugging statements are now ignored

12 #error Preprocessor Directives Format: #error tokens  Tokens are sequences of characters separated by spaces "I like C++" has 3 tokens When a #error directive is processed on some systems, it  displays a message including the specified tokens as an error message, e.g. #error 1 – Out of range error  stops preprocessing and prevents program compilation

13 The # and ## Operators #  Causes a replacement text token to be converted to a string surrounded by quotes  The statement #define HELLO( x ) printf( “Hello, ” #x “\n” ); would cause HELLO( John ) to become printf( “Hello, ” “John” “\n” );  Strings separated by whitespace are concatenated when using printf

14 The # and ## Operators ##  Concatenates two tokens  The statement #define TOKENCONCAT( x, y ) x ## y would cause TOKENCONCAT( O, K ) to become OK

15 Line Numbers #line  Renumbers subsequent code lines, starting with the specified constant integer value  File name can be included  #line 100 "myFile.c" Indicates that lines are numbered from 100 beginning with next source code line and that the name of the file for the purpose of any compiler messges is “file1.c” Makes errors more meaningful Line numbers do not appear in source file

16 Predefined Symbolic Constants Four predefined symbolic constants  Cannot be used in #define or #undef

17 Some predefined symbolic constants Note: These identifiers and the defined identifier cannot be used in #define or #undef directives.

18 Assertions assert macro  Defined in the header  Tests value of an expression  If 0 ( false ) prints error message and calls abort  Example: assert( x <= 10 ); If x is greater than 10 when the preceding statement is encountered in a program, an error containing the line number and file name is printed and the program terminates.  If NDEBUG is defined All subsequent assert statements ignored Hence, when assertions are no longer needed, the line #define NDEBUG is inserted in the program file rather than deleting each assertion manually.

19 Chapter 14 Other C Topics In this chapter, you will learn: To redirect keyboard input to come from a file. To redirect screen output to be placed in a file. To write functions that use variable-length argument lists. To process command-line arguments. To assign specific types to numeric constants. To use temporary files. To process external asynchronous events in a program. To allocate memory dynamically for arrays. To change the size of memory that was dynamically allocated previously.

20 Introduction Several advanced topics in this chapter Operating system specific  Usually UNIX or DOS

21 Redirecting Input/Output on UNIX and DOS Systems Standard I/O - keyboard and screen  Redirect input and output Redirect symbol( < )  Operating system feature, not a C feature  UNIX and DOS  $ or % represents command line  Example: $ sum < input  Rather than inputting values by hand, read them from a file Pipe command( | )  Output of one program becomes input of another $ random | sum  Output of random goes to sum

22 Redirecting Input/Output on UNIX and DOS Systems Redirect output ( > )  Determines where output of a program goes  Example: $ random > out Output goes into out (erases previous contents) Append output ( >> )  Add output to end of file (preserve previous contents)  Example: $ random >> out Output is added onto the end of out

23 Variable-Length Argument Lists Functions with unspecified number of arguments  Load  Use ellipsis(... ) at end of parameter list  Need at least one defined parameter  Example: double myfunction ( int i,... );  The ellipsis is used in the prototype and definition of a function with a variable length argument list  printf is an example of a function that can take multiple arguments  The prototype of printf is defined as int printf( const char* format,... );

24 stdarg.h variable-length argument list type and macros

25 fig14_02.c (1 of 2 ) Function average takes an integer i and an unspecified number of additional arguments

26 fig14_02.c (2 of 2 ) va_list variable holds information for other variable-argument macros va_start macro initializes ap with i elements va_arg macro retrieves next element from ap and converts it to type double va_end macro allows the function to facilitate a normal return to main

27 Using Command-Line Arguments Pass arguments to main on DOS or UNIX  Define main as int main( int argc, char *argv[] )  int argc Number of arguments passed  char *argv[] Array of strings Has names of arguments in order  argv[ 0 ] is first argument  Example: $ mycopy input output argc: 3 argv[ 0 ]: “mycopy" argv[ 1 ]: "input" argv[ 2 ]: "output"

28 fig14_03.c (1 of 2 ) Notice that main takes arguments argc and argv The program is passed three arguments: the name of the program, the name of the file to be read from, and the name of the file to write to. The program attempts to open the file specified by argv[1] for reading......then attempts to open the file specified by argv[2] for writing. The program takes each character from inFilePtr and writes them to outFilePtr

29 fig14_03.c (2 of 2 )

30 Notes on Compiling Multiple Source-File Programs Programs with multiple source files  Function definition must be in one file (cannot be split up)  Global variables accessible to functions in same file Global variables must be defined in every file in which they are used  Example: If integer flag is defined in one file To use it in another file you must include the statement extern int flag;  extern States that the variable is defined in another file  Function prototypes can be used in other files without an extern statement Have a prototype in each file that uses the function

31 Program Termination with exit and atexit Function exit  Forces a program to terminate  Parameters – symbolic constants EXIT_SUCCESS or EXIT_FAILURE  Returns an implementation-defined value  Example: exit( EXIT_SUCCESS ); Function atexit atexit( functionToRun );  Registers functionToRun to execute upon successful program termination atexit itself does not terminate the program  Register up to 32 functions (multiple atexit() statements) Functions called in reverse register order  Called function cannot take arguments or return values

32 fig14_04.c (1 of 2 ) atexit function instructs program to call function print when the program terminates exit function forces program to terminate

33 fig14_04.c (2 of 2 ) print function is called at program termination

34 volatile Type Qualifier volatile qualifier  Variable may be altered outside program  Variable not under control of program  Variable cannot be optimized

35 Suffixes for Integer and Floating-Point Constants C provides suffixes for constants  unsigned integer – u or U  long integer – l or L  unsigned long integer – ul, lu, UL or LU  float – f or F  long double – l or L  Examples: 174u 467L 3451ul  If integer constant is not suffixed type determined by first type capable of storing a value of that size ( int, long int, unsigned long int )  If floating point not suffixed of type double

36 More on Files C can process binary files  Not all systems support binary files Files opened as text files if binary mode not supported  Binary files should be used when rigorous speed, storage, and compatibility conditions demand it  Otherwise, text files are preferred Inherent portability, can use standard tools to examine data Function tmpfile  Opens a temporary file in mode "wb+" Some systems may process temporary files as text files  Temporary file exists until closed with fclose or until program terminates Function rewind  Positions file pointers to the beginning of the file

37 Binary file open modes

38 fig14_06.c (1 of 3 ) tmpfile function creates a temporary file

39 fig14_06.c (2 of 3 ) The program takes characters from filePtr and places them in tempFilePtr, converting tabs into spaces The program then takes characters from tempFilePtr and places them in filePtr.

40 fig14_06.c (3 of 3 )

41 Signal Handling Signal  Unexpected event, can terminate program Interrupts ( c), illegal instructions, segmentation violations, termination orders, floating-point exceptions (division by zero, multiplying large floats) Function signal  Traps unexpected events  Header  Receives two arguments: a signal number and a pointer to the signal handling function Function raise  Takes an integer signal number and creates a signal

42 signal.h standard signals

43 fig14_08.c (1 of 3 ) signal function instructs program to call signalHandler if a SIGINT signal is detected raise function causes a SIGINT signal to occur

44 fig14_08.c (2 of 3 )

45 fig14_08.c (3 of 3 ) signal function must be called again after a signal occurs

46 Dynamic Memory Allocation: Functions calloc and realloc Dynamic memory allocation  Can create dynamic arrays calloc( nmembers, size )  nmembers – number of elements  size – size of each element  Returns a pointer to a dynamic array  Key difference between malloc and calloc is that calloc clears the memory it allocates and malloc does not realloc( pointerToObject, newSize )  pointerToObject – pointer to the object being reallocated  newSize – new size of the object  Returns pointer to reallocated memory  Returns NULL if cannot allocate space  If newSize equals 0 then the object pointed to is freed  If pointerToObject equals 0 then it acts like malloc

47 Unconditional Branching with goto Unstructured programming  Use when performance crucial  break to exit loop instead of waiting until condition becomes false goto statement  Changes flow control to first statement after specified label  A label is an identifier followed by a colon (i.e. start: )  Quick escape from deeply nested loop goto start;

48 Performance Tip The goto statement can be used to exit deeply nested control structures efficiently.

49 fig14_09.c Labels for use with goto statement goto statement sends program to specified label

50 The End Thank you very much!


Download ppt "Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education."

Similar presentations


Ads by Google