Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C or C++?

  1. Sep 12, 2010 #1
    Hi there, I am a third (maybe second year for US standard?) year mathematical physics student and I have just realize that I do need obtain some programming knowledge.
    My university has a compulsory courses that teaches java programming, but, looking around I've come to the conclusion that probably getting some knowledge in languages like C(s) could be preferable.

    Here comes the "problem": should I begin with C++ or with C? I know they are very different, but at the same time they look very similar (all I know of programming in java and c++ goes just to the very beginning of OOD, and I just studied the very basics of syntax in C).

    So, if anyone could give me some "clear" example of the possible application of this two programs (c and c++) in the scientific world, that would be perfect!


    Apologies for any English mistake...^_^
     
  2. jcsd
  3. Sep 12, 2010 #2

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    C++ is a more versatile extension of C. Most schools are dispensing with C and starting with C++.
     
  4. Sep 12, 2010 #3

    jtbell

    User Avatar

    Staff: Mentor

    C++ is basically C that has been extended some ways:

    1. the addition of features to support object-oriented programming (i.e. classes). However, one is not forced to write programs in object-oriented style from the beginning, as in Java.

    2. the addition of "C++-style" ways to do certain things like input/output, character strings, and other collections of data. For example, if your program needs to work with a list of numbers, in C you would use an array; in C++ you can use either a "C-style" array or a "C++-style" vector. In my opinion, the C++-style ways are usually easier to use and less prone to errors, for beginning programmers.

    So my own preference would be to start by learning C++ for simple non-object-oriented programming, taking advantage of "C++-style" I/O, strings, vectors, etc. Then, depending on your needs, learn the "C-style" ways of doing things (so you can work with C programmers, or on already-existing C code), and/or object-oriented programming (i.e. learn to design your own "classes" of objects).
     
  5. Sep 12, 2010 #4

    phyzguy

    User Avatar
    Science Advisor

    Since most C programs compile with a C++ compiler, C can be considered basically a subset of C++. What I did was learn C first, then once I was comfortable with C, I added the C++ object-oriented concepts. This seemed to work well.
     
  6. Sep 12, 2010 #5

    jtbell

    User Avatar

    Staff: Mentor

    Another difference between learning programming by using "pure" C++ versus "C-style" C++ or "pure" C:

    When learning programming using C or C-style C++, you have to come to grips with pointers very early on. You need to understand pointers in order to do basic I/O (printf(), etc.), use character strings (char*), use arrays, and pass data to and from functions.

    As most programmers are well aware from personal experience, pointers are one of the most difficult concepts for beginning programmers.

    In pure C++ you don't need to use pointers for any of those things, which in my opinion simplifies learning those things. You can focus on the general principles of input and output, array-like data collections (vectors), etc. without having to worry about the low-level "nuts and bolts" of raw pointers.

    If you get far enough into programming, at some point you do have to learn about pointers in order to accomplish what you want, and that is the appropriate time to do it, in my opinion.

    I used to teach a two-semester introductory programming course that used C++. After I started to use the standard library features like the 'string' data type and vectors (which were new in 1998), I found it very difficult to come up with meaningful examples to motivate learning how to use raw pointers, until I got to data-structures topics like dynamic memory allocation ('new' and 'delete') and linked lists.

    And even those are unnecessary! The C++ standard library has "containers" that provide the functionality of linked lists, trees, etc. without having to use pointers. (You do have to use "iterators" which are sort of like pointers.)
     
    Last edited: Sep 12, 2010
  7. Sep 12, 2010 #6
    jtbell...you convinced me! Thank you very much for your explanations, very needed indeed!
     
  8. Sep 12, 2010 #7

    jtbell

    User Avatar

    Staff: Mentor

    Unfortunately, I haven't taught a full-length C++ course in several years, so I don't know which (if any!) of the common textbooks use a "postpone pointers until they're really necessary" approach. The book that I used then was the only one like it that I knew of, and it's out of print now, and it wouldn't fit well with currently-available compilers anyway. Many of the C++ books that I saw back then looked like C books that had been rewritten minimally to turn them into C++ books, still using char* for strings, C-style arrays instead of vectors, etc.

    I like the book https://www.amazon.com/Accelerated-...=sr_1_1?s=books&ie=UTF8&qid=1284322071&sr=1-1 by Koenig and Moo, as an "idiomatic" presentation of C++, but I hesitate to recommend it as the only textbook for someone who's new to programming. It's rather short and fast-paced. It has a nice style and is very well written, but it doesn't doesn't have lots of diagrams and worked-out examples and "self-check" questions and stuff like that. Maybe as a supplement to a more traditional textbook.

    Here's another possibility: https://www.amazon.com/Primer-4th-S...=sr_1_1?s=books&ie=UTF8&qid=1284322805&sr=1-1 by Lippman, Lajoie and Moo. I have the second or third edition, but this one looks like it's significantly different so I'd better not say anything. Check out the Amazon reviews, ignoring the ones written before 2005 when this edition appeared.
     
    Last edited by a moderator: May 4, 2017
  9. Sep 12, 2010 #8
    Tomorrow I'll go to the uni library and I'll see what it can offer.
    I quite like the idea of the Accelerated C++, as I am already a bit familiar with some of the very basics (variables, control statements, functions..).
    But, again, hopefully the university library will be able to help me :)
     
  10. Sep 12, 2010 #9
    I always liked the C++ primer, but you will probably find it a bit slow. Jt is correct about most of the books, they are simply C books retitled and added bits and there.

    The real issue is that on Windows C++ is really Microsoft dominated and it is all geared towards the Windows apps and not teaching the core fundamentals of the language. So when you look for books there are 1000 titles on Wiindows C++ programming.

    I know I throw this website out alot, and it is because it is good for programming specific issues. DevShed. It might be helpful once you get learning.

    The GNU C++ compiler is availble for windows.

    And as far as c or c++. Part of the C++ standard is that it should compile c files, as c files. Or there should be a compiler option that supresses certain extentions.

    the real difference is the standard libraries. In C++ there are IOStreams and other class libraries that encapsulate the C standard libraries. If you avoid class libraries, and stick to the C standard libraries. you can write ANSI C code that will compile under any C or C++ compiler.

    The most important thing is that you understand memory, pointers, and structures. That is when you move away from the basic stuff.

    Have fun with it
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C or C++?
  1. To C or not to C (Replies: 31)

  2. C++ and C++ Standard (Replies: 3)

  3. Java or C/C ++ (Replies: 5)

Loading...