C INTERVIEW QUESTIONS Part 3

Question: What is the stack?
Answer: 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 deallocated 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 deallocated when the calling function returned. This was a dangerous function to call; it’s not available anymore.

Question: When should a far pointer be used?
Answer : 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.


Question: What is the difference between far and near?
Answer: Some compilers for PC compatibles use two types of pointers.
Near pointers are 16 bits long and can address a 64KB range. far pointers are 32 bits long and can address a 1MB range.

Near pointers operate within a 64KB segment. There’s one segment for function addresses and one segment for data. far pointers have a 16-bit base (the segment address) and a 16-bit offset. The base is multiplied by 16, so a far pointer is effectively 20 bits long. Before you compile your code, you must tell the compiler which memory model to use. If you use a small code memory model, near pointers are used by default for function addresses.

That means that all the functions need to fit in one 64KB segment. With a large-code model, the default is to use far function addresses. You’ll get near pointers with a small
data model, and far pointers with a large data model. These are just the defaults; you can declare variables and functions as explicitly near or far.
Far pointers are a little slower. Whenever one is used, the code or data segment register needs to be swapped out. Far pointers also have odd semantics for arithmetic and comparison. For example, the two far pointers in the preceding example point to the same address, but they would compare as different! If your program fits in a small-data, small-code memory model, your life will be easier.

Question: Is it better to use malloc () or calloc ()?
Answer: Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big:

void *malloc( size_t size );

calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory
at least big enough to hold them all:

void *calloc( size_t numElements, size_t sizeOfElement );

There’s one major difference and one minor difference between the two functions. The major difference is that malloc () doesn’t initialize the allocated memory. The first time malloc () gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). calloc() fills the allocated memory with all zero bits. That means that anything there you’re going to use as a char or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you’re going to use as a pointer is set to all zero bits. That’s usually a null pointer, but it’s not guaranteed. Anything you’re going to use as a float or double is set to all zero bits; that’s a floating-point zero on some types of machines, but not on all.

The minor difference between the two is that calloc () returns an array of objects; malloc () returns one object. Some people use calloc () to make clear that they want an array.

Question: Why should we assign NULL to the elements (pointer) after freeing them?
Answer: 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 your debugger might be able to help you with immediately. Also, there still might be copies of the pointer that refer
to the memory that has been deallocated; that’s the nature of C. Zeroing out pointers after freeing them won’t solve all problems;

Question: When would you use a pointer to a function?
Answer : Pointers to functions are interesting when you pass them to other functions. A function that takes function pointers says, in effect, “Part of what I do can be customized. Give me a pointer to a function, and I’ll call it when that part of the job needs to be done. That function can do its part for me.” This is known as a “callback.” It’s used a lot in graphical user interface libraries, in which the style of a display is built into the library but the contents of the display are part of the application.

As a simpler example, say you have an array of character pointers (char*s), and you want to sort it by the value of the strings the character pointers point to. The standard qsort() function uses function pointers to perform that task. qsort() takes four arguments,

Ø a pointer to the beginning of the array,

Ø the number of elements in the array,

Ø the size of each array element, and

Ø a comparison function, and returns an int.


Question: What does it mean when a pointer is used in an if statement?
Answer: 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.

Question: Is NULL always defined as 0?
Answer: NULL is defined as either 0 or (void*)0. These values are almost identical; either a literal zero or a void pointer is converted automatically to any kind of pointer, as necessary, whenever a pointer is needed (although the compiler can’t always tell when a pointer is needed).

Question: What is a null pointer?
Answer: There are times when it’s necessary to have a pointer that doesn’t point to anything. The macro NULL, defined in , has a value that’s guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void* or char*. Some people, notably C++ programmers, prefer to use 0 rather than NULL.

The null pointer is used in three ways:

1) To stop indirection in a recursive data structure

2) As an error value

3) As a sentinel value

Question :How many levels of pointers can you have?
Answer: 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;
int *********ip09 = & ip08;
int **********ip10 = & ip09;
int ***********ip11 = & ip10;
int ************ip12 = & ip11;
************ip12 = 1; /* i = 1 */

The ANSI C standard says all compilers must handle at least 12 levels. Your compiler might support more.

Question: What is indirection?
Answer: If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable or any other object in memory, you have an indirect reference to its value.

Question: How do you print only part of a string?
Answer: /* Use printf () to print the first 11 characters of source_str. */

printf (“First 11 characters: ‘%11.11s’n”, source_str);

Question: How can I convert a string to a number?
Answer: The standard C library provides several functions for converting strings to numbers of all formats (integers, longs, floats, and so on) and vice versa.

The following functions can be used to convert strings to numbers:

Function Name Purpose

atof() Converts a string to a double-precision floating-point value.

atoi() Converts a string to an integer.

atol() Converts a string to a long integer.
Question; How can I convert a number to a string?
Answer: The standard C library provides several functions for converting numbers of all formats (integers, longs, floats, and so on) to strings and vice versa The following functions can be used to convert integers to strings:

Function Name Purpose

iota() Converts an integer value to a string.

ltoa () Converts a long integer value to a string.

ultoa () Converts an unsigned long integer value to a string.

The following functions can be used to convert floating-point values to strings:

Function Name Purpose

ecvt() Converts a double-precision floating-point value to a string without an embedded decimal point.

fcvt() Same as ecvt(), but forces the precision to a specified number of digits.

gcvt() Converts a double-precision floating-point value to a string with an embedded decimal point.


strtod() Converts a string to a double-precision floating-point value and reports any “leftover” numbers that could not be converted.

strtol() Converts a string to a long integer and reports any “leftover” numbers that could not be converted.

strtoul() Converts a string to an unsigned long integer and reports any “leftover” numbers that could not be converted.