C Programming Interview Questions With Answers - Part X.
67. What Are The Differences Between Getchar() & Scanf() Functions For Reading Strings?
Answer: The getchar() function is used to get a character from the standard input device. While using the getchar() function, there is no need to hit the return key. It is used only with character data types and it can be used to get continuous stream of characters.
The scanf() function is used to get input from the standard input device. Here, return key should be typed after each input. It can be used for all data types but continuous stream of inputs is not facilitated.
68. Out Of The Functions fgets() & gets(), Which One Is Safer To Use & Why?
Answer: Out of fgets() and gets(), the safer one to use is fgets(). The reason is, gets(), after receiving a string from the input device, gets terminated only when the enter key is pressed. The string can be limitless and it may cause buffer overflow.
69. What Is The Difference Between The Functions strdup() & strcpy()?
Answer: The strdup() function is helpful in duplicating a string to a location decided by the function itself. Here the contents of a string is copied to a memory location and returns the address.
The strcpy() function copies a string to a position demanded by the user. The arguments of the function are the source string and the destination string.
70. Explain, In Brief The Purpose Of The Following String Handling Functions:
- strcpy. Use Suitable Examples.
- strcat() Function concatenates two strings together and has the following form: strcat(string1,string2);
- strcmp() is the string comparison function defined in string.h header file. It has the following form:
strcmp will accept two strings. It will return an integer. This integer will either be:
- Negative if s1 is less than s2.
- Zero if s1 and s2 are equal.
- Positive if s1 is greater than s2.
- strcpy ( ) function is just like a string-assignment operator which take the following form:
strcpy is short for string copy, which means it copies the entire contents of src into dest. The contents of dest after strcpy will be exactly the same as src such that strcmp ( dest, src ) will return 0.src may be a character array variable or a string constant.
71. What Is A Macro? How Is It Different From A C Variable Name? What Are The Advantages Of Using Macro Definitions In A Program?
Answer: A macro is a pre-processor directive which is a program that processes the source code before it passes through the compiler. These are placed in the source program before the main. To define a macro, # define statement is used.
This statement, also known as macro definition takes the following general form:
#define identifier string
The preprocessor replaces every occurrence of the identifier in the source code by the string. The preprocessor directive definition is not terminated by a semicolon. For example
#define COUNT 100
will replace all occurrences of COUNT with 100 in the whole program before compilation.
72. What Are Preprocessor Directives? List Three Types Of Them.
Answer: Preprocessor directives are the commands given to a program known as preprocessor that processes the source code before it passes through the compiler. Each of these preprocessor directives begins with a # symbol. These can be placed anywhere in the program but usually placed before main ( ) or at the beginning of the program.
Before the source code passes through the compiler, it is examined by the preprocessor for any directives. If there are any, appropriate action is taken and the source program is handed over to compiler. These directives can be divided into following three categories:
- Macro substitution directives
- File inclusion directives.
- Compiler control directives.
73. Explain The New #pragma Directive?
Answer: #pragma is an implementation oriented directive that specifies various instructions to be given to the compiler.
causes compiler to perform "name"
74. Explain The New #error Directive?
Answer: #error is a preprocessor directive used to produce diagnostic messages during debugging.
causes the compiler to display the error message and terminate processing on encountering this directive.
75. What Are The Features Of C Preprocessor?
Answer: A preprocessor is a program that processes the source code before it passes through the compiler. It operates under the control of preprocessor directive. These are placed in the source program before the main.
To define a macro, # define statement is used. This statement, also known as macro definition takes the following general form:
#define identifier string
The preprocessor replaces every occurrence of the identifier in the source code by the string. The preprocessor directive definition is not terminated by a semicolon.
#define COUNT 100 will replace all occurrences of COUNT with 100 in the whole program before compilation. Similarly, we can define small functions with the help of macros.
For example, a macro defined as
#define SQUARE(x) (x*x) will calculate square of argument when it is called. This is called macro definition.
76. Give The Differences Between Macros & Functions?
Answer: A macro's definition is expanded into the code each time the macro is encountered in the source code. If your program invokes a macro 100 times, 100 copies of the expanded macro code are in the final program. In contrast, a function's code exists only as a single copy. Therefore, in terms of program size, the better choice is a true function.
When a program calls a function, a certain amount of processing overhead is required in order to pass execution to the function code and then return execution to the calling program. There is no processing overhead in "calling" a macro. In terms of speed, a macro has the advantage.
77. What Is The Difference Between The Following Directives: #include <filename> & #include "filename"?
Answer: Difference between both the syntax is:
- #include "filename": The search for the file is made first in the current directory and then in the standard directories as mentioned in the include search path.
- #include <filename>: This command would look for the file in the standard list of directories.
51) What are the uses of pointers in C Programming?
52) What is the difference between call by value and call by reference (or Pass by value or pass by reference)?
53) What is a huge pointer?
54) What is near pointer?
55) In C, why is the void pointer useful? When would you use it?
56) What is a NULL Pointer? Whether it is same as an uninitialized pointer?
57) Are pointers integer?
58) What does the error ‘Null Pointer Assignment’ means and what causes this error?
59) What is generic pointer in C?
60) How pointer variables are initialized?
...Return To C Questions INDEX.
...Return To C Programming INDEX.
... Return To HR Interview Index