Amine Quentin
- 12
- 0
Hi Guys
Do I need to learn C before C ++?
Do I need to learn C before C ++?
I expect a student to start learning with assembly.Amine Quentin said:Hi Guys
Do I need to learn C before C ++?
I don't think this is a reasonable expectation. Very few schools in the US or Europe start off budding computer science types with assembly language of any kind, to the best of my knowledge.Silicon Waffle said:I expect a student to start learning with assembly.
Mark44 said:One way that C++ frequently is taught is by presenting it first as a procedural language with no mention of the OOP features such as classes and inheritance, and using the C runtime library functions such as printf(), scanf() and the like.
Mark44 said:I don't think this is a reasonable expectation. Very few schools in the US or Europe start off budding computer science types with assembly language of any kind, to the best of my knowledge.
A bit of assembly language at the start would be a good thing, though...
Mark44 said:I don't think this is a reasonable expectation. Very few schools in the US or Europe start off budding computer science types with assembly language of any kind, to the best of my knowledge.
A bit of assembly language at the start would be a good thing, though...
My first practical computer course (in 1964) was based on assembly programming for an obscure computer called the "Wegematic". The theory was a mix of old-fashioned electronics (relays, tubes, Josephson junctions and germanium transistors) and the ALGOL 60 programming language.Hornbein said:At Harvard in 70's assembly language was the second and main computer course. No wonder Bill Gates dropped out.
Agreeing with what you said, but here's an anecdote. About 10 years ago I read an internal Microsoft white paper (I used to work for Microsoft) that advised using C instead of C++ in parts of the Windows kernel, for the reason that it was difficult to predict the compiled object code size of C++, due to the compiler generating constructors, destructors, and copy constructors, etc. Note that I'm not talking about all of Windows, just some of the code that runs in the kernel. I don't know if that white paper is still in effect.QuantumQuest said:Strictly speaking, it is not required to learn C before learning C++, but there is no harm to do so. There are cases - although few nowadays, that you'll need pure C as a programmer.
QuantumQuest said:But I think, that learning C, will definitely teach you good procedural programming and some unique features, that you'll not find in any other language and depending on your goals e.g. for system programming, drivers etc., this may prove to be of great help. If you want to go for modern application programming, then probably it's better to learn everything in the context of C++.
Polymorphic behaviors are not feasible in driver development but other OPP features are.Svein said:If you are going to write drivers or "close to the hardware" software, you cannot rely on having access to any run-time libraries. This means that you can use C, but not C++.
No. Just go straight to C++. And if you're familiar with OOP before, you might want to take a glance at C++ OOP. But like @phinds says. Study the C first then the ++ later.Amine Quentin said:Hi Guys
Do I need to learn C before C ++?
Yes, Mark. It is very easy to predict the assembly result from C than C++.Mark44 said:Agreeing with what you said, but here's an anecdote. About 10 years ago I read an internal Microsoft white paper (I used to work for Microsoft) that advised using C instead of C++ in parts of the Windows kernel, for the reason that it was difficult to predict the compiled object code size of C++, due to the compiler generating constructors, destructors, and copy constructors, etc. Note that I'm not talking about all of Windows, just some of the code that runs in the kernel. I don't know if that white paper is still in effect.
int Func1(int a)
{
return(a);
}
int Func1(inta, int b)
{
return(a+b);
}
I think by the time the compiler has finished with it 'Func1' is no longer an entity.Stephanus said:I have no idea how in the .obj file these two functions coexist together.
Compiler vendors are free to do whatever "name mangling" is necessary to disambiguate overloaded functions like these two. It's been a long time ago that I read about this, but I seem to recall that one vendor (Borland? Microsoft?) generated a symbol for each of these functions based on the function's name as well as the number and types of parameters.Stephanus said:Yes, Mark. It is very easy to predict the assembly result from C than C++.
In C++, these line numbers are valid:
I have no idea how in the .obj file these two functions coexist together.Code:int Func1(int a) { return(a); } int Func1(inta, int b) { return(a+b); }
Okay, this is off topic. But I'm just curious.Mark44 said:Compiler vendors are free to do whatever "name mangling" is necessary to disambiguate overloaded functions like these two. It's been a long time ago that I read about this, but I seem to recall that one vendor (Borland? Microsoft?) generated a symbol for each of these functions based on the function's name as well as the number and types of parameters.
That really needs a long answer. The short answer is - it depends. And the reason is the way C handles function calls.Stephanus said:So the .obj files for C++ can't be linked to every programming language freely? Only cdecl can be linked?
meBigGuy said:You WILL learn C before you learn C++. I took my first computer science course, a graduate level C++ class. I barely knew C. The process of learning all the subtleties of C syntax while also trying to learn the complex concepts of C++ made the class nervous breakdown material.
You need to go in knowing declarations, pointers and arrays, structures, enumeration, arrays of functions, casting types, etc etc etc. If not, you will be learning them as you go and take it from me, it's a tough one. Don't forget that C++ is built on top of C. The class (no pun) will assume you know C syntax.
Like this?Svein said:That really needs a long answer. The short answer is - it depends. And the reason is the way C handles function calls.
The cdecl keyword specifies that the function is compiled according to the C convention.
- In C, the last parameter in a function call is pushed first onto the stack. And the caller is responsible for tidying up the stack after the function call.
- In Pascal, the first parameter in a function call is pushed first onto the stack. And the callee is responsible for tidying up the stack at the end of the function call.
For the long answer, see https://en.wikipedia.org/wiki/X86_calling_conventions.
TestPascal proc
ret 4
TestPascal end procTestC proc
ret
TestC end proc
push AX
push BX
call TestPascal
; no add SP
push BX
push AX
call CestC
add SP,4
int Func1(int a)
{
return(a);
}
int Func1(int a, int b)
{
return(a+b);
}
I have no idea what function pointers in C++ with soo many function overloadings.tAllan said:In C++ it's generally not necessary or recommended to use function pointers. Such a thing would be common in C for things like event driven ui engines, e.g. lists of callback functions. But in C++ you would probably want to use interfaces or functors.
But now that I think about it, it's common to use C libraries in C++ programs, so I suppose it cam be important to be proficient in C for C++ programmers for that reason as well.
There is a lot of C based techniques or syntax that you should know and understand, but should completely avoid using in C++.
Linkers are beasts born in the late 1950s. While they've changed a bit since then (e.g., dynamic libraries, weak symbols), the basic concept remains the same. There's no place in that 50+ year old concept for the same function in multiple namespaces, multiple classes, or for different overloads of the same function, which means that every C++ implementation has to use some kind of name mangling. How that works is highly implementation-specific.Mark44 said:Compiler vendors are free to do whatever "name mangling" is necessary to disambiguate overloaded functions like these two. It's been a long time ago that I read about this, but I seem to recall that one vendor (Borland? Microsoft?) generated a symbol for each of these functions based on the function's name as well as the number and types of parameters.
cdecl is a Microsoft-specific concept. There's nothing in the C or C++ standard that addresses cdecl. Except for code wrapped with lots if #ifdef this, #ifdef that, etc., you won't find any code targeted for a POSIX implementation that uses cdecl. What you occasionally will find is code declared as having extern "C" linkage. This latter concept is something that is in the C++standard, and it essentially means "don't use name mangling."Stephanus said:Okay, this is off topic. But I'm just curious.
So the .obj files for C++ can't be linked to every programming language freely? Only cdecl can be linked?
I use function pointers in C++ quite often. Something that maps from a command to a function to be called is the modern way to avoid a switch statement with hundreds or even thousands of cases (or an if/else if/.../else with hundreds or thousands of alternatives). The C++ library provides the basic capability in a single line: std::map<command_type, function_pointer_type>. (With C++11, it's often better to use a std::unordered_map in lieu of a std::map for this purpose.) The switch or if statement equivalent is massively ugly, can be quite slow if implemented as an if statement, and is hard to maintain. The function pointer version is magical in comparison, particularly when combined with the fact that in C++, a lot of stuff can be made to happen before main() is entered.tAllan said:In C++ it's generally not necessary or recommended to use function pointers.
No. I guess I was not completely clear. Microsoft designed and implemented the first Windows code in Pascal. So when people started to implement functions in C, they could not be called from Pascal. Therefore Microsoft introduced cdecl as a keyword in Pascal (not C). It told the compiler to create a "C"-type call from Pascal (and to clean up the stack afterwards).D H said:cdecl is a Microsoft-specific concept. There's nothing in the C or C++ standard that addresses cdecl.
This makes sense in a way, but I think it's a little misleading. C++ is a much more complex language than C. Maybe you could say C++ coding is like a mix of building or modifying automatic transmissions and using them.ElijahRockers said:One of my TAs, when I asked him this very question, (C vs C++) likened the difference to that of driving a standard transmission automobile (C) to an automatic transmission (C++).
tAllan said:it's a little misleading. C++ is a much more complex language than C. Maybe you could say C++ coding is like a mix of building automatic transmissions and using them.
ElijahRockers said:One of my TAs, when I asked him this very question, (C vs C++) likened the difference to that of driving a standard transmission automobile (C) to an automatic transmission (C++).
Yes, yes, sorry. I type before I think. (some staff gave me a warning about thatD H said:Linkers are beasts born in the late 1950s. While they've changed a bit since then (e.g., dynamic libraries, weak symbols), the basic concept remains the same. There's no place in that 50+ year old concept for the same function in multiple namespaces, multiple classes, or for different overloads of the same function, which means that every C++ implementation has to use some kind of name mangling. How that works is highly implementation-specific.cdecl is a Microsoft-specific concept. There's nothing in the C or C++ standard that addresses cdecl. Except for code wrapped with lots if #ifdef this, #ifdef that, etc., you won't find any code targeted for a POSIX implementation that uses cdecl. What you occasionally will find is code declared as having extern "C" linkage. This latter concept is something that is in the C++standard, and it essentially means "don't use name mangling."
The above does not mean that C++ code cannot be linked to other languages. The Boost.Python library provides interoperability between C++ and python. SWIG goes several steps further, providing interoperability between C/C++ code and Javascript, Perl, PHP, Python, Tcl Ruby, C#, Common Lisp, D, Go, Java, Lua, Modula-3, OCAML, Octave, Scilab and R.
Stephanus said:I'm sorry, this is not my thread. But I am just curious.
If we throw all c source files and all it's .obj library (either from another c or assembly) and its c headers to a C++ compiler,
1. Would the compiler compile them perfectly?
2. Will C++ compiler generates executable 100% match as C compilier, perhaps there's a difference in .exe header or relocation table, but is it 99.99% matches?
Thanks.
Okay, what I mean is the result will be exactly the same, right.Hornbein said:Two C compilers will generate different output. It's up to the compiler writer.
Quite a lot of C code will successfully compile with a C++ compiler, (although the compiler might give warnings about non standard coding it still will produce a working result).Stephanus said:I have no idea how in the .obj file these two functions coexist together.
Amine Quentin said:Hi Guys
Do I need to learn C before C ++?
Yes, I agree. If one knows C very well; procedure calling; looping; structure; pointer; variable type; then in C++ one only has to learn the OOP concept and a little about operator overloading.jack476 said:They're basically the same language, C++ is only an extension of C. If you know one well, then the other will be easy, but there's no real reason to learn either one first.
In retrospect, I'm glad I learned C before C++ (due to requirements for my major, the C class was freshman year, C++ came much later for me) because there's just much less material that needs to be covered and it was easier to "graduate" to C++ once I had experience with the weaknesses of C that C++ was meant to improve on.
On the other hand, C++ is a much more common language than C, and it's usually the standard in programming businesses and one of the languages that a programmer must know at bare minimum, so there's also a case to be made for doing C++ first and then going to C if you're curious about where C++ comes from.
NO NO NO. You ABSOLUTELY need to learn C before you learn C++ PERIOD.jack476 said:They're basically the same language, C++ is only an extension of C. If you know one well, then the other will be easy, but there's no real reason to learn either one first.
In retrospect, I'm glad I learned C before C++ (due to requirements for my major, the C class was freshman year, C++ came much later for me) because there's just much less material that needs to be covered and it was easier to "graduate" to C++ once I had experience with the weaknesses of C that C++ was meant to improve on.
On the other hand, C++ is a much more common language than C, and it's usually the standard in programming businesses and one of the languages that a programmer must know at bare minimum, so there's also a case to be made for doing C++ first and then going to C if you're curious about where C++ comes from.
Since C source codes can be compiled by C++ compiler. There's nothing wrong with learning C++.meBigGuy said:NO NO NO. You ABSOLUTELY need to learn C before you learn C++ PERIOD.
rcgldr said:You can learn both at the same time...