Home Other Languages C Basic C Questions - 7

Login Form




Basic C Questions - 7 Print E-mail

1. The mostly redundant reserved word auto signifies that the compiler should allocate
storage for a variable ___________.

Which one of the following correctly completes the statement above?

  •  Only on the stack
  •  Only in a register
  •  Only until the end of the block
  •  Only in the data segment
  •  Only on the heap

 

2. Can include files be nested?

Answer 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

 

3. Sample Code
int aFunction(int y)
{
int x = 3 * y;

return x;
}
In the sample code above, with what scope is the variable x implicitly declared?

  •  extern
  •  static
  •  auto
  •  volatile
  •  register

 

4. Write the equivalent expression for x%8?

x&7

 

5. What is the stack?

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.

 

6. Which one of the following will turn off buffering for stdout?

  •  setbuf( stdout, NULL );
  •  setbuf( stdout, FALSE );
  •  setvbuf( stdout, NULL );
  •  setbuf( stdout, _IONBF );
  •  setvbuf( stdout, _IONBF );

 

7. Sample Code
int x=1;
int y=x+++ ++x;

Refer to the above sample code. What is the value of y?

  •  2
  •  3
  •  4
  •  5
  •  The value is implementation dependent because it is not defined in Standard C.

 

8. How can I search for data in a linked list?

Unfortunately, the only way to search a linked list is with a linear search, because the only way a linked list’s members can be accessed is sequentially. Sometimes it is quicker to take the data from a linked list and store it in a different data structure so that searches can be more efficient.

 

9. 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).

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
#else
#include
#endif

 

10. int *x;
      x = (int *) 15;

Is the above code legal?

  •  Yes; upon initialization, the number 15 is stored in a special pointer memory address space.
  •  Yes; a new memory space is allocated to hold the number 15.
  •  No; this syntax is not allowed.
  •  Yes; the pointer x points at the integer in memory location 15.
  •  No; this assigns the number 15 to an unallocated space in memory.