i need some C/C++ test intervie questions

L

LibraryUser

Ben said:
Out of curiosity, how many people actually do this? I know that
I fairly often refer to it myself while programming and have even
cited bits of it in debates at the office (VMware at the moment,
Stanford later this month).

I do, routinely. I have an alias setup to access (more or less)
the relevant portions, which is why I want a text version of the
standard. N869 comes to mind. The machinery seems to have a
better memory than mine.
 
L

LibraryUser

Richard said:
.... snip ...

Okay, I'm listening. Here's the question: is the behaviour of
this program well-defined?

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char buf[] = "hello world";
char *p = buf;
p[-1] = (toupper)(*p++); /* This is the trouble spot. */
printf("%s\n", buf);
return 0;
}

When I first saw this, I thought "no way". Then, after discussions
with a clc regular and much thumbing of the Standard, I began to
come to the conclusion that it might be okay (for suitably
pedantic values of "okay", obviously -- the code sucks, after all).

But I'm no longer certain, because... well, never mind! Is it
well-defined? If so, why? If not, why not?

No way. The destination p[-1] *may* be calculated on the
statement entry value of p, which will create an illegal pointer
before the actual storage.
 
L

LibraryUser

Bill said:
Didn't he have something to do with the Commodores?

Illustrating the foolishness of such a question. It has nothing
to do with the programmers abilities, but much to do with his
historical knowledge, curiosity, and exposure. In this country
(USA) at least, asking the man/woman in the street simple
historical questions, or even geographical questions, can produce
the most amazing gaffes. Leno takes advantage of this to
generate humor on his program.

I cannot remember the name offhand (I will refer to him as R),
but there was an Indian mathematician, totally untutored, who
wrote GH Hardy with some observations, and was thus discovered as
a major mathematical force. Hardy brought R to Cambridge (or
Oxford??) and R made major contributions to the mathematical
world. However R initially knew absolutely nothing about the
historical development and contributors to mathematics, such as
Newton, Gauss, Dedekind, and many others.
 
M

Martin Dickopp

Richard Heathfield said:
Okay, I'm listening. Here's the question: is the behaviour of this program
well-defined?

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char buf[] = "hello world";
char *p = buf;
p[-1] = (toupper)(*p++); /* This is the trouble spot. */
printf("%s\n", buf);
return 0;
}

This is certainly an example where I would have quoted the standard if I'd
been the first to answer. :)

First of all, `p' is obviously modified and used to determine the
/location/ to be stored to (as opposed to the value to store), so the
behavior is undefined according to 6.5#2 /unless/ there's an intervening
sequence point. It remains to be shown that the sequence point before the
`toupper' call is not guaranteed to happen between these two operations.
According to 6.5#3, "the order of evaluation of subexpressions and the
order in which side effects take place are both unspecified."

Martin
 
T

Tom Zych

Richard said:
...is the behaviour of this program well-defined?

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char buf[] = "hello world";
char *p = buf;
p[-1] = (toupper)(*p++); /* This is the trouble spot. */
printf("%s\n", buf);
return 0;
}

I'm not a standard C guru (yet). My answer is, it doesn't matter,
because even if one can dig into the standard and show that this
works reliably, it's bad code. You can't tell if it works without
being a language lawyer, and it offers no advantage over clear and
reliable code. It should be rewritten and the original coder
should be shot[1].

[1] If it had been in actual production code instead of a puzzle,
that is.
 
T

Tom Zych

Illustrating the foolishness of such a question. It has nothing
to do with the programmers abilities, but much to do with his
historical knowledge, curiosity, and exposure.
I cannot remember the name offhand (I will refer to him as R),
Ramanujan.

but there was an Indian mathematician, totally untutored, who
wrote GH Hardy with some observations, and was thus discovered as
a major mathematical force. Hardy brought R to Cambridge (or
Oxford??) and R made major contributions to the mathematical
world. However R initially knew absolutely nothing about the
historical development and contributors to mathematics, such as
Newton, Gauss, Dedekind, and many others.

An interesting parallel. IIRC, Ramanujan learned from one book and
figured out the rest on his own. You can do that with math[1],
because math is self-contained and deductive. Theoretically, I
suppose one could read one C book (which didn't mention Ritchie or
other C luminaries), then use a compiler and man pages to become
expert.

OTOH, I think curiosity counts for a lot in programming. And
experience is hard to obtain without exposure to a wider body of
literature.

[1] Well, if you're Ramanujan, you can :)
 
I

Irrwahn Grausewitz

LibraryUser said:
I cannot remember the name offhand (I will refer to him as R),
but there was an Indian mathematician, totally untutored, who
wrote GH Hardy with some observations, and was thus discovered as
a major mathematical force. Hardy brought R to Cambridge (or
Oxford??) and R made major contributions to the mathematical
world. However R initially knew absolutely nothing about the
historical development and contributors to mathematics, such as
Newton, Gauss, Dedekind, and many others.

His name actually was Srinivasa Ramanujan (1887-1920), if someone
wants to search for a biography. He moved to England in 1913, and,
unluckily, died young (as you can see from the dates above),
suffering from tubercolosis.
 
M

Martin Dickopp

Tom Zych said:
IIRC, Ramanujan learned from one book and figured out the rest on his
own. You can do that with math[1], because math is self-contained and
deductive.

Actually, Kurt Gödel proved that no complete axiomatic system can be free
of undecidable theorems. So not only is mathematics not self-contained and
deductive, but it couldn't even be replaced by an alternative complete
axiomatic system which is. :)

Martin
 
G

goose

Kevin D. Quitt said:
That would be me. 8o)



Well, I am - but I'm not perfect.

glad you realise it :) no one is. but ttry telling a prima donna
that they write faulty code.
Anybody who correctly points out an error (other than a deliberate one)
wins.


Nor would I. Nor have I.

unfortunately, i /have/ and i might again in the future ... through
no choice of my own (seems to be a downturn at the moment).
I'm generally not interested in seeing an applicant's code unless there's
some special reason for it.

<grin> not even my partially complete rtos written mostly in std C
(with all the platform-specific stuff in seperate files) ???
but i'm *proud* (or rather, was) of it...
All that's going to show me is their
programming idiom, and that can usually be changed if it's egregious. I'd
rather spend the time talking about their programming philosophy and
attitude, and trying to determine if they're going to be a good fit with
the existing crew.

or you can ask them to name the ergs on clc ;-)

goose,
 
G

goose

Kevin D. Quitt said:
I repeat: this is not a pass/fail exam, and not meant to be scientific.
It is meant mostly as a bullshit detector for people who claim skills they
don't have. When I'm hiring a C programmer, I know about what level of
expertise I'm looking for, and my test includes questions about other than
just C programming.

ah ... see .. any test that tests me on anything /other/ than
C (or any programming language I may claim to know), i'd do badly
on. my experience is a rather limited problem domain.

go on. ask me about emv :)

While I do not want to make the test public, I will send a copy to anybody
who asks.

ok! my address above is valid.
The answer sheet will cost you, though. 8o)

i'll make my own :) just dont do a "sco" on me and sue me for stealing
your code :)

goose,
 
B

Ben Pfaff

ah ... see .. any test that tests me on anything /other/ than
C (or any programming language I may claim to know), i'd do badly
on. my experience is a rather limited problem domain.

go on. ask me about emv :)

What's emv?
 
T

Tom Zych

Martin said:
IIRC, Ramanujan learned from one book and figured out the rest on his
own. You can do that with math[1], because math is self-contained and
deductive.
Actually, Kurt Gödel proved that no complete axiomatic system can be free
of undecidable theorems. So not only is mathematics not self-contained and
deductive, but it couldn't even be replaced by an alternative complete
axiomatic system which is. :)

Hmm. I don't really see how Gödel's theorem contradicts what I
said. IIRC, he proved that in any sufficiently complex formal
system, there are true statements that can't be proved
algorithmically, within the system. That doesn't mean they can't
be proved some other way; he had to do that to prove his result.

"Self-contained" != "complete". I meant that, unlike most
subjects, you don't have to know anything about the real world to
do math, because any resemblance between math and the real world
is purely coincidental. Damned useful, but still coincidental.
 
E

Eric Sosman

Richard said:
Okay, I'm listening. Here's the question: is the behaviour of this program
well-defined?

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char buf[] = "hello world";
char *p = buf;
p[-1] = (toupper)(*p++); /* This is the trouble spot. */
printf("%s\n", buf);
return 0;
}

When I first saw this, I thought "no way". Then, after discussions with a
clc regular and much thumbing of the Standard, I began to come to the
conclusion that it might be okay (for suitably pedantic values of "okay",
obviously -- the code sucks, after all).

But I'm no longer certain, because... well, never mind! Is it well-defined?
If so, why? If not, why not?

Stick with your first impression, I'd say. Undefined
behavior: Without intervening sequence points, `p' is both
modified and read for a purpose other than to determine the
new value (6.5, paragraph 2).

The sequence point at the function call is a red herring.
It separates the `*p++' and its side-effect(s) from the body
of the function, but does not separate them from the evaluation
of the lvalue `p[-1]'.
 
R

Richard Heathfield

Tom said:
Richard said:
...is the behaviour of this program well-defined?

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char buf[] = "hello world";
char *p = buf;
p[-1] = (toupper)(*p++); /* This is the trouble spot. */
printf("%s\n", buf);
return 0;
}

I'm not a standard C guru (yet). My answer is, it doesn't matter,
because even if one can dig into the standard and show that this
works reliably, it's bad code.

This is a very important aspect of the answer, which no self-respecting
interviewee should ignore as a possible lifeline if they are floundering on
the actual sequence point issue.
You can't tell if it works without
being a language lawyer, and it offers no advantage over clear and
reliable code. It should be rewritten and the original coder
should be shot[1].

Yes, I think that's a fair summary. :) My thanks to all those who
answered.
 
A

Arthur J. O'Dwyer

Without looking it up in any reference whatsoever, please tell me which way
round the middle two arguments of fread go.


I agree with your point, of course, but here's the mnemonic I eventually
managed to bash into my brain, and which might help some people:

Little number first, big number second.

unsigned char buffer[10];
fread(buffer, 1, 10, fp);

(Of course, this works only if you're like me and do fread/fwrite
with unsigned char buffers, rather than reading and writing whole
structs at a time. But that's not portable, so I know Richard
doesn't do it. ;-)

It works for memset, too:

Little number first, big number second.

unsigned char buffer[10];
memset(buffer, 0x00, 10);


It *doesn't* work for qsort. Now why on earth did they switch
those parameters around for qsort? Geez... but qsort has enough
other quirky bits that I end up looking it up anyway, just to
make sure I got the int (*)(const void *, const void *) correct.

HTH,
-Arthur
 
C

Christian Bau

Eric Sosman said:
Richard said:
Okay, I'm listening. Here's the question: is the behaviour of this program
well-defined?

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char buf[] = "hello world";
char *p = buf;
p[-1] = (toupper)(*p++); /* This is the trouble spot. */
printf("%s\n", buf);
return 0;
}

When I first saw this, I thought "no way". Then, after discussions with a
clc regular and much thumbing of the Standard, I began to come to the
conclusion that it might be okay (for suitably pedantic values of "okay",
obviously -- the code sucks, after all).

But I'm no longer certain, because... well, never mind! Is it well-defined?
If so, why? If not, why not?

Stick with your first impression, I'd say. Undefined
behavior: Without intervening sequence points, `p' is both
modified and read for a purpose other than to determine the
new value (6.5, paragraph 2).

Even if it were not undefined behaviour, the evaluation order is
unspecified, so you don't know whether the result would be stored int
buf [-1] or buf [0] and storing into buf [-1] is certainly not a good
idea.

Depending on your impression of the interviewer, that might be the
better answer. The interviewer might never have heard of "undefined
behavior" or "C Standard", but any interviewer should be able to
understand that this code could store into buf [-1].
 
G

goose

Ben Pfaff said:
What's emv?

(in case you were serious:)
smartcard standard for financial transactions ...europay/mastercard/visa

goose,
by day, i'm a boring terminal developer!!! but by *night*, i'm a
boring PIC developer :)
 
D

Duncan Harvey

Randy Howard said:
Those of us who have been in the game for decades find this sort of
practice demeaning, and a good indication that it is a job we don't
want anyway. Therefore, it saves time on both persons' parts.

[..snip..]

Not all "decades of experience" programmers are equal. [...]

Quite right.

When my employer receives a CV (or 'resume' if you don't mind the lack
of accent) they provide a trivial aptitude test for the candidate to
complete. Recently I've been required to review and comment on
candidates' CVs and tests. One guy claimed over twenty years of C
experience. Judging from his test results he'd never had occasion to
use C strings or dynamically allocated memory. It is certainly possible
to write interesting programs in C using neither of those facilities.
But for twenty years? That's either stupidity or wilful perversion. I
have no particular desire to see either close up.

FWIW I actually do believe that he'd been writing C for twenty years.
That's what scares me.
 
D

Duncan Harvey

Richard Heathfield said:
Consult the standard during office debates? Well, it's rare. The usual
reaction to "let me just show you the bit..." is "Rich! Rich! We
***believe*** you, okay?!?!?!?!?!"

Anyone would think they didn't like reading technical documents. Odd,
that.

The reaction might not be odd at all.

Some people have no fear of, nor dislike of, reading technical documents
but are instead mindful of how such documents are structured and are
well aware of the perils of receiving information out of context. With
special regard to standards documents, the context /is the entire
document/. One cannot just look at a single paragraph in isolation and
declaim "this proves what I say is true". <memo to self: insert comedy
remark about religious texts and their followers> For (a silly)
example, person A might be optimising an inner loop that invoked
ispunct() a lot. They start to do bizarre things to 'optimise' away this
call to "avoid function call overhead". Person B says that it might be
the case that the implementation has already done this for them. Person
A snorts and replies, '7.3.1.11 states "The ispunct() *function* tests
[...]"'[1] and slumps back into his chair satisfied. (I admit, as
hypothetical examples go, that is not entirely convincing but I trust
that you'll see what I'm driving at.)

I can imagine scenarios where "Yes, yes. I believe you." is a
convenient and reasonably polite shorthand for, "this is indeed
interesting, but I consider it a minor point that does not affect the
outcome of the subject we were discussing, nor the project we are
working on, and it would not be a good use of our employer's time
[remember, this is an office debate] to spend a lot of time discussing
this, however engrossing it may be for us as individuals to do so, and
why can't I finish saying this run-on sentence?".

"Yes, yes. I believe you." might mean "Yes, I believe that you believe
what you say is true, but to be sure that it is actually true I'd have
to check for myself, and I don't have time/cannot be bothered/have
better things to do/don't like reading technical documents/etc.".

I can imagine a further scenario, where the person saying "Yes, yes. I
believe you." is merely an idiot.

[1] For some unknown reason Person A always cites section numbers using
octal. And may not be aware of C99.
 
S

Sheldon Simms

Consult the standard during office debates? Well, it's rare. The usual
reaction to "let me just show you the bit..." is "Rich! Rich! We
***believe*** you, okay?!?!?!?!?!"

How nice. My experience of consulting the standard during an office
debate is less gratifying:

... so you see, according to the standard, that code has undefined
behavior. If you change this one line the code will work.

No, it didn't crash with the previous version of the compiler. The
new version must be buggy. I'm not changing my code.
 

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

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,204
Latest member
MalorieSte

Latest Threads

Top