Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What are the situations where C is the best choice? even better than C++?
Please give some reasons
Posted
Comments
[no name] 16-Jul-12 14:28pm    
When the micro processor that you are programming for that has an IDE that does not support C++.
Abdou Gadiri 16-Jul-12 15:06pm    
So, is that the only case?
Maximilien 16-Jul-12 15:41pm    
mostly yes.
lewax00 16-Jul-12 15:35pm    
Who needs an IDE? I'd say just when you don't have a C++ compiler... :P
[no name] 16-Jul-12 15:46pm    
I used to use Vi but then I got old and lazy :-)

First of all C++ quite fully supports C. So, if you want to use C, just do it in C++. There will be no difference.
If you don't care, then write C++. Some difference in performance appears when using OOP, but you really don't have to carry. The C++ OOP is the fastest OOP. Doing C OOP will not be faster. Anyway, C++ does not obligate you to do OOP.
Using C++ exceptions is more expensive for stack. But if you care, don't use C++ exception.
C++ templates, really no impact on performance.
C language is used in some specific situations. Mostly for making API to other languages or programs, plugins. In this case C acts as a bridge between some blackbox and usually C++, or some other language. And also C can be required in some platform specific. But this does not always mean that you are constrained to C.
 
Share this answer
 
v2
Microcontrollers and DSP chips still commonly use C. A lot of people that do kernel level programming also seem to lean towards C[^] as well. In general though, I'd say go with something that supports OOP, since it's easier to maintain and upgrade in the future than pure C. The performance differences are minimal between the two for most applications.
 
Share this answer
 
When programming microcontroolers. Even then it depends on the system you're programming for:

a) there may be no C++ compiler for the micorcontroller available
b) your system memory may be too restricted to allow for C++ due to its larger footprint
c) the responsible person(s) is/are not aware of an existing, suitable C++ compiler

(a) is becoming less of a problem since most companies that provide C compilers try to provide C/C++ in order to remain competitive
(b) is becoming less of a problem since small memory chips become more expensive to manufacture than larger ones (mostly due to low demand).
(c) is becoming less of a problem due to sites like these, and people like you asking such questions ;-)
 
Share this answer
 
There are no reason to use C in 2012.

Use C++, proper and modern C++.

It is better, faster, makes better and more clear code, reduces memory management issues, string issues, UI is a bitch in C, collections are a bitch in C...

In 2012, there are very few special cases where well programmed C will outperform well programmed C++; and I don't know what would be those, and if people are really comparing bad C code vs. bad C++ code.

There might be "niche" situation, mostly with programming micro-controlers that will force you to use C, but it will not be a performance issue, but because they are using an old C compiler that works and has been properly tested and generate safe binaries.
 
Share this answer
 
1) Because it posses better performance and execution speed (real-time programming & etc). A performance difference....
2) Compiler isn't support C++ language, but support C language
(That's something I haven't thought of, but let's say there isn't, in that case, is the lack of C++ compilers for micro controllers means that there can't be, or means that there shouldn't be. Or maybe they didn't design one because it's complex.
3) Probably, it's better to use a tool that is better suited to your task. It's depends...Sometimes the C language can solve your problem more efficiently for certain tasks.
4) A lot of programmers raised on C will stick to C because it's what they know, with logic like "well if you're not going to use the features of C++, you might as well use C". An experienced C programmer has techniques that he's comfortable with and simply doesn't see a need to switch.

But do not forget (!!!!!) that today's C++ is a multiparadigm programming language, one supporting a combination of procedural, object-oriented, functional, generic, and metaprogramming features. This power and flexibility make C++ a tool without equal, but can also cause some confusion. All the "proper usage" rules seem to have exceptions. How are we to make sense of such a language?

To make sense of C++, you have to recognize its four sublanguages:

* C. Way down deep, C++ is still based on C. Blocks, statements, the preprocessor, built-in data types, arrays, pointers, etc., all come from C. In many cases, C++ offers approaches to problems that are superior to their C counterparts
(C formerly called "C with classes" )
* Object-Oriented C++.
* Template C++.
* The STL.

Please read following article:
http://wolfhalton.info/2009/09/20/why-do-you-use-c-instead-of-c/[^]
 
Share this answer
 
Comments
lewax00 16-Jul-12 16:07pm    
You can still do C in C++, so I don't think 3 and 4 really apply (why have 2 compilers when you can use the same for both?). And I think that applies to 1 as well, no reason identical code should compile significantly different (specifically, when you aren't using C++ features).
Volynsky Alex 16-Jul-12 17:49pm    
Have you ever tried to find C++ compiler for PIC?
hi Abdou I think that we must use c when the task to solve require speed because the Object Oriented Model in c++ is a bit heavy than the simple and structured c.In another hand the OO paradigm of c++ is the most powerfull tool
for big projects.
 
Share this answer
 
Comments
Abdou Gadiri 16-Jul-12 15:01pm    
If we exclude OO paradigm of C++, won't it be as fast as C ?
Lisbey Collazo Alfonso 16-Jul-12 15:26pm    
no.
lewax00 16-Jul-12 15:30pm    
Based on what exactly? Why should a line like "a = b + 1;" be compiled any differently in C++ than it would in C?
lewax00 16-Jul-12 15:28pm    
I believe so, I think most compilers don't include the overhead for features like OO until they're actually used. So if you don't need them, don't use them, most C is valid C++ anyways. And I would think any decent compiler would generate very similar output for a C program as it would a C++ program with the same code.
Most reasons have been said, but here it goes:

C has more compiler support than C++ (mostly microcontrollers, for example, PIC).
It also has smaller memory footprint (again useful in microcontrollers).

So, its simple, use C when portability or use in very constrained environments is required. Also when you want to learn about memory management.

Otherwise, use C++ (you can even include C code in C++ or code "C compatible code" in C++). With very few exceptions, C is a subset of C++.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900