Interview Preparation Exam  >  Interview Preparation Notes  >  Placement Papers - Technical & HR Questions  >  Preprocessors (Part - 2), C Programming Interview Questions

Preprocessors (Part - 2), C Programming Interview Questions | Placement Papers - Technical & HR Questions - Interview Preparation PDF Download

7. What is the benefit of using an enum rather than a #define constant?

The use of an enumeration constant (enum) has many advantages over using the traditional symbolic constant style of #define. These advantages include a lower maintenance requirement, improved program readability, and better debugging capability. The first advantage is that enumerated constants are generated automati- cally by the compiler. Conversely, symbolic constants must be manually assigned values by the programmer. For instance, if you had an enumerated constant type for error codes that could occur in your program, yourenum definition could look something like this:

 

enum Error_Code
{
     OUT_OF_MEMORY,
     INSUFFICIENT_DISK_SPACE,
     LOGIC_ERROR,
     FILE_NOT_FOUND
};

 

In the preceding example, OUT_OF_MEMORY is automatically assigned the value of 0 (zero) by the compiler because it appears first in the definition. The compiler then continues to automatically assign numbers to the enumerated constants, making INSUFFICIENT_DISK_SPACE equal to 1, LOGIC_ERROR equal to 2, and so on.

If you were to approach the same example by using symbolic constants, your code would look something like this:

#define OUT_OF_MEMORY 0

#define INSUFFICIENT_DISK_SPACE 1

#define LOGIC_ERROR 2

#define FILE_NOT_FOUND 3

Each of the two methods arrives at the same result: four constants assigned numeric values to represent error codes. Consider the maintenance required, however, if you were to add two constants to represent the error codes DRIVE_NOT_READY and CORRUPT_FILE. Using the enumeration constant method, you simply would put these two constants anywhere in the enum definition. The compiler would generate two unique values for these constants. Using the symbolic constant method, you would have to manually assign two new numbers to these constants. Additionally, you would want to ensure that the numbers you assign to these constants are unique. Because you don't have to worry about the actual values, defining your constants using the enumerated method is easier than using the symbolic constant method. The enumerated method also helps prevent accidentally reusing the same number for different constants.

Another advantage of using the enumeration constant method is that your programs are more readable and thus can be understood better by others who might have to update your program later. For instance, consider the following piece of code:

 

void copy_file(char* source_file_name, char* dest_file_name)
{
     ...
     Error_Code err;
     ...
      if (drive_ready() != TRUE)
          err = DRIVE_NOT_READY;
     ...
}

 

Looking at this example, you can derive from the definition of the variable err that err should be assigned only numbers of the enumerated type Error_Code. Hence, if another programmer were to modify or add functionality to this program, the programmer would know from the definition of Error_Code what constants are valid for assigning to err.

Conversely, if the same example were to be applied using the symbolic constant method, the code would look like this:

 

void copy_file(char* source_file, char* dest_file)
{
     ...
     int err;
     ...
     if (drive_ready() != TRUE)
          err = DRIVE_NOT_READY;
     ...
}

 

Looking at the preceding example, a programmer modifying or adding functionality to the copy_file()function would not immediately know what values are valid for assigning to the err variable. The programmer would need to search for the #define DRIVE_NOT_READY statement and hope that all relevant constants are defined in the same header file. This could make maintenance more difficult than it needs to be and make your programs harder to understand.

A third advantage to using enumeration constants is that some symbolic debuggers can print the value of an enumeration constant. Conversely, most symbolic debuggers cannot print the value of a symbolic constant. This can be an enormous help in debugging your program, because if your program is stopped at a line that uses an enum, you can simply inspect that constant and instantly know its value. On the other hand, because most debuggers cannot print #define values, you would most likely have to search for that value by manually looking it up in a header file.


8. How are portions of a program disabled in demo versions?

If you are distributing a demo version of your program, the preprocessor can be used to enable or disable portions of your program. The following portion of code shows how this task is accomplished, using the preprocessor directives #if and #endif:

 

int save_document(char* doc_name)
{
#if DEMO_VERSION
     printf("Sorry! You can't save documents using the DEMO version of
             this program!\n");
     return(0);
#endif
     ...
}

 

When you are compiling the demo version of your program, insert the line #define DEMO_VERSION and the preprocessor will include the conditional code that you specified in the save_document() function. This action prevents the users of your demo program from saving their documents.

As a better alternative, you could define DEMO_VERSION in your compiler options when compiling and avoid having to change the source code for the program.

This technique can be applied to many different situations. For instance, you might be writing a program that will support several operating systems or operating environments. You can create macros such asWINDOWS_VERUNIX_VER, and DOS_VER that direct the preprocessor as to what code to include in your program depending on what operating system you are compiling for.

 

9. Is it better to use a macro or a function?

The answer depends on the situation you are writing code for. Macros have the distinct advantage of being more efficient (and faster) than functions, because their corresponding code is inserted directly into your source code at the point where the macro is called. There is no overhead involved in using a macro like there is in placing a call to a function. However, macros are generally small and cannot handle large, complex coding constructs. A function is more suited for this type of situation.

Additionally, macros are expanded inline, which means that the code is replicated for each occurrence of a macro. Your code therefore could be somewhat larger when you use macros than if you were to use functions.

Thus, the choice between using a macro and using a function is one of deciding between the tradeoff of faster program speed versus smaller program size. Generally, you should use macros to replace small, repeatable code sections, and you should use functions for larger coding tasks that might require several lines of code.


10. What is the best way to comment out a section of code that contains comments?

Most C compilers offer two ways of putting comments in your program. The first method is to use the /* and */ symbols to denote the beginning and end of a comment. Everything from the /* symbol to the */ symbol is considered a comment and is omitted from the compiled version of the program. This method is best for commenting out sections of code that contain many comments. For instance, you can comment out a paragraph containing comments like this:

 

/*
This portion of the program contains
a comment that is several lines long
and is not included in the compiled
version of the program.
*/

 

The other way to put comments in your program is to use the // symbol. Everything from the // symbol to the end of the current line is omitted from the compiled version of the program. This method is best for one-line comments, because the // symbol must be replicated for each line that you want to add a comment to. The preceding example, which contains four lines of comments, would not be a good candidate for this method of commenting, as demonstrated here:

 

// This portion of the program contains
// a comment that is several lines long
// and is not included in the compiled
// version of the program.

 

You should consider using the /* and */ method of commenting rather than the // method, because the // method of commenting is not ANSI compatible. Many older compilers might not support the // comments.


11. What is the difference between #include and #include "file" ?

When writing your C program, you can include files in two ways. The first way is to surround the file you want to include with the angled brackets < and >. This method of inclusion tells the preprocessor to look for the file in the predefined default location. This predefined default location is often an INCLUDE environment variable that denotes the path to your include files. For instance, given the INCLUDE variable

INCLUDE=C:\COMPILER\INCLUDE;S:\SOURCE\HEADERS;

using the #include version of file inclusion, the compiler first checks the C:\COMPILER\INCLUDE directory for the specified file. If the file is not found there, the compiler then checks the S:\SOURCE\HEADERS directory. If the file is still not found, the preprocessor checks the current directory.

The second way to include files is to surround the file you want to include with double quotation marks. This method of inclusion tells the preprocessor to look for the file in the current directory first, then look for it in the predefined locations you have set up. Using the #include "file" version of file inclusion and applying it to the preceding example, the preprocessor first checks the current directory for the specified file. If the file is not found in the current directory, the C:\COMPILER\INCLUDE directory is searched. If the file is still not found, the preprocessor checks the S:\SOURCE\HEADERS directory.

The #include <file> method of file inclusion is often used to include standard headers such as stdio.h orstdlib.h. This is because these headers are rarely (if ever) modified, and they should always be read from your compiler's standard include file directory.

The #include "file" method of file inclusion is often used to include nonstandard header files that you have created for use in your program. This is because these headers are often modified in the current directory, and you will want the preprocessor to use your newly modified version of the header rather than the older, unmodified version.


12. Can you define which header file to include at compile time?

Yes. This can be done by using the #if#else, and #endif preprocessor directives. For example, certain compilers use different names for header files. One such case is between Borland C++, which uses the header file alloc.h, and Microsoft C++, which uses the header file malloc.h. Both of these headers serve the same purpose, and each contains roughly the same definitions. If, however, you are writing a program that is to support Borland C++ and Microsoft C++, you must define which header to include at compile time. The following example shows how this can be done:

 

#ifdef __BORLANDC__
#include <alloc.h>
#else
#include <malloc.h>
#endif

 

When you compile your program with Borland C++, the __BORLANDC__ symbolic name is automatically defined by the compiler. You can use this predefined symbolic name to determine whether your program is being compiled with Borland C++. If it is, you must include the alloc.h file rather than the malloc.h file.

 

13. Can include files be nested?

Yes. Include files can be nested any number of times. As long as you use precautionary measures, you can avoid including the same file twice. In the past, nesting header files was seen as bad programming practice, because it complicates the dependency tracking function of the MAKE program and thus slows down compilation. Many of today's popular compilers make up for this difficulty by implementing a concept called precompiled headers, in which all headers and associated dependencies are stored in a precompiled state.

Many programmers like to create a custom header file that has #include statements for every header needed for each module. This is perfectly acceptable and can help avoid potential problems relating to #include files, such as accidentally omitting an #include file in a module.

 

14. How many levels deep can include files be nested?

Even though there is no limit to the number of levels of nested include files you can have, your compiler might run out of stack space while trying to include an inordinately high number of files. This number varies according to your hardware configuration and possibly your compiler.

In practice, although nesting include files is perfectly legal, you should avoid getting nest-crazy and purposely implementing a large number of include levels. You should create an include level only where it makes sense, such as creating one include file that has an #include statement for each header required by the module you are working with.


15. What is the concatenation operator?

The concatenation operator (##) is used to concatenate (combine) two separate strings into one single string. The concatenation operator is often used in C macros, as the following program demonstrates:

 

#include <stdio.h>
#define SORT(x) sort_function ## x
void main(void);
void main(void)
{
     char* array;
     int   elements, element_size;
     ...
     SORT(3)(array, elements, element_size);
     ...
}

 

In the preceding example, the SORT macro uses the concatenation operator to combine the strings sort_function and whatever is passed in the parameter x. This means that the line

SORT(3)(array, elements, element_size);

is run through the preprocessor and is translated into the following line:

sort_function3(array, elements, element_size);

As you can see, the concatenation operator can come in handy when you do not know what function to call until runtime. Using the concatenation operator, you can dynamically construct the name of the function you want to call, as was done with the SORT macro.


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 <stdio.h>
#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: intlongfloat, 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.
The document Preprocessors (Part - 2), C Programming Interview Questions | Placement Papers - Technical & HR Questions - Interview Preparation is a part of the Interview Preparation Course Placement Papers - Technical & HR Questions.
All you need of Interview Preparation at this link: Interview Preparation
85 docs|57 tests

Top Courses for Interview Preparation

FAQs on Preprocessors (Part - 2), C Programming Interview Questions - Placement Papers - Technical & HR Questions - Interview Preparation

1. What is a preprocessor in C programming?
Ans. A preprocessor in C programming is a tool that processes the source code before it is passed to the compiler. It performs various tasks such as macro expansion, file inclusion, and conditional compilation. The preprocessor directives, identified by a "#" symbol, are used to control the behavior of the preprocessor.
2. How does the preprocessor handle macro expansion?
Ans. The preprocessor handles macro expansion by replacing the macro name with its corresponding definition. This process is done before the compilation starts. The macro definition is usually placed at the beginning of the program using the "#define" directive. The preprocessor replaces every occurrence of the macro name with its definition, including any arguments passed to the macro.
3. What is file inclusion in the preprocessor?
Ans. File inclusion in the preprocessor allows us to include the contents of one file into another file. This is done using the "#include" directive. The included file is usually a header file containing function prototypes, constants, or other definitions that are needed in the current file. The preprocessor replaces the "#include" directive with the actual contents of the included file.
4. How does conditional compilation work in the preprocessor?
Ans. Conditional compilation in the preprocessor allows us to selectively include or exclude certain parts of the code based on certain conditions. This is done using the "#if", "#else", and "#endif" directives. The preprocessor evaluates the condition specified in the "#if" directive and includes the code between "#if" and "#else" if the condition is true. If the condition is false, the code between "#else" and "#endif" is included.
5. Can we use preprocessor directives inside functions in C programming?
Ans. No, we cannot use preprocessor directives inside functions in C programming. Preprocessor directives are processed before the compilation starts, and they affect the entire source code. They cannot be used to control the behavior or flow of individual functions. Preprocessor directives are typically placed at the beginning of the program or in header files.
Explore Courses for Interview Preparation exam

Top Courses for Interview Preparation

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Exam

,

Semester Notes

,

practice quizzes

,

video lectures

,

mock tests for examination

,

Important questions

,

C Programming Interview Questions | Placement Papers - Technical & HR Questions - Interview Preparation

,

pdf

,

MCQs

,

past year papers

,

Preprocessors (Part - 2)

,

shortcuts and tricks

,

Preprocessors (Part - 2)

,

Free

,

Previous Year Questions with Solutions

,

Summary

,

Objective type Questions

,

Preprocessors (Part - 2)

,

Extra Questions

,

C Programming Interview Questions | Placement Papers - Technical & HR Questions - Interview Preparation

,

study material

,

Viva Questions

,

Sample Paper

,

C Programming Interview Questions | Placement Papers - Technical & HR Questions - Interview Preparation

,

ppt

;