C Interview Questions

Showing Questions 1 - 20 of 861 Questions
First | Prev | Next | Last Page
Sort by: 
 | 
Jump to Page:
  •  

    When should the volatile modifier be used?

    The volatile modifier is a directive to the compiler’s optimizer that operations involving this variable should not be optimized in certain ways. There are two special cases in which use of the volatile modifier is desirable. The first case involves memory-mapped hardware (a device such as a graphics adaptor that appears to the computer’s hardware as if it were part of the computer’s memory), and...

  •  

    How can you restore a redirected standard stream?

    The preceding example showed how you can redirect a standard stream from within your program. But what if later in your program you wanted to restore the standard stream to its original state? By using the standard C library functions named dup() and fdopen(), you can restore a standard stream such as stdout to its original state. The dup() function duplicates a file handle. You can use the dup()...

  •  

    How do you determine whether to use a stream function or a low-level function?

    Stream functions such as fread() and fwrite() are buffered and are more efficient when reading and writing text or binary data to files. You generally gain better performance by using stream functions rather than their unbuffered low-level counterparts such as read() and write(). In multi-user environments, however, when files are typically shared and portions of files are continuously being locked,...

  •  

    How can I open a file so that other programs can update it at the same time?

    Your C compiler library contains a low-level file function called sopen() that can be used to open a file in shared mode. Beginning with DOS 3.0, files could be opened in shared mode by loading a special program named SHARE.EXE. Shared mode, as the name implies, allows a file to be shared with other programs as well as your own. Using this function, you can allow other programs that are running to...

  •  

    How can I make sure that my program is the only one accessing a file?

    By using the sopen() function you can open a file in shared mode and explicitly deny reading and writing permissions to any other program but yours. This task is accomplished by using the SH_DENYWR shared flag to denote that your program is going to deny any writing or reading attempts by other programs.  For example, the following snippet of code shows a file being opened in shared mode, denying...

  •  

    What will the preprocessor do for a program?

    The C preprocessor is used to modify your program according to the preprocessor directives in your source code. A preprocessor directive is a statement (such as #define) that gives the preprocessor specific instructions on how to modify your source code. The preprocessor is invoked as the first part of your compiler program’s compilation step. It is usually hidden from the programmer because it is...

  •  

    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 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;...

  •  

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

Showing Questions 1 - 20 of 861 Questions
First | Prev | Next | Last Page
Sort by: 
 | 
Jump to Page: