-10

I got this question from an interview:

main(x, y, z, p) {
  while (y = ++x)
    for (z = 0; z < y ? z = z * 8 + y % 8, y /= 8, p = z == y | z / 8 == y,
        1             : (x - ++p || printf("%o\n", x)) && x % p;)
      ;
}

1: Describe what x, y, z, p are used for.
2: Rewrite the code to be easy to understand.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • 2
    @orca123456 Is it an interview in a mental hospital? – Vlad from Moscow Oct 14 '22 at 21:02
  • Does this answer your question? [C functions declaration in K&R](https://stackoverflow.com/questions/18421735/c-functions-declaration-in-kr) – user17732522 Oct 14 '22 at 21:07
  • Possibly it is intended to be a trick question. If not, then surely `main` is being used (incorrectly) as an arbitrary name rather than as the name of the program entry point. Or in an implementation-defined manner. In either of those cases, it seems to be the intent of the question that you should figure out the significance of the fourth parameter. – John Bollinger Oct 14 '22 at 21:07
  • 7
    If that is from their code base, be thankful you didn't get the job... `:-)` – Fe2O3 Oct 14 '22 at 21:13
  • The correct answer: "Do you **REALLY** think rewriting that is important?" And if they answer "Yes", you reply with, "Is this some sort of intelligence test to see if I'm stupid enough to work here?" – Andrew Henle Oct 14 '22 at 21:17
  • Interviewing is hard, and like any hard problem (or any problem, for that matter), some people are better at it than others. This interview question, however, is one of the worst I've ever seen. As an interviewer I would never ask a question like this, and as an interviewee, I would be extremely reluctant to attempt to answer it, as it's either (a) a mistake, (b) a really perverted trick question, or (c) a clear and unignorable signal that, whoever or whatever they're looking for, it isn't me. – Steve Summit Oct 14 '22 at 21:20
  • 1
    If this is really `main`, something doesn't add up, because `y` should be a `char **`, so `y = ++x` is meaningless. – Steve Summit Oct 14 '22 at 21:22
  • "Rewrite the code to be easy to understand": `echo "you want а job in this nuthouse?"` – ddbug Oct 14 '22 at 21:31
  • Sort of, @SteveSummit. In the provided K&R-style function definition, `y` has type `int`, so `y == ++x` is ok. But for the same reason, the function does not conform even in the first two parameters to any form of `main()` I've ever run into. – John Bollinger Oct 14 '22 at 21:49

2 Answers2

2

The fourth parameter of main() should not be used by anyone, except computer archeologists or freaks.

This Wikpedia article says that the 4th main() parameter is something custom to Darwin. There it is array of string pointers, like argv, not an integer. In other ancient books it may be an integer.

Whoever dispensed you this exercise is a freak (as he/she isn't likely a computer archeologist)

Michael M.
  • 10,486
  • 9
  • 18
  • 34
ddbug
  • 1,392
  • 1
  • 11
  • 25
1

The C language specification does not define a four-parameter version of main(), so we must assume that the function provided is exercising an implementation-defined alternative, or that it is to be presented to a freestanding implementation in which the name main has no special significance. Either way, we cannot depend on the language specification to tell us the significance of any of the parameters.

As a result of the omission of a return type and of parameter types (either in the parameter list or in a separate parameter type list), C language specifications more recent than C90 do not define the meaning of the definition. In C90, it employs a K&R style definition with return type and all parameter types defaulting to int.

Even in C90, however, the function has undefined behavior for every possible combination of arguments. For many cases this arises from (eventual) integer overflow, and in one class of cases it arises from calling printf() with a signed integer argument (mis)matched to an %o directive, which requires an unsigned integer.

I'd say, then, that the best answer to the interview question would probably be

  1. The usage of all parameters is undefined because the behavior of the function is always undefined.

  2. Rewrite:

    int main(int x, int, y, int z, int p) {
        assert(("This function should not be called", 0));
    }
    

It's hard to say how interviewers would receive that answer. However, inasmuch as the question seems to be of the "see what they do with it" kind as opposed to the kind with right and wrong answers, you might get away with that if you could explain why the behavior is always undefined (left as an exercise).

John Bollinger
  • 160,171
  • 8
  • 81
  • 157