Skip to main content

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: 
  • strcat 
  • strcmp 
  • strcpy. Use Suitable Examples.

Answer:
  • strcat() Function concatenates two strings together and has the following form: strcat(string1,string2);
When this function is executed, string2 is appended to string1 by removing the null character at the end of string1. C permits nesting of strcat functions as strcat(strcat(string1,string2),string3);

  • strcmp() is the string comparison function defined in string.h header file. It has the following form:
int strcmp ( const char *s1, const char *s2 );
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.
Strcmp performs a case sensitive comparison; if the strings are the same except for a difference in case, then they're countered as being different. Strcmp also passes the address of the character array to the function to allow it to be accessed.

  • strcpy ( ) function is just like a string-assignment operator which take the following form:
char *strcpy ( char *dest, const char *src );

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.

#pragma name
causes compiler to perform "name"


74. Explain The New #error Directive?

Answer: #error is a preprocessor directive used to produce diagnostic messages during debugging.

#error message
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.

For example;
#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.
Both directives cause the entire contents of filename to be inserted into the source code at that point in the program.


Also Read The Following Questions:

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

Comments

  1. This is really helpful, it is so important to prepare for a job interview and if you suspect that you will be asked specific technical questions, then you should know what you are talking about.

    ReplyDelete

Post a Comment

Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...

Experienced guys can share their resumes at admin@interview-made-easy.com

Popular posts from this blog

Tell Me Something About Yourself - Interview Answers.

Try to introduce some of your most important employment-oriented skills as well as your education and accomplishments to the interviewer. Answer to this question is very important because it positions you for the rest of the interview . That's why this statement is often called the "Positioning Statement". One should take the opportunity to show his/her communication skills by speaking clearly and concisely in an organized manner. Since there is no right or wrong answer for this question hence it is important to appear friendly. YOUR ANSWERS CAN BE: 1) I am a person with strong interpersonal skills and have the ability to get along well with people . I enjoy challenges and looking for creative solutions to problems. 2) Besides the details given in my resume, I believe in character values, vision and action. I am quick in learning from mistakes. I am confident that the various tests that you have conducted will corroborate my competencies apti...

What Is (Are) Your Weakness (Weaknesses)? - Interview Answers

There’s a saying – “However small the thorn maybe it has the ability to pierce.” Never say you are a Workaholic, a Perfectionist, etc. because it sounds negative. Try to convert these negative biased qualities into positive ones. For Example, In   place of Workaholic you can say you are a hard worker and always willing to work for extra hours. In place of Perfectionist , you can say you are detail oriented and like to set high standard for work. “Try to specify your weakness as your strength and also highlight it as a benefit.” Your answers can be: 1) One of my weaknesses as I perceive is occasional compromise on time for quality and perfection. 2) I feel I am not very detail-oriented . I’m a person that wants accomplish as much as possible. I realized this hurts quality and therefore I‘m trying hard to find a balance between quality and quantity. 3) At times even when I need help, I try to solve my own problems instead of asking a co-worker who might know...

What Is (Are) Your Strengths? - Interview Answers

This is a simple and popular interview question. Generally people answer it in two ways. There are people who simply state their strength like “I am Young, Dynamic, Intelligent, Smart and so on…”. Such answer is neither right nor wrong but does not help u in any way also. Secondly there are peoples who state their strengths and explain them how he can use his strength for the job and industry. Do not simply state your strength. Everyone has some strength, all you need is to convert them into benefits. In short, try to advertise yourself by converting your features into strengths. “EVERYONE CLAIM THEY ARE HONEST, HARDWORKING, SMART AND SO ON….. BUT WITH AN EXAMPLE IT IS MUCH MORE BELIEVABLE.” Your answers can be : 1) I am a hard worker and because of this ability I can work for additional hours to accomplish my tasks.I am commitment oriented and hence I always enjoy the trust and confidence of my team mates which enables me to perform my duties very easily. 2) I am a...