Here are Third batch of frequently asked C aptitude questions & these questions have proper answers given so mnc team suggest you should go over these Apptitude questions before your job job interview
17. Why do programmers suggest not to use gets()?
A: Unlike fgets(), gets() cannot be told the size of the buffer it's to read into, so it cannot be prevented from overflowing that buffer. As a general rule, always use fgets().
18. Why is it that errno contain ENOTTY after a call to printf()?
A: Many implementations of the stdio package adjust their behavior slightly if stdout is a terminal. To make the determination, these implementations perform some operation which happens to fail (with ENOTTY) if stdout is not a terminal. Although the output operation goes on to complete successfully, errno still contains ENOTTY. (Note that it is only meaningful for a program to inspect the contents of errno after an error has been reported; errno is not guaranteed to be 0 otherwise.)
19. What is the difference between fgetpos/fsetpos & ftell/fseek? What are fgetpos() & fsetpos() good for exactly?
A: ftell() & fseek() use type long int to represent offsets (positions) in a file, & may therefore be limited to offsets of about 2 billion (2**31-1). The newer fgetpos() & fsetpos() functions, on the other hand, use a special typedef, fpos_t, to represent the offsets. The type behind this typedef, if chosen appropriately, can represent arbitrarily large offsets, so fgetpos() & fsetpos() can be used with arbitrarily huge files. fgetpos() & fsetpos() also record the state associated with multibyte streams.
20. How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work?
A: fflush() is defined only for output streams. Since its definition of "flush" is to complete the writing of buffered characters (not to discard them), discarding unread input would not be an analogous meaning for fflush on input streams.
There is no standard way to discard unread characters from a stdio input stream, nor would such a way necessarily be sufficient, since unread characters can also accumulate in other, OS-level input buffers. You may be able to read & discard characters until \n, or use the curses flushinp()function, or use some system-specific technique.
21. I'm trying to update a file in place, by using fopen mode "r+", reading a certain string, & writing back a modified string, but it's not working.
A: Be sure to call fseek before you write, both to seek back to the beginning of the string you're trying to overwrite, & because an fseek or fflush is always required between reading & writing in the read/write "+" modes. Also, remember that you can only overwrite characters with the same number of replacement characters, & that overwriting in text mode may truncate the file at that point.
22. How can I redirect stdin or stdout to a file from within a program?
A: Use freopen()
23. Once I've used freopen(), how can I get the original stdout (or stdin) back?
A: There isn't a good way. If you need to switch back, the best solution is not to have used freopen() in the first place. Try using your own explicit output (or input) stream variable, which you can reassign at will, while leaving the original stdout (or stdin) undisturbed.
It is barely possible to save away information about a stream before calling freopen(), such that the original stream can later be restored, but the methods involve system-specific calls such as dup(), or copying or inspecting the contents of . a FILE structure, which is exceedingly nonportable & unreliable.
24. How can I arrange to have output go two places at once, e.g. to the screen & to a file?
A: You can't do this directly, but you could write your own printf variant which printed everything twice.
25. How can I read a binary data file properly? I'm occasionally seeing 0x0a & 0x0d values getting garbled, & I seem to hit EOF prematurely if the data contains the value 0x1a.
A: When you're reading a binary data file, you should specify "rb" mode when calling fopen(), to make sure that text file translations do not occur. Similarly, when writing binary data files, use "wb".
+Some Great DBMS aptitude Questions
+Exhaustive collection of Operating System Concepts
+All the C apptitude questions You want
READ ANSWER and EXPLANATION