C Interview Questions

R

Richard Heathfield

Eric Sosman said:

#include <stdio.h>
int main(void) {
fputs ("The correct answer is\n", stdout);
fflush (stdout);
if (freopen(NULL, "w", stdin) == NULL) {
fputs ("indeterminate.\n", stderr);
fflush (stderr);
}
else {
fputs ("d: all of the above!\n", stdin);
fflush (stdin); /* 100% legal! */
}
return 0;
}

Ah, you cooked it. Well done.
 
J

Jordan Abel

2006-10-31 said:
Simon Biber said:



Point taken. Now, which is the correct argument to fflush?

a) stdout
b) stdin
c) stderr
d) All the above
e) A and C only
 
K

Keith Thompson

Jordan Abel said:
2006-10-31 <[email protected]>,
Richard Heathfield wrote: [...]
Point taken. Now, which is the correct argument to fflush?

a) stdout
b) stdin
c) stderr
d) All the above
e) A and C only

Depends on what you mean by "correct". fflush(stderr) is legal, but
not likely to be useful. fflush(stdin) does not violate any
constraint, and may be useful in some implementations.

Consider this:

fflush(stderr);
if (0) {
fflush(stdin);
}

Both calls are safe but useless (for different reasons). The second
one might even be useful in a compiler conformance test.

The question is so simple that only an expert could get it wrong.
:cool:}
 
W

Walter Roberson

Keith Thompson said:
fflush(stdin) does not violate any
constraint, and may be useful in some implementations.

Depends what you mean by "constraint": according to the standard
the behaviour is undefined. Not "unspecified" but "undefined".
 
J

Jordan Abel

2006-10-31 said:
Depends what you mean by "constraint": according to the standard
the behaviour is undefined. Not "unspecified" but "undefined".

Well, I would assume "does not violate any constraint" means "is not
a constraint violation" which IIRC is a term defined by the standard.

The point was - a compiler is required to accept it. Undefined behavior
only happens if the call actually happens. And then, as has been pointed
out, only if stdin is not open for writing or updating.
 
K

Keith Thompson

Depends what you mean by "constraint": according to the standard
the behaviour is undefined. Not "unspecified" but "undefined".

What I mean by "constraint" is "constraint", as the standard uses the
term. In particular, 5.1.1.3p1 says:

A conforming implementation shall produce at least one
diagnostic message (identified in an implementation-defined
manner) if a preprocessing translation unit or translation unit
contains a violation of any syntax rule or constraint, even
if the behavior is also explicitly specified as undefined or
implementation-defined. Diagnostic messages need not be produced
in other circumstances.

There are explicit "Constraints" sections throughout the standard.
 
B

Ben Pfaff

Keith Thompson said:
fflush(stderr) is legal, but not likely to be useful.

Quite useful, in fact, in loops like this, at least in "filters"
that already use stdout for something else:

for (;;) {
/* Make sure user can see we're doing something,
even if stderr is line-buffered. */
putc('.', stderr);
fflush(stderr);

/* Do something that takes a while. */
do_something();
}
 
J

Jordan Abel

2006-10-31 said:
Quite useful, in fact, in loops like this, at least in "filters"
that already use stdout for something else:

for (;;) {
/* Make sure user can see we're doing something,
even if stderr is line-buffered. */
/* stderr will never be line-buffered. */
 
B

Ben Pfaff

Jordan Abel said:
/* stderr will never be line-buffered. */

Why not? All I see in C99 is that it will never be fully
buffered:

As initially opened, the standard error stream is not fully
buffered; the standard input and standard output streams are
fully buffered if and only if the stream can be determined
not to refer to an interactive device.
 
C

Chris Torek

/* stderr will never be line-buffered. */

Not even if main() starts with:

setvbuf(stderr, NULL, _IOLBF, 0);

? (Or, use _IOFBF to make stderr fully-buffered.)

(I sped up the 4.xBSD "tar" program enormously by setting its stderr
to a buffered mode, once. I have forgotten the exact details; I
think this was before I wrote my stdio library, which has some
sneaky hacks for "pseudo-buffering" stderr in fprintf() calls.)
 
E

Eric Sosman

Jordan Abel wrote On 10/31/06 17:04,:
/* stderr will never be line-buffered. */

#include <stdio.h>
int main(void) {
setvbuf (stderr, NULL, _IOLBF, BUFSIZ);
/* Now what? */
 
K

Kenneth Brody

Richard said:
(e-mail address removed) said:


"C and C++ Basic Questians" didn't inspire a lot of confidence. Still, let's
see:
[...]

Well, I get a different set of questions when I click on "C and C++
Basic Questians Set 1":
0. What is the output of printf("%d")

Nasal demons.
1. What will happen if I say delete this

Syntax error during compile.
2. Difference between "C structure" and "C++ structure".

Dunno. Perhaps clc++ would know?
3. Diffrence between a "assignment operator" and a "copy constructor"

I didn't think that C had "copy constructor"s.
4. What is the difference between "overloading" and "overridding"?

C doesn't have "overloading".
5. Explain the need for "Virtual Destructor".

Again, no such thing in C.
6. Can we have "Virtual Constructors"?

And again.
7. What are the different types of polymorphism?

And yet again.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top