| C Interview Questions |
|
| 27 |
When does the compiler not implicitly generate the address of the first element of an array? |
| Ans: |
Whenever an array name appears in an expression such as array as an operand of the size of operator array as an operand of & operator. |
|
|
| 28 |
Is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name? |
| Ans: |
It’s easier for a C compiler to generate good code for pointers than for subscripts.
Array as a string literal initialize for a character array Then the compiler does not implicitly generate the address of the address of the first element of an array. |
|
| 29 |
why n++ executes faster than n+1? |
| Ans: |
The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more
|
|
| 30 |
which expression always return true? Which always return false? |
| Ans: |
Expression if (a=0) always return false expression if (a=1) always return true instructions to carry out this operation. |
|
| 31 |
Is it possible to execute code even after the program exits the main() function? |
| Ans: |
The standard C library provides a function named at exit () 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 at exit() function. |
|
| 32 |
What is the difference between a string and an array? |
| Ans: |
An array is an array of anything. A string is a specific kind of an array with a well-known convention to determine its length. There are two kinds of programming languages: those in which a string is just an array of characters, and those in which it’s a special type. In C, a string is just an array of characters (type char), with one wrinkle: a C string always ends with a NUL character. The “value” of an array is the same as the address of (or a pointer to) the first element; so, frequently, a C string and a pointer to char are used to mean the same thing. An array can be any length. If it’s passed to a function, there’s no way the function can tell how long the array is supposed to be, unless some convention is used. The convention for strings is NUL termination; the last character is an ASCII NUL (‘’) character. |
|
| 33 |
What is a static function? |
| Ans: |
A static function is a function whose scope is limited to the current source file.
Scope refers to the visibility of a function or variable. If the function or variable is visible outside of the current source file, it is said to have global, or external, scope. If the function or variable is not visible outside of the current source file, it is said to have local, or static, scope. |
|
| 34 |
what is a modulus operator? What are the restrictions of a modulus operator? |
| Ans: |
A Modulus operator gives the remainder value. The result of x%y is obtained by (x-(x/y)*y). This operator is applied only to integral operands and cannot be applied to float or double. |
|
| 35 |
Is using exit() the same as using return? |
| Ans: |
The exit () function is used to exit your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main () function, you are essentially returning control to the calling function, which is the operating system. In this case, the return statement and exit () function are similar. |
|
| 36 |
Write the equivalent expression for x%8? |
| Ans: |
X & 7. |
|
| 37 |
Can the size of operator be used to tell the size of an array passed to a function? |
| Ans: |
There’s no way to tell, at runtime, how many elements are in an array parameter just by looking at the array parameter itself. Remember, passing an array to a function is exactly the same as passing a pointer to the first element. |
|
| 38 |
What is the heap? |
| Ans: |
The heap is where malloc (), calloc (), and realloc () get memory. Getting memory from the heap is much slower than getting it from the stack. On the other hand, the heap is much more flexible than the stack. Memory can be allocated at any time and deal located in any order. Such memory isn’t deal located automatically; you have to call free (). Recursive data structures are almost always implemented with memory from the heap. Strings often come from there too, especially strings that could be very long at runtime. If you can keep data in a local variable (and allocate it from the stack), your code will run faster than if you put the data on the heap. Sometimes you can use a better algorithm if you use the heap—faster, or more robust, or more flexible. It’s a tradeoff.
If memory is allocated from the heap, it’s available until the program ends. That’s great
if you remember to deal locate it when you’re done. If you forget, it’s a problem. A “memory leak” is some allocated memory that’s no longer needed but isn’t deal located. If you have a memory leak inside a loop, you can use up all the memory on the heap and not be able to get any more. (When that happens, the allocation functions return a null pointer.) In some environments, if a program doesn’t deal locate everything it allocated, memory stays unavailable even after the program ends. |
|
| 39 |
When should a far pointer be used? |
| Ans: |
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 are declared explicitly far, so they’ll work no matter what code model the program uses.)
A far pointer can refer to information outside the 64KB data segment. Typically, such pointers are used with farmalloc () and such, to manage a heap separate from where all the rest of the data lives. If you use a small-data, large-code model, you should explicitly make your function pointers far.
|
|
| 40 |
What is the stack? |
| Ans: |
The stack is where all the functions’ local (auto) variables are created. The stack also contains some information used to call and return from functions. A “stack trace” is a list of which functions have been called, based on this information. When you start using a debugger, one of the first things you should learn is how to get a stack trace.
The stack is very inflexible about allocating memory; everything must be deal located in exactly the reverse order it was allocated in. For implementing function calls, that is all that’s needed. Allocating memory off the stack is extremely efficient. One of the reasons C compilers generate such good code is their heavy use of a simple stack. There used to be a C function that any programmer could use for allocating memory off the stack. The
memory was automatically deal located when the calling function returned. This was a dangerous function to call; it’s not available anymore. |
|
| 41 |
What is the difference between NULL and NUL? |
| Ans: |
NULL is a macro defined in for the null pointer. NUL is the name of the first character in the ASCII character set. It corresponds to a zero value. There’s no standard macro NUL in C, but some people like to define it. The digit 0 corresponds to a value of 80, decimal. Don’t confuse the digit 0 with the value of ‘’ (NUL)! NULL can be defined as ((void*)0), NUL as ‘’. |
|
| 42 |
Why should I prototype a function? |
| Ans: |
A function prototype tells the compiler what kind of arguments a function is looking to receive and what kind of return value a function is going to give back. This approach helps the compiler ensure that calls to a function are made correctly and that no erroneous type conversions are taking place. |
|