Live data from Hacker News

Interesting C Interview Questions and Answers

thegeekstuff.com

11–20 of 41 posts

Re: Interesting C Interview Questions and Answers

#11
post #3

I've studied most of these as part of my introductory C course in university. Are they really that interesting?

Considering that tons and tons of people apply for programming jobs who don't know how to program at all, they're at least borderline useful. Additionally there are not many classes with C at all these days.

Additionally there are not many classes with C at all these days.

Depends on what you are studying. While I never had any specific "learn C" courses it is by far the most used language in other courses (OS programming, parallel programming, compiler construction, network programming, micro-controllers, graphics and game programming, security courses etc.). No other language comes close (a lot of the time a C++ compiler is used but the C subset is what ultimately has been used).

Re: Interesting C Interview Questions and Answers

#13
post #3

I've studied most of these as part of my introductory C course in university. Are they really that interesting?

None of these are especially interesting C questions... Most rely on assumptions about the x86 architecture and GCC (stack grows downwards, ordering of stack variables in memory (not registers), ...).

#3 notes a warning for main not returning int, but doesn't point out that without stdlib.h, malloc() and free() will be declared implicitly with incorrect types. (Edit: this produces a warning on 4.7 GCC and Clang 3.0.)

Almost all of these questions have something wrong with them other than what the author is pointing out. I question the author's familiarity with C.

Re: Interesting C Interview Questions and Answers

#14
Well, Though the above code is not freeing up the memory allocated to ‘ptr’ but still this would not cause a memory leak as after the processing is done the program exits.

Well this is always true, so I guess that means there's no such thing as memory leaks in C. Checkmate garbage collectors.

Re: Interesting C Interview Questions and Answers

#15

Question 10 is a bad question. The host environment is under no obligation to provide you with pointers to writable memory in argv. Furthermore, this is veering off from C knowledge into C trivia. Question 12 is even worse, as the processing order is implementation defined. On my compiler, it prints out 60..40..60. C questions should be designed to show whether or not the candidate can write robust, professional qual…

2, 3, 4, 7 are all bad questions too. It's pretty bad as collections of C questions go.

Re: Interesting C Interview Questions and Answers

#16
post #3

I've studied most of these as part of my introductory C course in university. Are they really that interesting?

None of these are especially interesting C questions... Most rely on assumptions about the x86 architecture and GCC (stack grows downwards, ordering of stack variables in memory (not registers), ...). #3 notes a warning for main not returning int, but doesn't point out that without stdlib.h, malloc() and free() will be declared implicitly with incorrect types. (Edit: this produces a warning on 4.7 GCC and Clang 3.0.)…

Also the casts to (char *) from malloc suggest that the author is a C++ programmer.

Re: Interesting C Interview Questions and Answers

#17
I wonder why there are so many articles about C getting passed around where the author just doesn't know the language that well, or at the very least ventures well outside the boundaries of his knowledge. Where are the good articles on C, and why don't they ever show up on HN?

Re: Interesting C Interview Questions and Answers

#18
post #17

I wonder why there are so many articles about C getting passed around where the author just doesn't know the language that well, or at the very least ventures well outside the boundaries of his knowledge. Where are the good articles on C, and why don't they ever show up on HN?

The question is why it's on 12th place on the index. Don't people read articles before they press the little arrow?

Re: Interesting C Interview Questions and Answers

#19

Question 10 is a bad question. The host environment is under no obligation to provide you with pointers to writable memory in argv. Furthermore, this is veering off from C knowledge into C trivia. Question 12 is even worse, as the processing order is implementation defined. On my compiler, it prints out 60..40..60. C questions should be designed to show whether or not the candidate can write robust, professional qual…

Further to point one, it's under no obligation to provide you with writable memory of the size you'd like. strncpy(argv[0], "NewName", 7); WTF does 7 come from, the source or the destination?

Question 10 is just crazy broken. I'm not a standards expert, but I don't think argv[0] is guaranteed to be big enough to hold the 7 characters (plus \0) of NewName (i.e., even if it is writable, it may not be big enough).

As the comments said, argv[0] does not have to point to writable memory, because argc can be 0. In this case argv[0] is supposed to be NULL, which would segfault the program.

If you had to solve this question (it would be better not to ask it, because you don't learn much), I think the solution:

   argv[0] = "NewName";
would be better. It's at least shorter.
Post reply on HN