C and C++ It's so confusing..

Discussion in 'Mac Programming' started by QueenZ, Jan 22, 2009.

  1. macrumors 6502

    QueenZ

    Joined:
    Oct 26, 2008
    #1
    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..?
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  3. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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
     
  4. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #4
    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.
     
  5. thread starter macrumors 6502

    QueenZ

    Joined:
    Oct 26, 2008
    #5
    So which language should i learn then? C or C++?
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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.
     
  7. macrumors 6502

    Joined:
    Nov 5, 2007
    #7
    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.
     
  8. macrumors member

    Joined:
    Sep 17, 2008
  9. thread starter macrumors 6502

    QueenZ

    Joined:
    Oct 26, 2008
    #9
    Bad?? Tell me at least 10 reasons why it's bad
     
  10. macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
  11. macrumors regular

    MacRohde

    Joined:
    Jun 1, 2004
    Location:
    Copenhagen, Denmark
    #11
    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
     
  12. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #12
    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
     
  13. macrumors member

    Joined:
    Sep 17, 2008
    #13
    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.
     
  14. macrumors regular

    Joined:
    Feb 27, 2008
    #14
    How could you learned c++ before c ? What features of c language (not standard library) you have not learned 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.
     
  15. macrumors 6502

    hankolerd

    Joined:
    Sep 19, 2007
    Location:
    Seattle, WA
    #15
    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:
     
  16. macrumors regular

    Joined:
    Jan 22, 2008
    Location:
    USA
    #16
    Consider yourself fortunate you didn't have to go through assembly, Fortran and COBOL like some of us did before getting exposed to C. :)
     
  17. macrumors 65816

    cherry su

    Joined:
    Feb 28, 2008
    #17
    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
     
  18. macrumors 68020

    Acorn

    Joined:
    Jan 2, 2009
    Location:
    macrumors
    #18
    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.
     
  19. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #19
    You can only list 3 advantages?

    b e n
     
  20. macrumors member

    Joined:
    Sep 17, 2008
  21. macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Location:
    Denver, CO
    #21
    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...
     
  22. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #22
    I don't want to agree or disagree, just add some things to this:

    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++.

    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.

    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.

    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
     
  23. macrumors member

    Patillac

    Joined:
    Mar 26, 2008
    Location:
    Golden State
    #23
    So far, this is the best advice I have seen :D
     
  24. macrumors 65816

    MooneyFlyer

    Joined:
    Nov 18, 2007
    Location:
    Boston
    #24
    Yep. That's it. I know a lot of crappy languages that implement their function better than other languages would/could.
     
  25. macrumors newbie

    Joined:
    Feb 13, 2007
    #25
    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.
     

Share This Page