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

HR Interview Questions With Simple Answers - Top 30.

Here are some very important HR questions which are often asked during Interviews and I believe these answers can really help you to get through....... Click on them to view the answer: 1) Tell Me Something About Yourself ? 2) What Are Your Strengths? 3) What Are Your Weaknesses? 4) How Do You Handle Pressure / Can You Work Well Under Pressure? 5) What Are Your Short Term Goals? 6) What Are Your Long Term Goals? 7) Where Do You See After 5 Years? 8) Why Should We Hire You? 9) What Is Your Salary Expectation? 10) Why Do You Want To Leave Your Current Job? 11) Do You Prefer To Work Alone Or As A Team Player? 12) What Made You Choose Your Major / Stream? 13) Why Didn’t You Pursue A Career In Your Major / Stream? 14) Why Do You Want To Work For Us? 15) Are You Willing To Travel? 16) Are You Willing To Take Risks? 17) What Do You Know About This Company? 18) What Do You Seek From A Job? 19) How Do You Evaluate Success? 20) Dur...

Sample Cover Letter / Job Application.

To The General Manager (HR) [ Name and Designation (Bold Words)] Infosys Limited Delhi – 110001 Uttam Agrawal Gandhi Chowk, Bhartee Street [ Name Bhawanipatna, Kalahandi Contact Address Orissa – 766001 Telephone No.] Ph. no - +91-9438170446 [Sub: Application for the position of ___________ in your esteemed organization.] Respected Sir, (Always use ‘Respected’) With reference to your advertisement dated 14th may’ 08 in The Times of India for the position of ___________ . I would like to place myself as a strong contender before you. OR This has reference to your advertisement dated 14th may’ 08 in The Times of India for the position of ____________. In the said connection I take this opportunity to apply for the said position as a strong contender. /*This is the Introduction i.e the first paragraph of your application/* ( Now the Second Paragraph is the Self Introduction) Try to start the second paragraph with the below mentioned lines: - I have the p...