+ 2
When is C better then C++?
I don’t know if I need to use C or C++ sometimes but most times it is C++. So when is C better then C++?
37 Antworten
+ 7
One important use of c today is the GPU parallel programming, CUDA platform allows to program kernels executed by the GPU for fast throughput, running algorithms extremely faster!
Those kernels are very tiny snippets of code executed simultaneously on the GPU.
In short you can create your own shaders!
EDIT : you can also use c++ or Fortran but c is best for this purpose
+ 7
Maybe C is a little bit lower.
For example new and delete in C++ are wrappers of malloc and free under the hood, so you pay the cost of wrapping.
Kernels need to be executed as faster as possible because the do a giant job.
But I didn't said faster.. I said better.
I said faster about CUDA and parallel programming for such tasks.
C is better for its light characteristic and high level than assembly but the most closer.
C instructions are equivalent to one to 10 ASM instructions generally.
ASM would be too difficult to manage and won't have too much gain probably, this is my opinion eh!
+ 7
Right, is good to dig until you find the truth!
First in my opinion but not only mine, never ever mix c whit c++, idk why guys use to program c using c++, if you want c use c, if you want c++ use c++, you will avoid a lot of troubles.
C++ gives you c features but in c++ way.
C++ has new/delete, it is for OOP, it call constructors/~
malloc/free wont, ok built in doesnt need but why make things complicated?
take a look at this, the explain it better than me.
https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new
+ 7
You can always call a c program from c++ or c# using the linker.. or who knows how many things you can do.
For example Unreal Engine is in C# but you make code in C++ and when you build your game it make it in C++ or idk.. ps4 code, Android etc.
I'm not expert, I really don't know well the publishing process at all, but the concept is as always to choose the best/right language that suits best your need.
You don't have to write all your project in C , you can use c# for skeleton, c++ for functionalities and C for kernels for example.
or python or java, .net framework ( c is not .net framework if I remember ), this is the final powerful part of good programming knowledge I think, I still haven't gotten.
But I think I'm on the right path if I take care.
+ 7
@Zeke c and C++ are two different things .
it's not just the library used, a c program should be complied in C.
C is C , c++ is C++, as you'll have seen in the link I provided and in many threads.
it's pretty like the using namespace std..
in real effective programs you won't use it. just an example
+ 6
Because C is more simple, fast and low level.. perfect for kernels!
And every framework in parallel programming is written in c , OpenCL, OpenGL, CUDA etc
yes c is not outdated, it is still one of the most important language, but it's not simple!
You will say I said it is! previously..
well I was referring to the low number of rules/keywords.
But it's not simple to manually manage resources ok?
+ 6
@Timon yes i disagree too you must use always OOP, that's why plain c would be better for GPU parallel programming, it doesn't need objects, it just have to execute little kernels, thousands of little kernels at higherestimpossible speed!
The OOP part will stay in a main program only for the CPU and logics.
+ 5
Yes! ... edited
+ 5
yup, it's not, but you should.. string is an object.. can you write a program without strings? probably yes.. you can use char *x[ ] ...?? mm.. this is c.
The point is.. are you sure to want the c c++ mixing libs/style approach for a project? or for a job?
+ 4
You want to use C when C++ is not available.
+ 3
When learning Machine languages, I think C is better.
+ 3
Yes, Timon, many features are not so nessesary there. Anyway c++ not supported in Arduino :-)
+ 2
Well said @Bac......
+ 2
You're wrong, C++ supports every low level operation C supports.
Have you used C++?
+ 2
Yes, remember that C89 is a subset of C++ and that C didn't change that much since then.
+ 2
And you can't create fast GPU shaders with C++? Strange.
+ 2
Why should it be faster? And C++ is also low level.
+ 2
I would be totally inclined to agree with you AZTECCO, but you can use things like malloc and free in a C++ program along with any other C++ feature, which gives C++ just as much control as C. What I think Timon and I would like to know is what is exclusive to writing a strictly C program versus C++? In other words, what can you do in C that you can't in C++? And be specific, because as I said, you can use malloc and free in C++. In fact, you have access to all of C's libraries in any C++ program.
I am genuinely curious about this, because I don't know the answer and haven't seen someone answer it yet
+ 2
This means: If I wanted to use a C shader, I could do it in C++ and if I wanted to write my own one, I could also profit of the additional features of C++. But currently, there are just C shaders.
And if at one point of time in your project you really need just C functions, that mustn't mean that you should use a C compiler. It's just a potential you could come back to.
I disagree you must use oop for every application. Sometimes performance is just more important. But that doesn't mean you shouldn't benefit from other C++ features that are suited.