Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

QueenZ

macrumors 6502
Original poster
Oct 26, 2008
284
0
OK, So i have learned C++ a little bit..

Code:
// 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..

Code:
// 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..?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
OK, So i have learned C++ a little bit..

Code:
// 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..

Code:
// 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

macrumors G5
Jan 5, 2006
12,919
2,173
Redondo Beach, California
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors 6502
Nov 5, 2007
436
0
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.
 

MacRohde

macrumors regular
Jun 1, 2004
164
0
Copenhagen, Denmark
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors member
Sep 17, 2008
82
0
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

macrumors regular
Feb 27, 2008
100
0
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

macrumors 6502
Sep 19, 2007
353
6
Seattle, WA
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

macrumors regular
Jan 22, 2008
109
0
USA
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

macrumors 65816
Feb 28, 2008
1,217
1
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

macrumors 68030
Jan 2, 2009
2,643
352
macrumors
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

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

LtRammstein

macrumors 6502a
Jun 20, 2006
570
0
Denver, CO
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

MooneyFlyer

macrumors 65816
Nov 18, 2007
1,484
0
Boston
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

macrumors newbie
Feb 13, 2007
8
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.