-
How can you avoid including a header more than once?
One easy technique to avoid multiple inclusions of the same header is to use the #ifndef and #define preprocessor directives. When you create a header for your program, you can #define a symbolic name that is unique to that header. You can use the conditional preprocessor directive named #ifndef to check whether that symbolic name has already been assigned. If it is assigned, you should not include...
-
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. 1) The first advantage is that enumerated constants are generated automatically by the compiler. Conversely, symbolic constants must be manually...
-
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...
-
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...
-
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...
-
How can you check to see whether a symbol is defined?
You can use the #ifdef and #ifndef preprocessor directives to check whether a symbol has been defined (#ifdef) or whether it has not been defined (#ifndef).
-
How many levels of pointers can you have?
The answer depends on what you mean by “levels of pointers.” If you mean “How many levels of indirection can you have in a single declaration?” the answer is “At least 12.” int i = 0; int *ip01 = & i; int **ip02 = & ip01; int ***ip03 = & ip02; int ****ip04 = & ip03; int *****ip05 = & ip04; int ******ip06 = & ip05; int *******ip07 = & ip06; int ********ip08 = & ip07;...
-
What does it mean when a pointer is used in an if statement?
Any time a pointer is used as a condition, it means “Is this a non-null pointer?” A pointer can be used in an if, while, for, or do/while statement, or in a conditional expression.
-
How do you use a pointer to a function?
The hardest part about using a pointer-to-function is declaring it. Consider an example. You want to create a pointer, pf, that points to the strcmp() function. The strcmp() function is declared in this way: int strcmp(const char *, const char * ) To set up pf to point to the strcmp() function, you want a declaration that looks just like the strcmp() function’s declaration, but that has *pf...
-
Why should we assign NULL to the elements (pointer) after freeing them?
This is paranoia based on long experience. After a pointer has been freed, you can no longer use the pointed-to data. The pointer is said to “dangle”; it doesn’t point at anything useful. If you “NULL out” or “zero out” a pointer immediately after freeing it, your program can no longer get in trouble by using that pointer. True, you might go indirect on the null pointer instead, but that’s something...
-
When should a far pointer be used?
Sometimes you can get away with using a small memory model in most of a given program. There might be just a few things that don’t fit in your small data and code segments. When that happens, you can use explicit far pointers and function declarations to get at the rest of memory. A far function can be outside the 64KB segment most functions are shoehorned into for a small-code model. (Often, libraries...
-
What is a “null pointer assignment” error? What are bus errors, memory faults, and core dumps?
These are all serious errors, symptoms of a wild pointer or subscript. Null pointer assignment is a message you might get when an MS-DOS program finishes executing. Some such programs can arrange for a small amount of memory to be available “where the NULL pointer points to” (so to speak). If the program tries to write to that area, it will overwrite the data put there by the compiler. When the program...
-
How can you determine the size of an allocated portion of memory?
You can’t, really. free() can , but there’s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there’s no guarantee the trick won’t change with the next release of the compiler.
-
Is it possible to execute code even after the program exits the main() function?
The standard C library provides a function named atexit() that can be used to perform “cleanup” operations when your program terminates. You can set up a set of functions you want to perform automatically when your program exits by passing function pointers to the atexit() function.
-
Diffenentiate between an internal static and external static variable?
An internal static variable is declared inside a block with static storage class whereas an external static variable is declared outside all the blocks in a file.An internal static variable has persistent storage,block scope and no linkage.An external static variable has permanent storage,file scope and internal linkage.
-
What are advantages and disadvantages of external storage class?
Advantages of external storage class1)Persistent storage of a variable retains the latest value2)The value is globally available Disadvantages of external storage class1)The storage for an external variable exists even when the variable is not needed2)The side effect may produce surprising output3)Modification of the program is difficult4)Generality of a program is affected
-
-
When does the compiler not implicitly generate the address of the first element of an array?
Whenever an array name appears in an expression such asØ array as an operand of the sizeof operatorØ array as an operand of & operatorØ array as a string literal initializer for a character arrayThen the compiler does not implicitly generate the address of the address of the first element of an array
-
What is modular programming?
If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming.
-
What are the advantages of the functions?
Ø Debugging is easierØ It is easier to understand the logic involved in the programØ Testing is easierØ Recursive call is possibleØ Irrelevant details in the user point of view are hidden in functionsØ Functions are helpful...
C Interview Questions
Ans