C++ to C
Moderator: Coders of Rage
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
C++ to C
So, I learned python scripting a little bit, and then got into C++ programming.
I've been using C++ for, I think, a little over a year now, starting to use graphics libraries with it.
I was wondering, what is the difference between C and C++?
I thought it was just that C didn't have classes and C++ did.
If that is the only difference, then why all the debate over using C vs C++?
I mean, why use C?
Are there any other substantial differences aside from classes?
Is C more efficient?
And if so, then why?
Do you think I would benefit greatly in learning to use C?
Would it be easy after learning C++?
Thank you,
-Benjamin
I've been using C++ for, I think, a little over a year now, starting to use graphics libraries with it.
I was wondering, what is the difference between C and C++?
I thought it was just that C didn't have classes and C++ did.
If that is the only difference, then why all the debate over using C vs C++?
I mean, why use C?
Are there any other substantial differences aside from classes?
Is C more efficient?
And if so, then why?
Do you think I would benefit greatly in learning to use C?
Would it be easy after learning C++?
Thank you,
-Benjamin
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: C++ to C
Google is your friend. At least until Falco shows up to edumacate your ass.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- bbguimaraes
- Chaos Rift Junior
- Posts: 294
- Joined: Wed Apr 11, 2012 4:34 pm
- Programming Language of Choice: c++
- Location: Brazil
- Contact:
Re: C++ to C
Well, C++ is a superset of C, which means anything you can do in C, you can do in C++. What C++ adds is two main programming paradigms: object-orientated programming and generic programming. Also, a standard library and a standard template library (a.k.a. STL).
edit
Just thought it'd be good to say that this is by no means a complete explanation, just a ((very) quick) introduction.
edit
Just thought it'd be good to say that this is by no means a complete explanation, just a ((very) quick) introduction.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: C++ to C
Not 100% accurate. There are quite a few little nuances between the two. Novice C and C++ developers may not be aware of them, but more advanced users of both languages definitely need to be familiar... It can be different enough that the C++ standard provides anbbguimaraes wrote:which means anything you can do in C, you can do in C++.
Code: Select all
extern "C"
C++ adds its OWN standard library, but C already has a standard library.bbguimaraes wrote:What C++ adds is two main programming paradigms: object-orientated programming and generic programming. Also, a standard library and a standard template library (a.k.a. STL).
While there is definitely more to it than that, that's still a GIGANTIC difference. No polymorphism, encapsulation, or inheritance. That's an entire programming paradigm that the C language does not support natively (although you can still mimic each of these within the C language).Benajmin100 wrote:I thought it was just that C didn't have classes and C++ did.
I will answer that by answering your next questions:Benjamin100 wrote:I mean, why use C?
Yes. C code tends to be faster than the equivalent C++ code for a variety of reasons. Most of C++'s high-level object-oriented mechanisms have some sort of overhead associated with them. The most classic example is the vtable introduced by virtual functions. Each call to a virtual function requires a lookup table to invoke the correct "version" of the method depending on the type the object was instantiated as. When you get into some of the higher-level features of C++, especially the optional features, like exceptions and RTTI, you really start to introduce quite a bit more overhead.Benjamin100 wrote:Is C more efficient?
And if so, then why?
There is also a shitload more overhead associated with an object-oriented mindset rather than with C++ as a language. Classes tend to have constructors, destructors, copy constructors, etc, whereas a structural paradigm in C uses straight initializers. C++ favors encapsulating the shit out of datatypes with additional "accessor methods" that provide function overhead that normally isn't present in C. There are plenty more examples of this, but I am currently high on hydrocodone, so I will stop here.
C is best suited for applications that are extremely low level and/or require very efficient resource management. It is most often used with microprocessors, drivers, and operating systems. All of these tasks would be a pain in the ass to write in C++, because of their low-level nature.
Many libraries and APIs tend to favor C over C++ for obvious compatibility reasons. A C library can be utilized by C or C++, but the reverse is not (easily) true. OpenGL, OpenAL, OpenCL, and SDL are great examples of this.
It depends on what you want to do. In the modern world, I think we are beginning to see the end of C's lifetime for your average desktop application. Languages like Java, C#, and C++ (with frameworks like QT) are going to be much better suited.Benjamin100 wrote:Do you think I would benefit greatly in learning to use C?
Would it be easy after learning C++?
With that being said, the C language is still going to be king for a loooong time in the arenas of embedded systems, drivers, operating systems, and anything that is either extremely close to hardware or needs to be extremely fast.
Would YOU personally benefit from learning C? Probably not. But there are many people out there who honestly would. I work with the C language every day at work. I interface low-level C drivers for various chips with our C++-based embedded OS. It takes extensive knowledge of both languages and both programming paradigms. My hobbies are all either low-level game development related or are microprocessing related, where I am either writing C or am very closely interacting with it from C++.
Another interesting application of C that is emerging is using it as a wrapper language. Once I learned Objective-C, I thanked god almighty that I was well-versed in ANSI C. I was easily able to write minimal Obj-C and write the majority of my application(s) in C++. Then C++ and ObjC can communicate through a thin C wrapper. The same is becoming true for Java. Look at the NDK for Android. If you know what you're dong with C, you can get Java and C++ talking to each other...
- bbguimaraes
- Chaos Rift Junior
- Posts: 294
- Joined: Wed Apr 11, 2012 4:34 pm
- Programming Language of Choice: c++
- Location: Brazil
- Contact:
Re: C++ to C
Exactaly what I'd say, if I was giving a long answer.
That doesn't mean it can't do anything C can, but I get what you're saying. I was careful not to mean, and it's important to say, "you can use C code in C++ without any changes".Falco Girgis wrote:Not 100% accurate. There are quite a few little nuances between the two. Novice C and C++ developers may not be aware of them, but more advanced users of both languages definitely need to be familiar... It can be different enough that the C++ standard provides anbbguimaraes wrote:which means anything you can do in C, you can do in C++.pragma.Code: Select all
extern "C"
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: C++ to C
I would also like to point out that, even if you deal primarily high-level languages, a minimal (at least) understanding of C can be very beneficial if for no other reason than that it is everywhere. Even if you disregard embedded systems and interfacing with other languages, almost all of the basic utilities (and libraries) of your computer are written in C. When writing in a high-level language, it's likely that the compiler/interpreter is itself written in C. Those fancy libraries your language has convenient wrappers for? Again, C. So although you may rarely need to deal with it directly, it's important to have some sort of understanding so that you know how to deal with things when the abstractions leak.Falco Girgis wrote:Would YOU personally benefit from learning C? Probably not. But there are many people out there who honestly would. I work with the C language every day at work. I interface low-level C drivers for various chips with our C++-based embedded OS. It takes extensive knowledge of both languages and both programming paradigms. My hobbies are all either low-level game development related or are microprocessing related, where I am either writing C or am very closely interacting with it from C++.
Another interesting application of C that is emerging is using it as a wrapper language. Once I learned Objective-C, I thanked god almighty that I was well-versed in ANSI C. I was easily able to write minimal Obj-C and write the majority of my application(s) in C++. Then C++ and ObjC can communicate through a thin C wrapper. The same is becoming true for Java. Look at the NDK for Android. If you know what you're dong with C, you can get Java and C++ talking to each other...
And from a very interesting blog post, In Defence Of C:
C is worth knowing, even if it isn't absolutely necessary.Some day you’ll need to go spelunking into the depths of your runtime environment. Maybe you’ll need to call some other C-based API for which you don’t have a convenient wrapper in your high-level language of choice. Like, say, mmap-ing a part of a file instead of the whole thing. Or maybe you’ll just want a bit of a performance boost. And on that day, boy will you wish you knew C.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
- bbguimaraes
- Chaos Rift Junior
- Posts: 294
- Joined: Wed Apr 11, 2012 4:34 pm
- Programming Language of Choice: c++
- Location: Brazil
- Contact:
Re: C++ to C
Yeah, I don't know of any language that makes you understand how computers (and thus your programs) work better than C and assembly. And that's knowledge you ARE going to need, even if you use the highest language in the world.
- GroundUpEngine
- Chaos Rift Devotee
- Posts: 835
- Joined: Sun Nov 08, 2009 2:01 pm
- Current Project: mixture
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: UK
Re: C++ to C
Great reply Falco, I swear I learn something every time I read your posts..
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
Re: C++ to C
Thanks guys!
- THe Floating Brain
- Chaos Rift Junior
- Posts: 284
- Joined: Tue Dec 28, 2010 7:22 pm
- Current Project: RTS possible Third Person shooter engine.
- Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
- Programming Language of Choice: C/C++, Python 3, C#
- Location: U.S
Re: C++ to C
What do you think about:Falco Girgis wrote:Yes. C code tends to be faster than the equivalent C++ code for a variety of reasons. Most of C++'s high-level object-oriented mechanisms have some sort of overhead associated with them. The most classic example is the vtable introduced by virtual functions. Each call to a virtual function requires a lookup table to invoke the correct "version" of the method depending on the type the object was instantiated as. When you get into some of the higher-level features of C++, especially the optional features, like exceptions and RTTI, you really start to introduce quite a bit more overhead.Benjamin100 wrote:Is C more efficient?
And if so, then why?
There is also a shitload more overhead associated with an object-oriented mindset rather than with C++ as a language. Classes tend to have constructors, destructors, copy constructors, etc, whereas a structural paradigm in C uses straight initializers. C++ favors encapsulating the shit out of datatypes with additional "accessor methods" that provide function overhead that normally isn't present in C. There are plenty more examples of this, but I am currently high on hydrocodone, so I will stop here.
C is best suited for applications that are extremely low level and/or require very efficient resource management. It is most often used with microprocessors, drivers, and operating systems. All of these tasks would be a pain in the ass to write in C++, because of their low-level nature.
Many libraries and APIs tend to favor C over C++ for obvious compatibility reasons. A C library can be utilized by C or C++, but the reverse is not (easily) true. OpenGL, OpenAL, OpenCL, and SDL are great examples of this.
http://www.youtube.com/watch?v=OB-bdWKwXsU , (look at about: 57:30) .
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: C++ to C
Excellent link, thanks for posting. Stroustrup knows his shit.THe Floating Brain wrote: What do you think about:
http://www.youtube.com/watch?v=OB-bdWKwXsU , (look at about: 57:30) .
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- GroundUpEngine
- Chaos Rift Devotee
- Posts: 835
- Joined: Sun Nov 08, 2009 2:01 pm
- Current Project: mixture
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: UK
Re: C++ to C
Legit saucedandymcgee wrote:Excellent link, thanks for posting. Stroustrup knows his shit.THe Floating Brain wrote: What do you think about:
http://www.youtube.com/watch?v=OB-bdWKwXsU , (look at about: 57:30) .
- wtetzner
- Chaos Rift Regular
- Posts: 159
- Joined: Wed Feb 18, 2009 6:43 pm
- Current Project: waterbear, GBA game + editor
- Favorite Gaming Platforms: Game Boy Advance
- Programming Language of Choice: OCaml
- Location: TX
- Contact:
Re: C++ to C
These APIs are not written in C just so both C and C++ can utilize them, but so pretty much *any* language can use them. Nearly every language has an FFI to C, but very few to C++.Falco Girgis wrote:Many libraries and APIs tend to favor C over C++ for obvious compatibility reasons. A C library can be utilized by C or C++, but the reverse is not (easily) true. OpenGL, OpenAL, OpenCL, and SDL are great examples of this.
Right, C is the Lingua Franca of programming languages.Falco Girgis wrote: Another interesting application of C that is emerging is using it as a wrapper language. Once I learned Objective-C, I thanked god almighty that I was well-versed in ANSI C. I was easily able to write minimal Obj-C and write the majority of my application(s) in C++. Then C++ and ObjC can communicate through a thin C wrapper. The same is becoming true for Java. Look at the NDK for Android. If you know what you're dong with C, you can get Java and C++ talking to each other...
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: C++ to C
Good point and nicely put.wtetzner wrote:These APIs are not written in C just so both C and C++ can utilize them, but so pretty much *any* language can use them. Nearly every language has an FFI to C, but very few to C++.Falco Girgis wrote:Many libraries and APIs tend to favor C over C++ for obvious compatibility reasons. A C library can be utilized by C or C++, but the reverse is not (easily) true. OpenGL, OpenAL, OpenCL, and SDL are great examples of this.
C is basically the entry point to the natively compiled world for other languages...
- THe Floating Brain
- Chaos Rift Junior
- Posts: 284
- Joined: Tue Dec 28, 2010 7:22 pm
- Current Project: RTS possible Third Person shooter engine.
- Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
- Programming Language of Choice: C/C++, Python 3, C#
- Location: U.S
Re: C++ to C
@GroundUpEngine and @dandymcgee Thanks
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself