• +91 9723535972
  • info@interviewmaterial.com

C Interview Questions and Answers

C Interview Questions and Answers

Question - 71 : - How can I search for data in a linked list?

Answer - 71 : - 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.

Question - 72 : - How many levels of pointers can you have?

Answer - 72 : - 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 - 73 : - Is it better to use a macro or a function?

Answer - 73 : - The answer depends on the situation you are writing code for. Macros have the distinct advantage of being more efficient (and faster) than functions, because their corresponding code is inserted directly into your source code at the point where the macro is called. There is no overhead involved in using a macro like there is in placing a call to a function. However, macros are generally small and cannot handle large, complex coding constructs. A function is more suited for this type of situation. Additionally, macros are expanded inline, which means that the code is replicated for each occurrence of a macro. Your code therefore could be somewhat larger when you use macros than if you were to use functions. Thus, the choice between using a macro and using a function is one of deciding between the tradeoff of faster program speed versus smaller program size. Generally, you should use macros to replace small, repeatable code sections, and you should use functions for larger coding tasks that might require several lines of code.

Question - 74 : - What are the standard predefined macros?

Answer - 74 : - 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. _ _ 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 strict ANSI C conformity. _ _cplusplus Is defined if you are compiling a C++ program.

Question - 75 : - What is a const pointer?

Answer - 75 : - The access modifier keyword const is a promise the programmer makes to the compiler that the value of a variable will not be changed after it is initialized. The compiler will enforce that promise as best it can by not enabling the programmer to write code which modifies a variable that has been declared const. A const pointer, or more correctly, a pointer to const, is a pointer which points to data that is const (constant, or unchanging). A pointer to const is declared by putting the word const at the beginning of the pointer declaration. This declares a pointer which points to data that can’t be modified. The pointer itself can be modified. The following example illustrates some legal and illegal uses of a const pointer: const char *str = hello; char c = *str /* legal */ str++; /* legal */ *str = ‘a’; /* illegal */ str[1] = ‘b’; /* illegal */

Question - 76 : - What is a pragma?

Answer - 76 : - The #pragma preprocessor directive allows each compiler to implement compiler-specific features that can be turned on and off with the #pragma statement. For instance, your compiler might support a feature called loop optimization. This feature can be invoked as a command-line option or as a #pragma directive. To implement this option using the #pragma directive, you would put the following line into your code: #pragma loop_opt(on) Conversely, you can turn off loop optimization by inserting the following line into your code: #pragma loop_opt(off)

Question - 77 : - What is #line used for?

Answer - 77 : - The #line preprocessor directive is used to reset the values of the _ _LINE_ _ and _ _FILE_ _ symbols, respectively. This directive is commonly used in fourth-generation languages that generate C language source files.

Question - 78 : - What is the difference between text and binary modes?

Answer - 78 : - Streams can be classified into two types: text streams and binary streams. Text streams are interpreted, with a maximum length of 255 characters. With text streams, carriage return/line feed combinations are translated to the newline n character and vice versa. Binary streams are uninterpreted and are treated one byte at a time with no translation of characters. Typically, a text stream would be used for reading and writing standard text files, printing output to the screen or printer, or receiving input from the keyboard. A binary text stream would typically be used for reading and writing binary files such as graphics or word processing documents, reading mouse input, or reading and writing to the modem.

Question - 79 : - 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().

Answer - 79 : - In multi-user environments, however, when files are typically shared and portions of files are continuously being locked, read from, written to, and unlocked, the stream functions do not perform as well as the low-level functions. This is because it is hard to buffer a shared file whose contents are constantly changing. Generally, you should always use buffered stream functions when accessing nonshared files, and you should always use the low-level functions when accessing shared files

Question - 80 : - What is static memory allocation and dynamic memory allocation?

Answer - 80 : - Static memory allocation: The compiler allocates the required memory space for a declared variable.By using the address of operator,the reserved address is obtained and this address may be assigned to a pointer variable.Since most of the declared variable have static memory,this way of assigning pointer value to a pointer variable is known as static memory allocation. memory is assigned during compilation time. Dynamic memory allocation: It uses functions such as malloc( ) or calloc( ) to get memory dynamically.If these functions are used to get memory dynamically and the values returned by these functions are assingned to pointer variables, such assignments are known as dynamic memory allocation.memory is assined during run time.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners