Q. What are the standard predefined macros?
The ANSI C standard defines six predefined macros for use in the C language:
Macro Name | | Purpose |
__LINE__ | - | Inserts the current source code line number in your code. |
__FILE__ | - | Inserts the current source code filename in your code. |
__DATE__ | - | Inserts the current date of compilation in your code. |
__TIME__ | - | Inserts the current time of compilation in your code. |
__STDC__ | - | Is set to 1 if you are enforcing strict ANSI C conformity. |
__cplusplus | - | Is defined if you are compiling a C++ program. |
Q. How can a program be made to print the line number where an error occurs?
The ANSI C standard includes a predefined macro named __LINE__ that can be used to insert the current source code line number in your program. This can be a very valuable macro when it comes to debugging your program and checking for logic errors. For instance, consider the following portion of code:
int print_document(char* doc_name, int destination)
{
switch (destination)
{
case TO_FILE:
print_to_file(doc_name);
break;
case TO_SCREEN:
print_preview(doc_name);
break;
case TO_PRINTER:
print_to_printer(doc_name);
break;
default:
printf("Logic error on line number %d!\n", __LINE__);
exit(1);
}
}
If the function named print_document() is passed an erroneous argument for the destination parameter (something other than TO_FILE, TO_SCREEN, and TO_PRINTER), the default case in the switch statement traps this logic error and prints the line number in which it occurred. This capability can be a tremendous help when you are trying to debug your program and track down what could be a very bad logic error.
Q. How can a program be made to print the name of a source file where an error occurs?
The ANSI C standard includes a predefined macro named __FILE__ that can be used to insert the current source code filename in your program. This macro, like the __LINE__ macro, can be very valuable when it comes to debugging your program and checking for logic errors. For instance, the following code, includes the filename as well as the line number when logic errors are trapped:
int print_document(char* doc_name, int destination)
{
switch (destination)
{
case TO_FILE:
print_to_file(doc_name);
break;
case TO_SCREEN:
print_preview(doc_name);
break;
case TO_PRINTER:
print_to_printer(doc_name);
break;
default:
printf("Logic error on line number %d in the file %s!\n",
__LINE__, __FILE__);
exit(1);
}
}
Now, any erroneous values for the destination parameter can be trapped, and the offending source file and line number can be printed.
Q. How can you tell whether a program was compiled using C versus C++?
The ANSI standard for the C language defines a symbol named __cplusplus that is defined only when you are compiling a C++ program. If you are compiling a C program, the __cplusplus symbol is undefined. Therefore, you can check to see whether the C++ compiler has been invoked with the following method:
#ifdef __cplusplus /* Is __cplusplus defined? */
#define USING_C FALSE /* Yes, we are not using C */
#else
#define USING_C TRUE /* No, we are using C */
#endif
When the preprocessor is invoked, it sets USING_C to FALSE if the __cplusplus symbol is defined. Otherwise, if__cplusplus is undefined, it sets USING_C to TRUE. Later in your program, you can check the value of the USING_Cconstant to determine whether the C++ compiler is being used.
No comments:
Post a Comment