Welcome Guest | Sign in | Register
Preprocessors - C Programming Interview Questions and Answers | LucentBlackBoard | LucentBlackBoard.com

Home > Technical Interviews > Computer Science & Engineering > C Programming > Preprocessors Questions and Answers

16. How can type-insensitive macros be created?

A type-insensitive macro is a macro that performs the same basic operation on different data types. This task can be accomplished by using the concatenation operator to create a call to a type-sensitive function based on the parameter passed to the macro. The following program provides an example:
#include
#define SORT(data_type) sort_ ## data_type
void sort_int(int** i);
void sort_long(long** l);
void sort_float(float** f);
void sort_string(char** s);
void main(void);
void main(void)
{
int** ip;
long** lp;
float** fp;
char** cp;
...
sort(int)(ip);
sort(long)(lp);
sort(float)(fp);
sort(char)(cp);
...
}
This program contains four functions to sort four different data types: int, long, float, and string (notice that only the function prototypes are included for brevity). A macro named SORT was created to take the data type passed to the macro and combine it with the sort_ string to form a valid function call that is appropriate for the data type being sorted. Thus, the string
sort(int)(ip);
translates into
sort_int(ip);
after being run through the preprocessor.

17. 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.

18. 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.

19. 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.

20. 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 theUSING_C constant to determine whether the C++ compiler is being used.




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.