Loading...

C Programming Interview Questions

This article is mainly focused on the most repeatedly asked and the latest updated questions.

C Interview Questions Set-5

Commonly Asked C Programming Interview Questions | Set 5


1. How can we read/write Structures from/to data files?
Ans: To write out a structure we can use fwrite() as Fwrite( &e, sizeof(e),1,fp);Where e is a structure variable. A corresponding fread() invocation can read the structure back from file. calling fwrite() it writes out sizeof(e) bytes from the address &e. Data files written as memory images with fwrite(),however ,will not be portable, particularly if they contain floating point fields or Pointers. This is because memory layout of structures is machine and compiler dependent. Therefore, structures written as memory images cannot necessarily be read back by programs running on other machine, and this is the important concern if the data files you‘re writing will ever be interchanged between machines.


2. Difference between array and pointer?
Ans: Array

  • Array allocates space automatically
  • It cannot be resized
  • It cannot be reassigned
  • sizeof (arrayname) gives the number of bytes occupied by the array
Pointer
  • Explicitly assigned to point to an allocated space
  • It can be sized using realloc()
  • pointer can be reassigned
  • sizeof (p) returns the number of bytes used to store the pointer variable p


3. What are C identifiers?
Ans: These are names given to various programming element such as variables, function, arrays.It is a combination of letter, digit and underscore.It should begin with letter. Backspace is not allowed.


4. What is a preprocessor, what are the advantages of preprocessor?
Ans: A preprocessor processes the source code program before it passes through the compiler.

  • A preprocessor involves the readability of program
  • It facilitates easier modification
  • It helps in writing portable programs
  • It enables easier debugging
  • It enables testing a part of program
  • It helps in developing generalized program


5. What is FILE?
Ans: FILE is a predefined data type. It is defined in stdio.h file.


6. Differentiate between a constant pointer and pointer to a constant?
Ans:

const char *p; //pointer to a const character
char const *p; //pointer to a const character
char * const p; //const pointer to a char variable
const char * const p; // const pointer to a const character


7. What are the advantages of using array of pointers to string instead of an array of strings?
Ans:

  • Efficient use of memory
  • Easier to exchange the strings by moving their pointers while sorting


8. How are structure passing and returning implemented?
Ans: When structures are passed as arguments to functions, the entire structure is typically pushed on the stack, using as many words as are required. Some compilers merely pass a pointer to the structure, though they may have to make a local copy to preserve pass-by-value semantics.
Structures are often returned from functions in a location pointed to by an extra,compilersupplied hidden argument to the function. Some older compilers used a special,static location for structure returns, although this made structure-valued functions non-reentrant, which ANSI C disallows.


9. How can we read/write Structures from/to data files?
Ans: To write out a structure we can use fwrite() as Fwrite( &e, sizeof(e),1,fp);Where e is a structure variable. A corresponding fread() invocation can read the structure back from file. calling fwrite() it writes out sizeof(e) bytes from the address &e. Data files written as memory images with fwrite(),however ,will not be portable, particularly if they contain floating point fields or Pointers. This is because memory layout of structures is machine and compiler dependent. Therefore, structures written as memory images cannot necessarily be read back by programs running on other machine, and this is the important concern if the data files you‘re writing will ever be interchanged between machines.


10. Difference between syntax vs logical error?
Ans: Syntax Error

  • These involves validation of syntax of language
  • Compiler prints diagnostic message
Logical Error
  • logical error are caused by an incorrect algorithm or by a statement mistyped in such a way that it doesn‘t violet syntax of language
  • difficult to find


- Related Topics