Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

If I am asked in an interview to write down some code, should I completely stick with C++ or is it acceptable to "mix" the two to write easy code as it comes to my mind?

I'll explain it: if I'm asked to create a "stack" data type, a C++ class is perfectly suited, while if I need to parse some strings a strtok/sscanf is far easier than STL. On the other hand, if I need a generic container STL and templates are the way to go.

Everything can be done in C or in C++, but in an interview I can't code everything or remember every standard/STL function. What is an acceptable tradeoff?

share|improve this question
1  
Most C++ programmers would say you shouldn't mix in pure C without a really damned good reason (which you better be able to justify). –  Rig Aug 30 '12 at 19:36
2  
I guess a "I can't remember the STL function to handle the string splitting" isn't great, is it? –  John Smith Aug 30 '12 at 19:42
14  
That you know enough to even ask this question would be a positive sign in my book if I was the interviewer. Just ask them what they want. –  GrandmasterB Aug 30 '12 at 19:42
2  
sprintf and FILE can make my life easier at times in C++ world –  Thomas Eding Aug 30 '12 at 20:10
    
If I was an interviewer I wouldn't be impressed if someone answered this question by saying what function they'd call. –  James Aug 30 '12 at 21:22

6 Answers 6

up vote 12 down vote accepted

To be really pedantic, the C++ offers supports for C, natively, maybe only a subset, but if the topic is the C++ the C programming language is included, and this is not an opinion, i would expect no arguments on that in a professional environment.

share|improve this answer
    
Agreed. What's more, C is kinda part of C++ so why not use it to benefit? Sticking purely to one part of the language and not touching the other is not a good use of the language, in my opinion. –  Pius Aug 31 '12 at 5:17

If I am asked in an interview to write down some code, should I completely stick with C++ or is it acceptable to "mix" the two to write easy code as it comes to my mind?

That entirely depends on what kind of job you are applying for and which language they expect you to use. Generally, if you are writing C++, it is best to stick to C++ as far as possible. I would however expect every C++ programmer to know at least a bit of C (and vice versa).

I'll explain it: if I'm asked to create a "stack" data type, a C++ class is perfectly suited, while if I need to parse some strings a strtok/sscanf is far easier than STL. On the other hand, if I need a generic container STL and templates are the way to go.

If they expect you to write a whole working data type during the interview, you should be wary. It is good if they ask you about technical details, but if they expect you to spend hours of writing programs during the interview, that likely says something bad about the employer.

If you use scanf(), you have probably made a big mistake no matter if C or C++. You better explain to the interviewer that you are just writing something quick & dirty and would never use scanf in production code. There are many pit falls with various C library functions.

The interviewer shouldn't expect you to remember every function in the C and C++ standard librarier, but they should expect you to know which ones that are blatantly dangerous. gets(), getchar(), scanf(), setjmp() etc.

share|improve this answer
    
totally agree, it was just to make an example, I would AT LEAST use scanf_s, and if I can I would TOTALLY avoid it in production code –  John Smith Aug 31 '12 at 13:54

Unless specified, most programming questions will let you use whatever language you want when answering a specific question. You may want to explain which language you are using to answer the specific question to avoid confusion and explain why you are using it.

If you are not sure if this will be OK with the interviewer, ask - it is absolutely acceptable to ask if you can answer different questions using different languages (in most on-paper answers, pseudo-code will be more than acceptable).

share|improve this answer
4  
Yes, that's what I've been thinking: pseudo-code. I don't have a knowledge base in my mind and I tend to memorize techniques and way of using stuff rather than syntax or API declarations (which may change). If I can pseudo-code it and I have a "decent" amount of programming experience with a language, doesn't matter what it is I think I can code it. –  John Smith Aug 30 '12 at 19:30
2  
It would be a positive sign in my book if the candidate was easily able to switch between C | C++ | C++ & STL and could explain why they chose a particular path. –  GlenH7 Aug 30 '12 at 19:32
6  
@JohnPell - In my experience understanding principles and how and when to apply them trumps deep language knowledge every time. –  Oded Aug 30 '12 at 19:33

Unless your interviewer explicit request that you code in C or C++, I would just use what ever is appropriate for the interview.

For me, if my candidate was mixing the two up, I would be more interested in his strategy or reason for mixing the two language. Having someone on my team that can provide a solution and be able to justify the solution is extremely valuable.

share|improve this answer
1  
+1, Justifying is the key. I'd say this is the key to being a good developer in general (besides programming skills, of course). –  K.Steff Aug 30 '12 at 20:08

I think that when you are actually explicitly asked to do a short assignment in C++, it would be of utmost importance to actually stick with C++ and not mix C with it. Modern C is not a subset of modern C++, as such mixing the two would only imply that you aren't very familiar with what clean, modern C++ is like.

share|improve this answer

You should just ask to the interviewer what he prefer, if you know how to use both.

Depending on his answer you can discover:

  1. if he really know the differences between C and C++;
  2. if he knows about C++ standard library;
  3. if it's important to him.

There is no generic answer to this question. It's too dependent on the job and the objectives of the recruiter. If he wants you to show language knowledge then he will be specific. If he want you to show you problem solving skills, and communication skills, then he will not really care.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.