Can I Study C++ to Learn C?
I have to know C for a non-programming job interview--not expert level, but enough to look at code and know what's going on. I've studied C++ but it's been a while and I'm rusty, so I need to study. The problem is, all the books I really like are for C++; there are very few for C alone.
Would I be okay studying C++ books and just avoiding the OOP-related parts (classes, objects, inheritance, polymorphism, encapsulation, <iostream> vs. <stdio.h>)? Or are there too many other major or minor differences that will screw me up on my C test? Any other significant differences to avoid?
Re: Can I Study C++ to Learn C?
C is a subset of C++. Meaning C++ can compile and run most of C but C cannot compile and run C++ so if you are required to learn C I would not suggest it since very little of what you will learn will be useable. C is function driven whereas C++ is object driven.
Re: Can I Study C++ to Learn C?
Although most of C stuff can be used in C++, C is a different programming language.
It has different targets, C programmers are facing different problems, uses different approaches and a different programming "philosophy".
So, at a C interview you can expect more C-specific than C/C++ common problems.
Concluding: if your immediate target is to get a C programmer job, learn C language from dedicated C books.
Re: Can I Study C++ to Learn C?
On the other hand, anybody who is competent with C++ can look at C code and easily understand it.
Re: Can I Study C++ to Learn C?
Quote:
Originally Posted by
GCDEF
On the other hand, anybody who is competent with C++ can look at C code and easily understand it.
Yes, for the most part what GCDEF states is true. If the C code is within a function, then it more than likely can be understood by a C++ programmer.
However there are simple things that will throw a C++ programmer off, while a C programmer would more than likely know the answer. It all stems from C and C++ having different rules.
For example:
Code:
#include <stdio.h>
int main()
{
printf("%lf\n", sqrt(2.0));
}
If on a C test, you're asked "what does this output", a C++ programmer who wasn't aware of the rules of 'C' would say "1.414213" or something similar to that, and they would be wrong. A C programmer would say "cannot be determined because the behaviour is undefined", and the C programmer would be right. The C++ programmer would be even more confused that the answer is the latter after being told that the code above compiles and links successfully as a 'C' program.
Since header file inclusion/non-inclusion is ingrained in a C programmer's head as part of their experience, they know that sqrt() returns an int if the header (or correct prototype) is not used, when in fact sqrt() is a function that returns a double. Therefore the code is valid 'C', but it has undefined behaviour. So we don't know if the program crashes, prints "1.0000" or prints a totally bogus number not even remotely close to "1.4141213".
These types of questions can be used to flush out persons who falsely claim they are C programmers who have not really used the language (they are C++ programmers). Similarly, there are questions on C++ interviews that do the opposite, i.e. ask simple C++ questions to persons claiming to be C++ programmers who are really C programmers.
So the OP needs to be very careful. As Ovidiu mentioned, read 'C' books if the interview is about 'C', and not C++ books.
Regards,
Paul McKenzie
Re: Can I Study C++ to Learn C?