What is the difference in the usage of C vs C++? Can C and C++ programs be interchanged, or are they separate languages? If I learn C++ would I know C?
This is a really long answer because there's a lot of nuance in the situation; hopefully you will find some answers within.
The TLDR: They are effectively separate languages as used by their practitioners, and you won't really know one by learning the other even if some of the knowledge will transfer cleanly.
There are subsets of C and C++ that are essentially the same language. Their respective standards groups have compatibility between the languages as an explicit goal, though they are not perfectly synchronized so there tend to be places where new features have not been brought in from the other yet.
Many compiler suites provide C and C++ support via single compiler that switches how it works based on compile-time options. So, aside from a couple of assumptions in the type checking and symbol naming, the C-compatible subset of C++ compiles to machine code very much like what you would get by compiling that same code written as C via the C-mode compiler.
C has a mountain of legacy code in embedded systems, operating systems, etc. So its standard committee is very conservative in how they change the language, and compiler vendors are slow to move to newer versions. It's a very old language; all the ideas in C were well-developed in the 60s and the major distinction it has from its predecessors is a static type system and memory model that recognize the shift from word-oriented to byte-oriented addressing in the machines that the developers used. Look and feel and basic feature set were intentionally kept very close to B, which was in turn a mostly syntactic overhaul and simplification of BCPL, which was in itself a subset of CPL oriented towards machine-level bootstrapping of the CPL language at the University of Cambridge and University of London. The design of CPL itself started in 1963, based on the ideas in ALGOL 60 but aimed at a larger domain of programming areas.
C++ was an attempt to bring some newer ideas in programming languages (type-based abstraction, Simula-style objects, generic programming) to the C language. Because C structures are capable of encoding most compound data structures and can also contain references to code (via function pointers), the features of C++ were prototyped and initially implemented via a preprocessor to a C compiler. The new features just expanded into a particular encoding in C structures. They still follow a similar implementation technique, though modern compilers may find better code for it due to knowing more about the intent.
Bjarne Stroustrup, the initial and primary designer of C++, used the design criteria that you shouldn't have to pay a runtime cost for features not used by your program as a guide for how he integrated features into C++ atop of the base C language. He also held compatibility with C as a strongly desired feature. These guides to the development of C++ are probably the source of both its wide acceptance and the sheer weight of books on the "right way" to code in C++. It's very much an "a la carte language", by design, but one must be tasteful about which features are used and how they are used together.
Because the extensions of C++ over C focus on abstraction, or the idea of writing programs that hide (hopefully) irrelevant details of how they work from other programs that call them, a programmer that learns C++ first may well feel lost as to how to build things in plain C, as these are precisely the details that are quickly abstracted away. However, highly experienced C++ programmers will most likely have learned exactly how C++ features translate to C (which is practically prerequisite to knowing which features one ought to use in C++ when designing a library or other system from scratch) and will find a shift to programming in C less daunting but perhaps more irritating than to the novice C++ programmer. It can be quite frustrating to have powerful, time-saving tools taken away, even if the same ends can be achieved through a manual encoding of the same techniques.
On the flip-side, an experienced C programmer will have a leg up on the novice C++ user in understanding the costs and benefits of various C++ features, but may be tempted to avoid hiding the sort of details they are used to working with. Experienced C programmers learn a distrust of opaque code bordering on the neurotic as a survival mechanism, so exploiting features that hide things is very difficult to motivate. This would lead to a rather stilted style of C++ code and grumbling from fully-converted C++ coworkers, though, which is perhaps why many in the C++ camp strongly advocate learning C++ first.
One must also keep in mind that although most of the features in C++ have been around in programming languages since the late 60s/early 70s, it's taken quite a while to work out how the underlying concepts fit together in the engineering of systems. A lot of early "object-oriented" work, especially once "Object Oriented" became a silver bullet buzzword in the industry, ended up chasing bad analogies and led to poorly designed systems as a result.
More recent approaches to designing programs in C++ take advantage of a rising level of understanding among practitioners of both object oriented design as well as alternative approaches such as functional programming and type-generic programming. C++ itself has also advanced at a much faster pace than C (for various reasons it hasn't been used as widely in areas that require really long-term support, like OS kernels, as C has, which makes its practitioners a tad bit less conservative in language extension) and so today's C++ is markedly different in style (if not substance) than that of 10-15 years ago.
To wrap it up: both C and C++ are widely used, but the features of C++ beyond C are of the sort that can have a huge effect on the style of programs and what you have to know to understand them at different levels. An intermediate user of either language, unfamiliar with the other, will likely see a typical example of the other as rather foreign. Expertise at either can be both helpful and detrimental towards learning the other, depending on the situation.
The two languages are closely related with C++ being a direct descendent of C via way of "C with Classes", essentially just a series of additions to C. This was eventually developed further and split off into its own language to become C++.
Today, the two have diverged substantially, especially with respect to the situations they tend to be used in, and what is considered "good" or "proper" coding style in each. However, despite this divergence C remains almost a strict subset of C++ so it is possible to write C code and (as long as you avoid doing a few things) have it compile without issue using a C++ compiler. The reverse is not true. Short of restricting yourself to the subset of C++ that is also in C, you will not be able to compile C++ code using a C compiler.
That being said, modern C++ code looks substantially different than modern C code even though they share a similar past, as the language communities have developed very different philosphies. C++ code will generally use a higher level of abstraction than C code. You will often see C programmers much more willing to get down and dirty, so to speak, working at the bit and byte level, and performing manual memory management (although these can also come up in C++ as well). C tends to be used for very low level projects: firmware, operating systems, embedded software, ect. C++ is generally used at the application level on projects where performance is still desirable, but a higher level of abstraction is appreciated, such as video games and web browsers.
Which one you learn depends a lot on what you want to do. If you're interested in getting down close to the hardware, or value a compact and comparatively simple language I would start with C. If you want to get into application development or have no interest in low level software then definitely go with C++.
I took C++ class in uni for the credits for a semester, did well but never coded again.
Never learned the difference between all C languages and I know next to nothing about programming in general, but I am ok at math and logic stuff
Since I’ve started exploring C, I’ve realized that many programming languages rely on libraries built using C “bindings.” I know C is fast and simple, so why don’t people just stick to using and improving C instead of creating new languages every couple of years?
I recently posted a post where I was asking for resources for C programming. There people suggested me to start learning C++ if it's my ultimate goal.
Earlier I was going to learn C++ after C but now I'm rethinking my decision.
Should I just start with C++ and skip C?
I am a python user so I may not have all the information but why do people and especially big companies still use C instead of C++? Isn't C++ the superset of C so it is supposed to be better? Also OOP is literally booming everywhere while C is still procedural. I don't suppose that there is much speed comprise between the two, so is there a specific reason?
I was talking to a programmer and he said that these three languages are important because they are the foundation of everything. He also said that if I learn all three, it will be easy to learn anything.
Why do people use C over C++?
If you can write C code in C++, what is the reason to not use C++ if it just has more features that you might want to use: smart pointers, vectors, templates ect. I've seen a lot of people use C over C++. The main examples I can think of are Linux and DWM window manager. I though that maybe it's because a lot of people write code, and it's just easier to use C because it's much more simple, but what is the problem with just googling the things that C++ provides and just use it instead? I've also seen solo devs use C so...
Should I use C over C++?
I've a casuall advanced beginer. I like to have controll over the resources the program uses because I think its' fun to try and find the most optimal way for the program to execute to achieve your goal. I genuenly use C++, but I don't really use any of it's features that I mentioned in 1-st paragraph. The only things I really use from C++ are std::cout and new, delete. I was wondering if I should switch to C instead. I've head that it is simplier than C++ because it has less features, but I'm a bit afraid I'm gonna miss some really helpfull features that C++ has that I will one day need. Writing C-like code in C++ feels like I'm procrastinating the decsision I have to make between the two languages.
Why there are so many people using the C language instead of C++?, I mean C++ has more Cool features and the Compiler also supports many CPUs. So why People still using C?
Edit: Thanks for all the usefull comments :D
Disclaimer: I am mainly a web dev and I had only recently started to interested in low level languages
So, I want to beginn learning c or c++ but I am not sure what to pick I know that C is lighter and C++ has a bunch of more features but I am still not sure
I will eventually learn both but to get a better prospective which one should I learn at first?
Hello, i wanted to inquire about what the differences between C and C++. I'm familiar with the fact that originally C++ was C but with OOP and a few improvements, however i know that these really aren't the only the differences now. If anybody could explain comprehensively the differences or link to a resource that does it would be much appreciated.
Extra note: I'm familiar with C++ but not C
i regularly come into this sub and hear that C and (modern) C++ fit different use cases and are valuable in their own respective rights.
simultaneously, this sub also suggests that modern C++ is better than C with classes.
This would all be fine and good, except that we also all agree that C with classes is better than C.
So, my question is this: huh?
I would call my self intermediate in C++ and basic in C. I seriously don’t understand why you would C instead of C++ except if you don’t have access to libraries.
Hey,
I wan't to learn C# to use in Unity, but also to learn C++ to use for unreal engine at some point.
I know both languages derive from C, but I have no purpose of C at this point. I don't know if it'd make sense to learn anyway, to have that fundamental understandment.
I kind of just wanna get started in programming, and learn one language to stick with for a while, because I have a feeling that they are fairly similar in terms of concept, just different syntax, but I don't know what language to start with.
My proficiency at the moment are just python and javascript.
So I've been studying web development for 1 1/2 years now, by following through the odin project, I've built my own web application that i use (markdown app) using react, created a cli tool to fetch data from a game using restful apis to diplay on the terminal since thats where i spend most of time on, a somewhat working chrome extension that tracks how much time and how many times you've visited a website, and I've also built an r/place clone using express and websockets, but now i feel bored of web development and want to dabble into more complex stuff like mid or low level programming languages like c or c++, i want to be classified a self taught software engineer and not soley just a web developer and dont want to be restricted to only web development, so the question here is should i be learning c or c++? Both of these are mid or low level languages right? And c seems to be much more simpler contrary to c++, does learning c have any advantages over c++ if i were to pick it instead of the latter?
A former co-worker talked about how C++ is just better than C, since it has more functionalities and if memory is an issue (like in a micro processor) you just avoid including packages and write C code in your .cpp file.
But a co-worker at my new office instead had the mentality that why should we even bother with .cpp files if we are already 99% certain we’ll need to write everything in pure C.
I understand this forum might be biased but I would still like to know your opinions on the matter. When, if ever, is it better to code in pure C instead of C++?
Genuine question, I want to understand the landscape here.
Two arguments I’ve heard that can hold water are:
-
There’s no C++ compiler for my platform
-
My team is specialist in C, so it makes sense to play to our strengths
Are either of these you? If so, what platform are you on, or what industry?
If not, what’s the reason you stick to C rather than work with C++ using C constructs, such that you can allow yourself a little C++ if it helps a certain situation?
I read a post recently where somebody had a problem that even they identified as solvable in C++ with basic templating, but didn’t want to “rely” on C++ like it’s some intrinsically bad thing. What’s it all about?
EDIT: for those asking why I have to ask this repeatedly-asked question, the nuance of how a question is asked can elicit different types of answers. This question is usually asked in a divisive way and I’m actively trying to do the opposite.
I am new to programming and I want to adopt the traditional (hard-core) approach of starting with a mid-level language in order to understand the basics of programming and the moving on to Python, Javascript, etc.
If I had to choose between C and C++, which one should I chose to start with?
When I decided that I wanted to learn I consulted in forums, coming to the conclusion that if I wanted to learn to program anything I should learn c++ (they said it was the most used language in the video game industry and other leading applications like Photoshop).
I read some recommended books, web tutorials, videos...and I always had the feeling that everything was more complicated than necessary (classes, inheritance, virtual functions, constructors, destroyers, processes that occurred under the hood and forgot to take into account, multiple ways to initialize variable-objects.
But the worst part of it was reading code from other programmers. Very difficult for me. I never found any source code commented enough to be understood by anyone other than the writer.
I decided to start programming my own projects. I started with a text editor that used the mswindows console API. Everything went well until I started making mistakes. Some errors were detected by the compiler. Some understood them but others (STL) did not. The worst errors are those that do not interrupt the execution of your program and that you do not understand why your program does not work as it should.
In the books there is almost no mention of how to debug your program. Debugging is not part of the language specifications. I think this is one of the biggest mistakes made by those who teach a programming language. There's not much literature on the subject either. There aren't many debuggers. Trying to follow step by step the execution of your program through the debugger to see what happens at all times is, in my opinion, the most effective. It was impossible when I hit the STL.
I decided then that to know what was happening to my program I had to use my own functions. I started to implement my own containers ( no templates), no inheritance, no virtual functions. Everything as simple as possible. And there was light. I finally figured out why Mr. Torvalds doesn't like C++.
I decided to look more closely at the C language. I read Dennis Ritchie's book. Then I read Mr. King's book, which for me is the best book. I discovered the simplicity of language. It has hardly changed over the years (not so with C+++). I also read about assembler, ABI and undefined behavior. I migrated my program from C++ to C and since then I have mastered my programs and not the other way around. I hope this will be useful to those who are just starting out.