1

Possible Duplicate:
What is the proper declaration of main?

I just attended my first c++ exam. First question was

int main(◊, char ** argv)
Which one of the following suggestions will not work (as first formal parameter substitute for ◊):

    a) char argc
    b) int argc
    c) double argc
    d) bool argc


The answer counts 2% in a 4 hour purely handwritten individual exam.
All tools are allowed, accept any programmable device or any means of communication

Who can get this right :)?

Community
  • 1
  • 1
user1361521
  • 87
  • 1
  • 1
  • 4
  • Have you paraphrased, or is that the actual question? If it is, then it is quite badly formuulated. – juanchopanza May 16 '12 at 08:27
  • 1
    Question needs either to specify an implementation or to ask which one *will* work. One of them is guaranteed by C++, so it's entirely up to the implementation whether it permits the others, and if so what happens. Plenty of the people who write these exams know C++ less well than plenty of people on SO, so I think that how to pass exams in "C++-stupid" isn't really on topic for the site. – Steve Jessop May 16 '12 at 08:40
  • @juanchopanza && Steve Jessop. That is the full question. It was originally asked in the Danish language, but the translation to English is pretty accurate. – user1361521 May 16 '12 at 08:42
  • 1
    @user1361521: I won't downvote you, but please send a "-1" to the examiners. – Steve Jessop May 16 '12 at 08:44
  • I would give 0 marks to whoever set the question, unless the Danish equivalent of "will not work" isn't totally ambiguous. – juanchopanza May 16 '12 at 08:46
  • This question seriously needs to define what *will not work* means. There's *always* a way to get it to "work", involving dirty hacks, but it *works*. Meh. – user703016 May 16 '12 at 08:46
  • 1
    Or if the question allows you to check multiple answers, and the Danish phrase translated here as "which one of these" could also be understood as "which of these". – Steve Jessop May 16 '12 at 08:47
  • :)... Only 1 specific answer is supposed to be the correct one. – user1361521 May 16 '12 at 08:58
  • The term "will not work" is just as vague in the danish form. – user1361521 May 16 '12 at 09:00

4 Answers4

7

Define what "works" means. Any of those might work, but a valid, standard-compliant, well-formed C++ program has either of the following signatures:

int main()
int main(int argc, char** argv)
int main(int argc, char* argv[])

So a), c) and d) are wrong.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • And I think using the clue-stick(2x4) on anyone using a) c) or d) should be allowed. Technically on a platform where sizeof(char) == sizeof(int) a) could work just fine, but I would still prefer not seeing it at all, ever. – r_ahlskog May 16 '12 at 08:27
  • 3
    @r_ahlskog so you're saying UB is fine, as long as it works as expected? – Luchian Grigore May 16 '12 at 08:28
  • I said "could work" and "prefer not seeing it at all, ever", maybe "would not explode every odd day of the week" and "slap them around a bit with the stick, just to be sure" would have been better. These questions where schools push UB really turns my stomach. – r_ahlskog May 16 '12 at 08:33
  • Is it UB? I thought it was implementation-defined which signatures are supported for `main` (other than that the return must be `int`). I sort of took it as read that the meaning of any additional signatures is also implementation-defined rather than UB. Of course, some implementations are slack to the point of non-conformance, because neither compiler nor linker actually checks that `main` has a supported signature. – Steve Jessop May 16 '12 at 08:36
  • @SteveJessop the UB comment was just a analogy, I wasn't implying having a different signature is UB. Just saying that even if something appears to work, it doesn't make it OK. – Luchian Grigore May 16 '12 at 08:39
  • 1
    Possibly there's a technical difference between "working fine" and "being fine". Things that work fine but aren't fine are the sneakiest bugs! – Steve Jessop May 16 '12 at 08:42
  • @Steve Jessop. I agree. The question however, demands only 1 specific answer to be correct. – user1361521 May 16 '12 at 08:47
  • @SteveJessop oh yeah - unlucky it works fine :) – Luchian Grigore May 16 '12 at 08:48
  • 1
    The signatures of `main` are implementation defined; except for the two required ones, the only requirement is that they all return `int`. _If_ the program uses a signature of `main` which isn't supported, I think that a diagnostic is required; from a QoI point of view, it is certainly required. So b) is guaranteed, and a), c) and d) are implementation defined, but will either fail to compile or will work. – James Kanze May 16 '12 at 08:58
3

Define "will not work" ?

int main(char argc, char ** argv)
{
    printf("%d\n", argc);
    return 0;
}

./a.out 1 2 3
Output: 4

int main(int argc, char ** argv)
{
    printf("%d\n", argc);
    return 0;
}

./a.out 1 2 3
Output: 4

int main(double argc, char ** argv)
{
    printf("%d\n", *(int*)&argc);
    return 0;
}

./a.out 1 2 3
Output: 4

int main(bool argc, char ** argv)
{
    printf("%d\n", argc);
    return 0;
}

./a.out 1 2 3
Output: 4
user703016
  • 37,307
  • 8
  • 87
  • 112
  • 1
    +1: for breaking the rules of the exam and using a programmable device :) – Nick May 16 '12 at 08:26
  • @Cicada - Regarding "Will not work": Your guess is as good as mine. Which compiler are you using? In Xcode, I have only managed to compile option b). – user1361521 May 16 '12 at 09:12
  • Using g++ 4.5.2, everything compiled (with a warning on a, c, d though) and everything ran correctly. – user703016 May 16 '12 at 09:14
2

Given that the question is asking which one will not work. It would have to be double all the others are integers.

I believe this would be the correct answer because you can't index an array with anything other than an integral value. But that assumes that you actually want to index the argv array.

But what a bad question to ask on a C++ exam.

Nick
  • 25,026
  • 7
  • 51
  • 83
  • It is indeed "which one will not work" - Only choosing exactly 1 option will qualify as an answer. Can you explain why you would argue the double? To me they all have the same functionality, as long as main() is called with no more arguments than argc has room for. – user1361521 May 16 '12 at 09:22
0

"int argc" is the correct usage. argc represents the number of parameters passed to the main. So its only int.