Should I Learn C or C++ for Scientific Applications?

In summary: I would recommend starting with a book that uses more "traditional" programming approach, like "C++ Primer" by Bjarne Stroustrup.
  • #1
noxf
8
0
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...^_^
 
Technology news on Phys.org
  • #2
C++ is a more versatile extension of C. Most schools are dispensing with C and starting with C++.
 
  • #3
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).
 
  • #4
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.
 
  • #5
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:
  • #6
jtbell...you convinced me! Thank you very much for your explanations, very needed indeed!
 
  • #7
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/dp/020170353X/?tag=pfamazon01-20 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/dp/0201721481/?tag=pfamazon01-20 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:
  • #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 :)
 
  • #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
 

1. Should I learn C or C++ for scientific applications?

Both C and C++ are commonly used in scientific applications, so it ultimately depends on your specific needs and preferences. However, if you are just starting out, it may be more beneficial to learn C++ as it is a more modern and versatile language with built-in object-oriented programming capabilities.

2. What are the advantages of using C for scientific applications?

C is a low-level language that allows for more efficient memory management and faster execution, making it a good choice for computationally intensive scientific applications. It also has a large library of mathematical and scientific functions that can be easily integrated into programs.

3. What are the advantages of using C++ for scientific applications?

C++ has all the advantages of C, but with the added benefit of object-oriented programming. This allows for more organized and modular code, making it easier to handle complex scientific applications. C++ also has a robust standard template library that includes data structures and algorithms commonly used in scientific computing.

4. Can I use both C and C++ in the same scientific application?

Yes, it is possible to use both C and C++ in the same application. C++ is compatible with C code, so you can easily incorporate existing C code into a C++ project. This can be useful if you have a large library of C functions that you want to use in a C++ program.

5. Are there any other languages that are commonly used in scientific applications?

Yes, there are several other languages that are commonly used in scientific computing, such as Python, Java, and Fortran. These languages have their own strengths and weaknesses, so it is important to research and choose the best language for your specific application.

Similar threads

  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
4
Replies
107
Views
5K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
878
  • Programming and Computer Science
2
Replies
69
Views
4K
  • Programming and Computer Science
Replies
1
Views
686
  • Programming and Computer Science
Replies
2
Views
1K
Replies
3
Views
333
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top