C or c++

Pages: 12
Be honest. C can do everything c++ can right? I wanna be able to develope OS, programs that speed up internet, graphics and stuff. I heard the mother of all graphics, windows, was written in C. I like that. But which language should i pick and why while both languages can do the same and c is closer to the machine?
Be honest.

Never. It's against the forum's policy to be honest.

C can do everything c++ can right?

Er... no. Although they both compile to machine code, C++ has (in my opinion) a much stronger system for generics than C (templates v. parameter macros), and its object-oriented programming facilities are actually noteworthy. Also, with C++0x, C++ will get lambda functions and the keyword auto, which I doubt that C will ever get.

I heard the mother of all graphics, windows...

?

But which language should i pick and why while both languages can do the same and c is closer to the machine?

C is a simpler language and has a code:MC ratio that's closer to 1:1 than C++. It's easier to shoot yourself in the foot in C, but I find it's easier to debug when you do. C++ is a bit more complex, but offers a greater choice of how to program things (in many cases making a task a lot easier), however with more choices, there's more room for hard-to-debug errors.

I can't hope to give an unbiased opinion, so... your choice.

-Albatross
AFAIK, Windows is in C because that was what was available back then, but C++ is much more powerful than C. True that you can get things done in C, but you can get things done better in C++.

For example, I wish you good luck trying to manage collections in C. You'll have to use C arrays, meaning you will have to keep dynamically allocating memory, copying data around removing pointers, etc. It is just not worth it. In C++, you could create a simple array class that would do it for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
static const int C_StdCapacityIncrease = 32;

template<class T>
class Array
{
    T *_arrPtr;
    int m_size;
    int _capacity;
    int _capacityIncrease;

public:
    Array(int initialSize = 0; int capacityIncrease = C_StdCapacityIncrease)
        : _arrPtr(initialSize ? new T[initialSize] : 0)
        , m_size(initialSize), _capacity(initialSize), _capacityIncrease(capacityIncrease)
    { }

protected:
    void IncreaseCapacity()
    {
        _capacity += capacityIncrease;
        T *tempPtr = new T[_capacity];
        memcpy(tempPtr, _arrPtr, m_size);
        delete[] _arrPtr;
        _arrPtr = tempPtr;
    }

public:
    int Add(const T &item)
    {
        if (_capacity <= m_size)
        {
            IncreaseCapacity();
        }
        _arrPtr[m_size] = item;
        return m_size++;
    }
    void Clear()
    {
        if (_arrPtr)
        {
            delete[] _arrPtr;
            _arrPtr = 0;
        }
    }

    //Destructor
    ~Array()
    {
        Clear();
    }
};


That would be just an array class with very basic functionality, of course, and look at all those lines, because all that is needed to properly maintain a C array. In C, you would have to code standalone procedures to take care of all that, like this:

1
2
3
4
5
6
7
8
9
10
11
12
//The C way
void* CreateArray(int initialSize, size_t sizeOfItem)
{
    return malloc(initialSize * sizeOfItem);
}

void FreeArray(void* arrPtr)
{
    free(arrPtr);
}

//etc. 


The C way then imposes a lot more work to each array you create: You must ensure you delete memory yourself, you must keep track of sizes and capacities yourself, etc.

And don't even get me started in the elimination of an item in the array in the middle of it. :-S

C++ produces far better code, IMHO.

P. S. : I know that if you are using C++ the best would be to use std::vector. I just made an array class as an example of the complexities that can be abstracted into a class.
Last edited on
closed account (S6k9GNh0)
C++ was around during the creation of Microsoft Windows, it just wasn't quite as popular.

Windows is definitely *not* "the mother of all graphics" and you definitely deserve to be smacked for that one.

Generally, C is based on a procedural and functional design where as C++ is based on an OOP design. C++ provides templates for generic types, object type that are functional, and so on. This argument goes on and on.

C++ becomes more and more popular as it provides higher level facilities while still giving the power (and speed I guess...) of C. C++ is a superset of C after all.
closed account (zb0S216C)
I don't know where I heard this: C is more commonly used than C++ when developing kernels, or anything that communicates with hardware. Personally, I'm a bit unsure about that, but, I've peeked into the sources of hardware-communicating software, and many of them are written in C.

I also heard somebody say: C++ is more platform dependent than C. I honestly don't know what to believe.
Last edited on
Albatross wrote:
easier to shoot yourself in the foot in C
Bjarne Stroustrup wrote:
but when you do it [in C++] blows your whole leg off

:P

Framework wrote:
C++ is more platform dependent than C

On phones and consoles and stuff? Code::Blocks happily finds a compiler to work with on all three major platforms as far as I know.
Last edited on
I've peeked into the sources of hardware-communicating software, and many of them are written in C.

I don't know the situation of another regions
But in Taiwan, many people who involved in hardware-communicating software are not come from
CS but EE
And most of the EE students don't know what is C++(they always think C is almost equal to C++)
For EE students, c is almost enough for their application
No matter how many benefits c++ could bring to them, most of them would not like to learn it

First : too damn complicated compare to c
Second : most of their comrades don't know c++, it is very difficult to find one who didn't write c in c++
Third : many of them believe "higher level abstraction means slower and fatter"
Last edited on
Generally speaking (I emphasize on generally because its not always the case)...

C is commonly used to do lower-level stuff (like making a kernel for an OS) because it is lighter and faster than C++, and also communicates with hardware in a more direct manner. An example of a famous program written in C is the Linux kernel. They usually make libraries in C as well (like SDL for example).

C++ is used for situations where polymorphism and OOP (amongst other things) are necessary, like when designing a large computer game for example. In fact, developments made in gaming technology was one of the main drivers into moving the world of OOP forward.


At the moment, C++ is more popular than C because it follows (encourages) modern programming concepts like objects, encapsulation, and polymorphism. C follows the older procedural and modular approach to programming.

In the world of programming, they both have their own strengths and weaknesses and will definitely still be around for a looong time. They were both initially made by the same people and share quite a lot of syntax and functionality with one another.
Last edited on
faster than C++

c never faster than c++ if they do the same thing, the machine code should be the same

C++ is used for situations where polymorphism and OOP is necessary

c++ is a multi paradigm language, oop is only one of the paradigm support by c++
Exactly, most of the standard library of c++ are designed under the idea
of generic programming but not oop
c++ is a multi paradigm language, oop is only one of the paradigm support by c++

That is why I was saying "generally speaking" because it is "not always the case", hence my first sentence.

c never faster than c++ if they do the same thing, the machine code should be the same

A lot of people debate about this, so you could be right. I personally don't really care which one is faster, just so long as I can get the job done when using either one of them.


Last edited on
C can do everything c++ can right?
Yes. In the same way Assembly can do everything C can. The question is whether it will be practical.

programs that speed up internet
That's oddly specific... In any case, it's not too hard. libcurl lets you easily do that for HTTP/S.

I heard [...] windows, was written in C.
This is true, to some extent. The kernel is in C, but nearly everything else that runs in user mode, such as the API and the desktop environment, is in C++.

But which language should i pick and why while both languages can do the same and c is closer to the machine?
Who said it is? It doesn't provide as many liguistic features as C++, which means you won't be able to easily abstract away even if you want to.

Albatross:
It's easier to shoot yourself in the foot in C, but I find it's easier to debug when you do. C++ is a bit more complex, but offers a greater choice of how to program things (in many cases making a task a lot easier), however with more choices, there's more room for hard-to-debug errors.
You think so? I never encountered a bug in C++ that would have been easier to deal with in C. If anything, C++ encourages programming styles that make stupid bugs (uninitialized objects, un-uninitialized objects, and other sorts of bugs that can be prevented by making grammatical correctness and sematical correctness equivalent) harder to be produced. This also means that any bugs that do appear will be some hard S.O.B.s.
What sort of bugs are you talking about?
closed account (S6k9GNh0)
helios, I disagree with debugging C. I don't know if you've ever had a complicated template error but it sure is a headache. Classes also give a sort of abstraction that can hide bugs.
Last edited on
Templates can be a bit of a pain to debug, but they're certainly less painful than the C alternative. At least you can step through a template.
I think if your primary focus is in OS-level programming, C is fine (in fact, you may look into Google's relatively new go language): C is much smaller and simpler than C++, so you can focus on the OS. Also, what you learn in C is not lost when you go use C++ one day.

C++, in theory, should be ok, too, but it's easier to get bogged down in language minutiae with C++ when you really prefer to be bogged down with hardware/machine-level details

depending on how low you want to go, you may be playing with POSIX (or lower levels) which is a C lib anyways

(agree 100% with helios' comment on template debugging)
If you read about portability and compatibility in C/C++, you easily find out that while C++ is a better language (yes, I consider it better than C), its portability plain sucks: Want to create a library of C++ classes? Sure you can as long as it is compiled with the same compiler as the program that uses it. It is sooo specific, that even a change of version in the compiler is good enough to break this.

So I guess that because of this, OS makers only choice is to go with a public C interface. This is what Windows did, and from what I read here, it is also what Linux did. C interfaces are the only ones safe enough to move around with different compilers.

Just remember this: You can publish a C interface commanding C++ internals.
Many software firms actually use both of them together when implementing a solution.

Libraries containing generic functions are usually written in C, whilst the main program is written in C++. Together, they can be quite a powerful combination.

But knowing one means that you already have a good idea of the other. They are very close to one another, but both encourage different programming methodologies. However, if you know both, that could pave the way for greater opportunities.

So always look to expand your horizons, because it can only do you good. I know C very well, but I am currently new to C++ hence why I am here. And to be very honest, I find C++ a lot more intuitive with its methodologies (even more so than other OOP lang's like C#).
Last edited on
since c is just a subset(c++ is not a superset of c) of c++
Knowing c++ means that you already have a good idea of c, but just knowing c would not
make you a good c++ coders.There are too many different between c and c++

This is the commend come from the father of c++, the title is
Learning Standard C++ as a New Language
http://www2.research.att.com/~bs/new_learning.pdf
Last edited on
c & c++ are alike. I would learn c++ first. It's a great language. I would definitely learn both of them. Learn whichever one you would like to be better with first.
Last edited on
I'm going to go back and read more then the first two posts on this thread after I write this but I feel it has to be said. Bill Gates is a business man first and a geek second, the Windows API is written in C because at the time of it's development C was popular and writting an entire API in C++ would have been a gamble that he would not have been willing to take. Since then the API has been largely adapted to C++ (due in no small part to the dominance of C++ no doubt) as well as M$'s developed languages.

EDIT: If you want a real world example of what might have happend had he gone the other direction just try to write something for Apple. Their language of choice is Objective-C which is related to C and therefore related to C++. But if you had a gun to my head I wouldn't be able to work with that mess and I have given up on any hopes of wriotting for that platform. No doubt some of you will be able to do it. I know for a fact that some people who have commented on this thread do, but the majority of us will be left screaming and cursing at our monitors.

EDIT EDIT: By the "mother of all graphics and windows" I assume you mean the Windows API, Direct X and OpenGL. Yes they are written in C but they are also VERY compatible with C++.
Last edited on
Bill Gates is a business man first and a geek second, the Windows API is written in C because at the time of it's development C was popular and writting an entire API in C++


I heard a rumour that Bill Gates himself isn't actually a (good?) programmer, but befriended programmers to code on his behalf and took credit for most of the work.

Of course, rumours will be rumours.

Pages: 12