PDA

View Full Version : C and C++ It's so confusing..




QueenZ
Jan 22, 2009, 12:43 PM
OK, So i have learned C++ a little bit..

// my program in C++

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}

And now i started learning C and i also have learned C a little bit..

// my program in C
#include <stdio.h>

int main()
{
printf ("Hello World!");
return 0;
}

C and C++ is the almost the same except that there are some more functions and features in C++. But now when i have learned C++ a little but and started learning C it's kind of confusing and i want to write stuff from C++ to my C program and it's really all mixed up...

I mean in C i have t o include stdio.h where in C++ i have to include iostream and i don't have to write .h in C++ but i do in C... weird..

and also i output with cout << in C++ but with printf in C..

How can i do it if i keep mixing these languages... I guess if i want to learn C or C++ then i kind of have to learn both of them to understand it completely.. why did they make them so different..? :(

C is like basic of C++ but then C++ is very much different and i don't like that...

What do you guys think about this situation..?



toddburch
Jan 22, 2009, 12:46 PM
Personally, I got over it pretty quick.

lee1210
Jan 22, 2009, 01:04 PM
OK, So i have learned C++ a little bit..

// my program in C++

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}

And now i started learning C and i also have learned C a little bit..

// my program in C
#include <stdio.h>

int main()
{
printf ("Hello World!");
return 0;
}

C and C++ is the almost the same except that there are some more functions and features in C++. But now when i have learned C++ a little but and started learning C it's kind of confusing and i want to write stuff from C++ to my C program and it's really all mixed up...

I mean in C i have t o include stdio.h where in C++ i have to include iostream and i don't have to write .h in C++ but i do in C... weird..

and also i output with cout << in C++ but with printf in C..

How can i do it if i keep mixing these languages... I guess if i want to learn C or C++ then i kind of have to learn both of them to understand it completely.. why did they make them so different..? :(

C is like basic of C++ but then C++ is very much different and i don't like that...

What do you guys think about this situation..?

I think you need to treat them as completely different languages, and treat any similarities as coincidental. Some knowledge of one might transfer to the other, but that applies between most programming languages.

I would honestly focus on learning one well first, not getting enough knowledge to put together "Hello, World!" in one, then move on to the other. C++ is not C, and C is not C++. There is certainly some code that will compile in both, and even act the same way in both, but treat this as the exception, not the rule.

-Lee

ChrisA
Jan 22, 2009, 01:15 PM
As a beginner, yes the exact syntax is what you care about now. But later what matters is how to break big problems up into small problems and those into smaller problems in such a way that the little problems become independent from each other.

It's like if you were learning to become a writer. Yes you have to learn to use a word processor but learning Word is not that same as learning how to write a novel. Learning how to get the #include stuff correct is like learning Word. Something you will get past and not think to much about later.

What I do i just write using whatever funtions and APIs I need then later lookup which #includes are required. Actually I use GNU autoconf and some #if preprocessor stuff because on different systems (Mac OS, Linux, Solaris,..) you need different #includes and you want to automate that. Yess it's confusing but in the end it is just "boiler plate" template stuff that gets cut and pasted in from some other project you did.

QueenZ
Jan 22, 2009, 01:15 PM
So which language should i learn then? C or C++?

lee1210
Jan 22, 2009, 01:19 PM
So which language should i learn then? C or C++?

This could rage for days. It has before. C is more basic, in my opinion, and i don't think heading full-bore into Object-Oriented programming is the right move right now. If you aren't going to use OOP, than there's not a lot of arguments for using C++. As such, i'd say start with C. It will give you a strong foundation in control structures, dealing with variables, functions, pointers, etc. that will carry over to a lot of other languages. C++ can provide the same, but i feel that it's so "multi-paradigm" it can lead the beginner astray.

-Lee

EDIT: What ChrisA said above is important. You are learning some basic tools. You have never used them before. Don't get too frustrated now. You have a lot of time to spend being confused before you're comfortable programming. Don't worry too much, just ask questions when something doesn't make sense, don't get too worked up about it.

I'm a Mac
Jan 22, 2009, 01:21 PM
So which language should i learn then? C or C++?

It depends what you want to do. C provides a good foundation to any of your programming endeavors, and fully learning it (which I wish I did) will probably help you in the future. What is your background in? Is C++ your first language? If you want to get into Windows Programming, then C++ is a good language to learn. For the Mac, C is more useful because it would make it easier to pick of Objective-C.

MacRumors Guy
Jan 22, 2009, 01:39 PM
C++ is just bad. Go with C.

QueenZ
Jan 22, 2009, 01:50 PM
C++ is just bad. Go with C.

Bad?? Tell me at least 10 reasons why it's bad

autorelease
Jan 22, 2009, 02:08 PM
Bad?? Tell me at least 10 reasons why it's bad

http://en.wikipedia.org/wiki/C%2B%2B#Criticism

http://everything2.com/index.pl?node=The+C%2B%2B+Programming+Language+Freakshow (slightly outdated)

MacRohde
Jan 22, 2009, 02:09 PM
You have to look at C and C++ as two completely different languages that simply share a vast amount of syntactical (and semantical) heritage.

I you wan't to learn programming you can use for low level OS programming or hardware programming go with C. Other than that C++ is much more applicable.

While C only supports simple procedural programming. C++ is a multi-paradigm language and supports procedural programming, object-oriented programming, generic programming, and some weird wanna-be functional programming.

So although C isn't bad per se. I would say go with C++ for the following reasons:


Used in more arenas than C
More modern language, which means many more resources, books, tutorials, help from other programmers etc.
Can be used for object-oriented programming which is the paradigm used by the vast majority of programmers

lee1210
Jan 22, 2009, 02:21 PM
And so it begins...

Languages aren't better than one another, but sometimes one language is a better tool for a particular job than another.

We can (and will given 1/2 a chance) argue about this for days. No one will change anyone's mind, and you will not get a definitive winner on which language you should start with. Just pick one and go, and don't think you're learning the other at the same time.

-Lee

MacRumors Guy
Jan 22, 2009, 02:31 PM
Let's be serious here C++ only has 3 advantages over C: it's more type safe, has a larger library and namespaces. (iostream IO is also much nicer than the stdio C crap).

Now as counter points to the previous poster:

C is more used than C++, most C++ software also uses C code (i.e. code that is compiled as C code)
Yeah, although I don't know if C++ has more tutorials or guides or whatever I guess the topic starter will need alot more help if he goes the c++ route anyway lol.
And also the most misused by the programmers.

newb16
Jan 22, 2009, 03:09 PM
But now when i have learned C++ a little but and started learning C

How could you learned c++ before c ? What features of c language (not standard library) you have not learned in c++?


I mean in C i have t o include stdio.h where in C++ i have to include iostream and i don't have to write .h in C++ but i do in C... weird..

and also i output with cout << in C++ but with printf in C..


Using c++ console i/o styles doesn't mean you write in c++, really. All operators, arithmetic, loops, etc are the same in c++. You can as well write c++ program and do not use iostream and have all i/o using (f)printf.

hankolerd
Jan 22, 2009, 03:33 PM
The first language I learned was java, and then C++, followed by assembly and C.

Every language is different and each has its pros and cons. Try to program in both C and C++. As you get more experience you will learn when one should be used over another, but knowing both is very valuable.

Don't worry about confusing the languages, it happens for a while but you will get over it. Good luck. :apple:

iAthena
Jan 22, 2009, 04:25 PM
The first language I learned was java, and then C++, followed by assembly and C.

Every language is different and each has its pros and cons. Try to program in both C and C++. As you get more experience you will learn when one should be used over another, but knowing both is very valuable.

Don't worry about confusing the languages, it happens for a while but you will get over it. Good luck. :apple:

Consider yourself fortunate you didn't have to go through assembly, Fortran and COBOL like some of us did before getting exposed to C. :)

cherry su
Jan 22, 2009, 04:52 PM
if you want C and/or C++ practice you should do problems from http://train.usaco.org/
it's a training site for a programming competition (USACO) and can help you play around and learn the language

Acorn
Jan 22, 2009, 05:51 PM
c++ is an extension to the c language. its not necessary to learn c before you learn c++. pretty much anything you can do in c you can do in c++. the c language allows you to do things you really shouldnt do. allowing you to shoot yourself in the foot more often. think of c++ like a stricter c with more features.

the confusion you are referring to is the standard library. there are some updated functions for c++. if you are learning c++ then use the updated functions. a decent c++ book usually explains this matter a little better. these are just routines to make your job easier. not language constructs. so dont let this slow you down.

just remember that just because a language offers you certain features like classes and friends it doesnt make it necessary to use them all. keep everything as simple and easy to read as possible. dont try and use every language feature.

i think you should learn c++ btw. best of luck.

lazydog
Jan 23, 2009, 11:29 AM
Let's be serious here C++ only has 3 advantages over C: it's more type safe, has a larger library and namespaces. (iostream IO is also much nicer than the stdio C crap).

Now as counter points to the previous poster:

C is more used than C++, most C++ software also uses C code (i.e. code that is compiled as C code)
Yeah, although I don't know if C++ has more tutorials or guides or whatever I guess the topic starter will need alot more help if he goes the c++ route anyway lol.
And also the most misused by the programmers.


You can only list 3 advantages?

b e n

MacRumors Guy
Jan 23, 2009, 11:43 AM
Help me find more...

LtRammstein
Jan 23, 2009, 11:51 AM
I have learned C, C++, and fairly good at both.

The one thing you have to understand about the two languages is that they are used for specific and usually completely different reasons.

C++ tends to be more used in developing OS based applications. While C tends to be used in more obscure areas like parallel computing and hardware programming. I'm not saying that C++ can't do what C does and vice versa.

I am an Electrical Engineering student, and from working with both, I find C to be a bit odd in the way you have to program it, but very straight forward. C++ tends to be the lazy programmer's language of choice because the compiler optimizes most of your code for you.

Here's the best way to explain the hierarchy of the languages:

C#/(Obj-C?)
|
C++/(Obj-C?)
|
C
|
Assembly (x86, MIPS, etc)

(I find it hard to place Obj-C, so if someone can explain where it goes, that would be great.)

If you want to program for a computer, you need C++, C# (specifically Windows) because C# takes a lot of C++'s concepts and Java concepts. Now if you want to program for the Mac, learning C would be better, since Obj-C is basically C with object based Framework and API, plus Obj-C can compile and run C functions, which can make some algorithms easier to write, etc. I work in mostly C and few other languages for programming Microprocessors/microcontrollers (right now I'm learning to distribute processing like Folding@Home and SETI@Home do) and FPGAs.

It really depends on what you want to do with the knowledge of the language. What I recommend is find an area you want to go into, (Hardware or Software), and follow suggestions of people who specify in those areas.

Besides, once you've mastered C, you can go onto Matlab. ;) (Hurray for internet memes!)

PS:

Here's what I find to C++ being better than C:

- Object-Oriented. This can make calculations and other things easier to work with.

- Works with most if any API found on really any platform. You can find C++ code on Linux, Unix, Windows, etc...

- C++ has far better compilers and IDEs than C. Mainly Visual Studio 2008, etc...

Here's what I find to C being better than C++:

- Data allocation is at the beginning of the program rather than anywhere you want.

- Works on any platform.

- Used in most cores for low level (hardware level) functionality.

- Less overhead on writing algorithms...

lee1210
Jan 23, 2009, 12:41 PM
I don't want to agree or disagree, just add some things to this:

<snip>
C++ tends to be the lazy programmer's language of choice because the compiler optimizes most of your code for you.

C compilers can optimize code VERY well, and since there's less overhead like exceptions, etc. i would argue that it is easier to optimize C code than C++.


<snip>
Here's the best way to explain the hierarchy of the languages:

C#/(Obj-C?)
|
C++/(Obj-C?)
|
C
|
Assembly (x86, MIPS, etc)

(I find it hard to place Obj-C, so if someone can explain where it goes, that would be great.)


C# is more, in my opinion, purely Object-Oriented than C++. It also has things one might expect from a "modern" OOP language, like Garbage collection. C++ lacks garbage collection (boost smart pointers, etc. are not the same, but they are nice), and Objective-C 1.0 did, but 2.0 has it, but not for the iPhone, so it's sort of in the middle. If you go purely by age, C++ is older than Objective-C 1.0 is older than C# is older than Objective-C 2.0 (though C# has many versions and has evolved as well... and C++ has as well, w/ C++ 0x, boost even though it is not all standard, etc.). If you assume the design of the languages have gotten better through time, C# is the newest, so it should be best, but as the others evolved they are probably closer now than ever.


<snip>
- Data allocation is at the beginning of the program rather than anywhere you want.


You can allocate memory whenever you'd like with malloc and friends. It would be very difficult to do a lot of things with only fixed stack storage.


<snip>
- Less overhead on writing algorithms...

This is just a nitpick. You write algorithms on paper, you implement them in code.

I will insist again, that this argument will see no victor (if that was possible, one of the languages would be dead by now). The OP should just pick one of these options for now, and stick with it. The other will not go out of style in the meantime, nor will the one that is chosen, so you can always go back and learn the other when you are ready.

Just pick one, and start coding. Ask a lot of questions. Read a lot of documentation. Good luck.

-Lee

Patillac
Jan 23, 2009, 02:24 PM
Just pick one, and start coding. Ask a lot of questions. Read a lot of documentation. Good luck.

-Lee

So far, this is the best advice I have seen :D

MooneyFlyer
Jan 23, 2009, 03:52 PM
And so it begins...

Languages aren't better than one another, but sometimes one language is a better tool for a particular job than another.

...

-Lee

Yep. That's it. I know a lot of crappy languages that implement their function better than other languages would/could.

FlashTheInfidel
Jan 24, 2009, 06:40 AM
So far, this is the best advice I have seen :D

I agree: don't confuse yourself when you don't have to. I know I argued against lee1210 in the other thread but he's right here.

There's also a heck of a lot of BS and incomplete information flying around in this thread - doesn't strike me as very helpful.

Personally, I'd suggest sticking with C for awhile as I'm guessing you want to work on Mac OS and it's useful to know at least the basics before moving on to Obj-C (which you'll probably need for Cocoa). It's useful to know the basics of C before you move onto C++ as well. I know plenty of people who make a nice living without moving from C at all though.

Nobita
Jan 24, 2009, 06:45 AM
I'm a C++ programmer for three years. Trust me it's got a steep learning curve, but C is worse! It's so much easier to get syntax wrong in C, in addition C doesn't have those library you can use to make your life easier (such as vector, stack, queues, strings, list, you name it.)

lazydog
Jan 26, 2009, 07:41 AM
Help me find more...

okay... how about exception handling, function overloading, operator overloading, templates… const, references, static/dynamic/const_cast... oh and let's not forget all the object orientated stuff. Even if you don't like or don't understand oop you can still make great use of parts of std, eg std::string etc. If you do that then you'll also appreciate the benefits of resources being deleted when they go out of scope. Oh and another benefit is the ability to compile standard C.

So if I was starting off, I would learn C++, starting with the basic C core and then expand out into the C++ 'parts' in small steps. Once you've learnt C, I'd say it's probably a good idea to catch up with the rest of the world and learn even just a little bit of oop.

b e n

trule
Jan 26, 2009, 08:07 AM
OK, So i have learned C++ a little bit..

What do you guys think about this situation..?



Do you have a reason to learn C or C++? Other than causing yourself pain? C and C++ are real engineering languages best used for solving real engineering problems.


Try to stick with Perl, PHP, Ruby...or if you must, Java.

QueenZ
Jan 26, 2009, 10:46 AM
Yes i do, i have to learn C++ at school... :)

Soulstorm
Jan 26, 2009, 12:02 PM
I won't elaborate on the matter much, because I am not very fond of threads that end up in a disagreement over what programming language is better.

C++ exists for a reason. So does C. If C++ was the truly better than C, then no one would program in C. If C was better than C++, then the need for developing C++ would not have existed, and there would be no C++.

As for some advice for the thread starter... Whatever you choose, you will surely need to learn the language's relative in order to do some things. For example, if you learn C, there is no way you will avoid learning at least some features of C++ in the future, and vise-versa.

lee1210
Jan 26, 2009, 12:07 PM
Yes i do, i have to learn C++ at school... :)

It sounds like we could have saved a lot of time if we knew that in the first place =). It sounds like the decision is made for you, so just focus on C++ now, you can always go back and learn C later.

-Lee

jw2002
Jan 29, 2009, 12:12 AM
So which language should i learn then? C or C++?

Most definitely C++ because it is better to learn the object oriented paradigm as soon as possible. Some people will pooh pooh C++ as being only marginally better than C (such as stricter type checking), but that's ignoring the tremendous potential and advantage of C++: it's an OO language.

Also, responding to part of the first post, just because you are using C++, it doesn't mean you have to use <iostream> for your I/O. Feel free to use <stdio> if you really have the need for both C and C++ modules to access the same file descriptors and keep I/O synchronized.

MacRumors Guy
Jan 29, 2009, 06:21 AM
okay... how about exception handling, function overloading, operator overloading, templates… const, references, static/dynamic/const_cast... oh and let's not forget all the object orientated stuff. Even if you don't like or don't understand oop you can still make great use of parts of std, eg std::string etc. If you do that then you'll also appreciate the benefits of resources being deleted when they go out of scope. Oh and another benefit is the ability to compile standard C.

Exception Handling is an issue of much debate. however I'll give you that.

Function overloading Not really needed, if the functions are different you just describe the diference in its name.
Operator overloading Dangerous specially when combine with the myriad of C++ "features".
Templates Hard to program correctly (past trivial stuff), hard to debug. Actually I had more success debugging data structures purely defined in macros than templates lol.
const C has const?
casts Never had problems with either casts (C and C++ casts).
object oriented stuff is this and advantage or a disadvantage?

stuarthatto
Jan 29, 2009, 06:47 AM
As a beginner, yes the exact syntax is what you care about now. But later what matters is how to break big problems up into small problems and those into smaller problems in such a way that the little problems become independent from each other.


Bingo! Little problems are easier to solve, code is more compact, more efficient, easier to debug, easier to read - and makes code RE-USABLE!

Took me ages to stop programming a problem top to bottom, branching, jumping, subroutining and so on.... but breaking a problem into its smallest parts is the secret with C++ and Java I guess

lazydog
Jan 29, 2009, 08:17 AM
Hi MacRumors Guy

I don't really understand what you are arguing about... the C++ features that you're dismissing are at the core of the things you like and mentioned in an earlier post!

b e n

lee1210
Jan 29, 2009, 08:38 AM
So after a week of back-and-forth, we've learned that some people prefer C++ and some prefer C, and both have problem domains that they are well suited to, and you won't be harmed by learning either first. We've also learned that you need to learn C++ for school, so that seems to make the decision pretty easy. I am sure we could keep this argument going for another few weeks, but at the end nobody's mind will be changed, and you will just have 100 differing opinions on this matter.

My point is that there is no right or wrong answer. So you don't learn OOP right away? That won't kill you. There are non-OOP languages you might want to use in the future, so knowing how to be effective without OO isn't going to hurt you. So you don't learn to manage memory as well using malloc/free right away? Big deal. It's useful, but you can learn it later. Just get coding. You will be fine.

-Lee

MacRumors Guy
Jan 29, 2009, 09:20 AM
Hi MacRumors Guy

I don't really understand what you are arguing about... the C++ features that you're dismissing are at the core of the things you like and mentioned in an earlier post!

b e n

Minus the fact that the std templates are already done!

Sander
Jan 29, 2009, 09:31 AM
Exception Handling is an issue of much debate. however I'll give you that.

Programming languages are an issue of much debate.

Also, I heard that screwdrivers are very dangerous if you poke yourself in the eye with them. I'll stick to hammers, thank you very much.

lazydog
Jan 29, 2009, 09:49 AM
Minus the fact that the std templates are already done!

Okay I see where you're coming from I think... using C++ as a souped up C. Sounds like a good path because you would be learning the core, C foundation anyway, yet be exposed to some of the harder aspects of C++ but in an easy accessible manner.

b e n

sheepopo39
Jan 29, 2009, 10:40 AM
I remember having that problem when i first started programming in those two languages

jw2002
Jan 30, 2009, 01:31 AM
Function overloading Not really needed, if the functions are different you just describe the diference in its name.
Operator overloading Dangerous specially when combine with the myriad of C++ "features".
Templates Hard to program correctly (past trivial stuff), hard to debug. Actually I had more success debugging data structures purely defined in macros than templates lol.


Gotta disagree on all these points.

For instance, function overloading is awesome because it decouples the interface from the implementation. A complex number in C++ can call a function named sin() which accepts a complex number and properly calculates the complex sin. This is provided through the standard template library. So from a scientific programming standpoint, C++ is superior because the formula written on the page can be transcribed into much more readable code. In C, complex numbers are a hot mess.

Operator overloading is not dangerous. Again, the disciplines of science and math use operator overloading in their very notation! We use the + and - sign for scalar, vector, and matrix addition. So C++ facilitates what has already existed in mathematical notation for centuries. I will agree that indiscriminate operator overloading can be inappropriate (such as making the + operator perform division), but that boils down to the same thing as giving a misleading name to a function.

Templates can be tricky, but if you find yourself writing a big one, then why not consult the standard template library first? You can probably incorporate one of those through inheritance, containment, or some design pattern and save yourself a ton of time from reinventing the wheel. Templates provide all the advantages of macros along with being type-safe. And no, macros are not easier to debug than templates. Good luck figuring out the error when that 25-line macro gets expanded into the middle of an if statement!

All of the above concepts can provide clear benefits even for non OO-aware programmers.

Furthermore, C++ has one more biggie over C: the reference. In C every pointer dereference "->" is a ticking time bomb. The reference in C++ allows all of the low level detailed modules to assume that they are dealing with objects directly and eliminates the need for pointer dereferencing. Thus any pointer bugs will rear their heads either at compile time or in a very high level module of the C++ code.

lazydog
Jan 30, 2009, 07:02 AM
Well elucidated jw2002!

b e n

MacRumors Guy
Jan 30, 2009, 01:28 PM
Gotta disagree on all these points.

For instance, function overloading is awesome because it decouples the interface from the implementation. A complex number in C++ can call a function named sin() which accepts a complex number and properly calculates the complex sin. This is provided through the standard template library. So from a scientific programming standpoint, C++ is superior because the formula written on the page can be transcribed into much more readable code. In C, complex numbers are a hot mess.


The situations where function overloading is usefull or recommended are so trivial that using a more descriptive function name instead is not that much of a deal. BTW: function overloading is also a major pain in the ass for C++ who do cross plattform development thanks to C++'s type coercion plus the different disambiguation rules each compiles uses.
C99 has support for complex numbers. The GNU complex library also supports complex numbers. For you is that much more painful to call csin instead of sin?


Operator overloading is not dangerous. Again, the disciplines of science and math use operator overloading in their very notation! We use the + and - sign for scalar, vector, and matrix addition. So C++ facilitates what has already existed in mathematical notation for centuries. I will agree that indiscriminate operator overloading can be inappropriate (such as making the + operator perform division), but that boils down to the same thing as giving a misleading name to a function.


Nice comparison except for a very subtle detail, in math those operators have no side effects!


Templates can be tricky, but if you find yourself writing a big one, then why not consult the standard template library first? You can probably incorporate one of those through inheritance, containment, or some design pattern and save yourself a ton of time from reinventing the wheel. Templates provide all the advantages of macros along with being type-safe. And no, macros are not easier to debug than templates. Good luck figuring out the error when that 25-line macro gets expanded into the middle of an if statement!


What if I can't? Do you think a C programmer reinvents the heel more often because they don't have the object oriented mondo jambo?
When you define a macro at least you know how it expands.

[Last post]