C++ to C

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

C++ to C

Post by Benjamin100 »

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
User avatar
dandymcgee
ES Beta Backer
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

Post by dandymcgee »

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! :twisted:
User avatar
bbguimaraes
Chaos Rift Junior
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

Post by bbguimaraes »

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.
User avatar
Falco Girgis
Elysian Shadows Team
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

Post by Falco Girgis »

bbguimaraes wrote:which means anything you can do in C, you can do in 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 an

Code: Select all

extern "C"
pragma.
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).
C++ adds its OWN standard library, but C already has a standard library.
Benajmin100 wrote:I thought it was just that C didn't have classes and C++ did.
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).
Benjamin100 wrote:I mean, why use C?
I will answer that by answering your next questions:
Benjamin100 wrote:Is C more efficient?
And if so, then why?
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.

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.
Benjamin100 wrote:Do you think I would benefit greatly in learning to use C?
Would it be easy after learning C++?
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.

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...
User avatar
bbguimaraes
Chaos Rift Junior
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

Post by bbguimaraes »

Exactaly what I'd say, if I was giving a long answer.
Falco Girgis wrote:
bbguimaraes wrote:which means anything you can do in C, you can do in 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 an

Code: Select all

extern "C"
pragma.
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".
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: C++ to C

Post by Ginto8 »

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...
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.

And from a very interesting blog post, In Defence Of C:
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.
C is worth knowing, even if it isn't absolutely necessary.
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.
User avatar
bbguimaraes
Chaos Rift Junior
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

Post by bbguimaraes »

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.
User avatar
GroundUpEngine
Chaos Rift Devotee
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

Post by GroundUpEngine »

Great reply Falco, I swear I learn something every time I read your posts.. ;)
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: C++ to C

Post by Benjamin100 »

Thanks guys!
User avatar
THe Floating Brain
Chaos Rift Junior
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

Post by THe Floating Brain »

Falco Girgis wrote:
Benjamin100 wrote:Is C more efficient?
And if so, then why?
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.

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.
What do you think about:
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

ImageImage
User avatar
dandymcgee
ES Beta Backer
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

Post by dandymcgee »

THe Floating Brain wrote: What do you think about:
http://www.youtube.com/watch?v=OB-bdWKwXsU , (look at about: 57:30) .
Excellent link, thanks for posting. Stroustrup knows his shit. :lol:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
GroundUpEngine
Chaos Rift Devotee
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

Post by GroundUpEngine »

dandymcgee wrote:
THe Floating Brain wrote: What do you think about:
http://www.youtube.com/watch?v=OB-bdWKwXsU , (look at about: 57:30) .
Excellent link, thanks for posting. Stroustrup knows his shit. :lol:
Legit sauce ;)
User avatar
wtetzner
Chaos Rift Regular
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

Post by wtetzner »

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.
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: 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...
Right, C is the Lingua Franca of programming languages.
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.
User avatar
Falco Girgis
Elysian Shadows Team
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

Post by Falco Girgis »

wtetzner wrote:
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.
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++.
Good point and nicely put.

C is basically the entry point to the natively compiled world for other languages...
User avatar
THe Floating Brain
Chaos Rift Junior
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

Post by THe Floating Brain »

@GroundUpEngine and @dandymcgee Thanks :mrgreen:
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
Post Reply