Do I need to learn C before C ++?

  • Thread starter Thread starter Amine Quentin
  • Start date Start date
Click For Summary
Learning C before C++ is not a requirement, but it can be beneficial for understanding foundational programming concepts. C++ builds on C's syntax while introducing object-oriented programming features that enhance code organization and teamwork for larger projects. Beginners can start with C++ directly, focusing on procedural programming before tackling OOP concepts. However, grasping C can provide valuable insights into low-level programming and memory management, which are useful in specific contexts like system programming. Ultimately, the choice of whether to learn C first depends on individual goals and the complexity of projects being undertaken.
  • #31
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++.
I have no idea what function pointers in C++ with soo many function overloadings.
 
Technology news on Phys.org
  • #32
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.
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.

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?
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.
 
  • #33
tAllan said:
In C++ it's generally not necessary or recommended to use function pointers.
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.
 
  • #34
In C++ it would be considered more idiomatic to use a map of functors rather than function pointers. One of the advantages is that this gives you more compatability with the STL. It also gives you more flexability and in some cases increased performance when the functors methods can be inlined. I believe this is one main reason why std::sort so heavily outperforms c's sort function for example.
 
Last edited:
  • #35
D H said:
cdecl is a Microsoft-specific concept. There's nothing in the C or C++ standard that addresses cdecl.
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).
 
  • #36
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++).
 
  • #37
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++).
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.
 
  • #38
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.

Well if you think about it, automatic transmissions are more complicated than standard transmissions also. If C++ makes large projects more manageable, and the goal of "moving the car" is likened to large programming projects, then maybe it still works.

But I see what you mean.
 
  • #39
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++).

Hmm. But C++ takes more effort to write, while automatic transmission requires less effort.

To me, C++ is like circuit boards plugged into bus, while C is point-to-point wiring. A bad C++ program is like both: circuit boards connected by point-to-point wiring. The worst of both worlds.

I like formal declarations of interfaces. I don't see any disadvantage to it for high level code. I never tried to use it for real-time software.
 
  • Like
Likes ElijahRockers
  • #40
D 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.
Yes, yes, sorry. I type before I think. (some staff gave me a warning about that :smile:)
The question is "Is every .obj file can be linked with by every compiler, is function with cdecl declaration can be linked with any other languages"
I forgot that cdecl only works for a function, as @Svein points out before. cdecl doesn't work for the whole .obj files. Even if you have one function declared with cdecl prefix (or sufix) the other function overloading can make confunsion.
 
  • #41
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.
 
  • #42
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.

Two C compilers will generate different output. It's up to the compiler writer.
 
  • Like
Likes ElijahRockers
  • #43
My whole point is that after a C++ class you will be very skilled at C. You SHOULD do that before the class, and if you do, you will have capacity and understanding to better learn C++. You will use almost all of your C skills when you write C++. So not having to learn C in the C++ class leaves to able to absorb more of the C++ subtleties. I feel very strong about this, having done it the wrong way myself. I'm repeating it because the thread is all over the place.
 
  • #44
Hornbein said:
Two C compilers will generate different output. It's up to the compiler writer.
Okay, what I mean is the result will be exactly the same, right.
You write a C code to calculate, say.. the orbit of the moon. And throw the codes to C++ compiler, its executable will still calculate the orbit of the moon.
But if we change the process
Throw C++ source codes to C...
 
  • #45
Stephanus said:
I have no idea how in the .obj file these two functions coexist together.
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).
You cannot get a C compiler to make a working result from code that uses C++ objects, C++ libraries, and such.
 
  • #46
Amine Quentin said:
Hi Guys
Do I need to learn C before C ++?

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.
 
  • #47
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.
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.
 
  • #48
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.
For example, say your first assignment is a simple linked list class. If you know C and can implement a linked list, then it is a simple C++ assignment to encapsulate it in a class and provide methods, constructors, destructors, etc. If you don't know C you will spend a week learning how to write C to implement a linked list before you can even begin to think about making it a class. You don't even know how to do basic .h files.

Taking a C++ class without knowing C at all will be impossible. I tried it with mediocre C skills and suffered immensely.
 
  • #49
You can learn both at the same time, switching between C and C++ is not that big a deal, even while learning. Depending on the project you're working on, C may be all you need. If using something from C++ standard template library, then you'll need C++ for that project. The projects that you work on while learning are more of an issue than whether you use C or C++.

In the case of school, what matters if there is an introduction to programming class that uses C and/or C++.

At some point, you may want to spend a few weeks learning assembler with very simple projects for a basuc understanding of how programs work. I'm not sure if it's worth an entire semester, unless you're interested in that.
 
  • #50
meBigGuy said:
NO NO NO. You ABSOLUTELY need to learn C before you learn C++ PERIOD.
Since C source codes can be compiled by C++ compiler. There's nothing wrong with learning C++.
You can learn C++ without OOP and operator overloading and function overloading and it's like C before you know you compile it by C++ compiler. As @rcgldr says,
rcgldr said:
You can learn both at the same time...
 
  • #51
Stephanus said:
Since C source codes can be compiled by C++ compiler. There's nothing wrong with learning C++.
You can learn C++ without OOP and operator overloading and function overloading and it's like C before you know you compile it by C++ compiler. As @rcgldr says,
No, it's not "like" C when you do that, it IS C.
 
  • #52
phinds said:
No, it's not "like" C when you do that, it IS C.
Impressive isn't it. It's two entirely different languages yet very much alike.
 
  • #53
Stephanus said:
Impressive isn't it. It's two entirely different languages yet very much alike.
I don't understand what you are talking about. What do you mean they are "two entirely different languages" ? C++ is just OOP put on top of C.
 
  • #54
phinds said:
I don't understand what you are talking about. What do you mean they are "two entirely different languages" ? C++ is just OOP put on top of C.
You are right, C++ supersedes C but on one hand a very large number of core libraries developed during the time C came along are heavily dependent on C. Industrial and business needs are, on the other hand, relying on the management of time frame and budget, so all of a sudden people can't take risk to upgrade their systems to latest technologies that use C++ or the likes and at the same time security issues have also been brought up a lot like plagues or pathogens the human have to fight against.
C developers thus have to keep its mainstream C alive while simultaneously developing C++ language. Almost all seniors understanding C also can code in C++ but the reverse case for the young people may not be true.
 
  • #55
I feel very strongly about this. The fact that C++ is C is meaningless (although it is true). When you go into ANY C++ class there will be assumptions about how much C you know. Please read my example above regarding building a linked list class. And it isn't like the C++ class is going to spend time teaching everyone about C basics like pointers, arrays, and the like.

If you talk to the professor and he specifically says that he will teach C++ beginning with the assumption that the students have zero C knowledge, then possibly I'll relent. But, even then, you will learn more if you know C going in.

Again, the fact that C++ is based on C is meaningless in this context. The C++ constructs are built on top of (out of) C, and you should understand basic C going in. You use a lot of basic C in any C++ program.
 
  • #56
meBigGuy said:
I feel very strongly about this. The fact that C++ is C is meaningless (although it is true). When you go into ANY C++ class there will be assumptions about how much C you know. Please read my example above regarding building a linked list class. And it isn't like the C++ class is going to spend time teaching everyone about C basics like pointers, arrays, and the like.

If you talk to the professor and he specifically says that he will teach C++ beginning with the assumption that the students have zero C knowledge, then possibly I'll relent. But, even then, you will learn more if you know C going in.

Again, the fact that C++ is based on C is meaningless in this context. The C++ constructs are built on top of (out of) C, and you should understand basic C going in. You use a lot of basic C in any C++ program.
You are making exactly the point I have been propounding all along. It seems to me that you are agreeing with me while disagreeing with me.

You say "The C++ constructs are built on top of (out of) C, and you should understand basic C going in" which is exactly what I mean when I say that C++ is just C plus the OOP constructs.
 
  • #57
phinds said:
You are making exactly the point I have been propounding all along. It seems to me that you are agreeing with me while disagreeing with me.

You say "The C++ constructs are built on top of (out of) C, and you should understand basic C going in" which is exactly what I mean when I say that C++ is just C plus the OOP constructs.
Not merely C plus OOP, but the operator overloading is a very new concept.
Of course you can't make operator overloading without class definition.
cin and cout are the most common objects.
 
  • #58
Stephanus said:
Not merely C plus OOP, but the operator overloading is a very new concept.
Of course you can't make operator overloading without class definition.
cin and cout are the most common objects.
I consider overloading to be part of the OOP constructs.
 
  • #59
phinds said:
I consider overloading to be part of the OOP constructs.
But in Delphi, there's no operator overloading. OOP doesn't have to have operator overloading. But, you can't do operator overloading without OOP. Furthermore in C++ there is function overloading feature, right. Doesn't have anything to do with OOP.
Although I use Delphi, but I'm very impressed with C++.
I don't have to write this, you must have known it, just to give you the idea.
In C++, we can write.
Vector + Vector = Vector
Vector - Vector = Vector
Coordinate + Vector = Coordinate
Matrix * Number = Matrix

and we can overrule these
Vector + Coordinate = n/a

I bet, Dennis Ritchie (?), when he developed C++ he must have thought that any programmer who wants to code in C++ must have sufficient knowledge in mathematic, logic as well as programming language.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
86
Views
2K
Replies
69
Views
10K
Replies
6
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 133 ·
5
Replies
133
Views
10K