Skip to content
asoplata edited this page Jul 29, 2013 · 7 revisions

(For C and C++ Best Practices, see this page)

The [C](https://en.wikipedia.org/wiki/C_(programming_language) and C++ languages are two of the most popular general-purpose programming languages in the world and some of the oldest still used heavily. Much of systems-level programming, embedded life-critical programming, and programming where efficiency and speed are desired (as in scientific programming for simulation) are written in C and C++. For example, the Linux kernel is written entirely in C, and the Windows operating system is written in C, C++, and C#. However, both can be used for much simpler sub-enterprise-level programs for the desktop, the command line, network connections, you name it. They are "compiled" languages (as opposed to "interpreted" like Python), so each time you make a change to the core code, you must compile the code into a binary executable file, the running of which is then "running the program". C++ is technically a superset of C, but use and best practices of each of them have diverged so much that they are considered practically different languages; for example, some things that are best practices in C are worst practices in C++ due to the difference in their standard libraries.

Advantages of C/C++ for scientific programming

  • The languages and all the major compilers used today are free.
  • If you have a machine today that can be called "a computer" in the colloquial way, then you can develop C or C++ on it. This includes desktops, laptops, servers, netbooks, etc. no matter the operating system (Windows, Mac, Linux).
  • There are lots of both old and reliable, or new and fancy, tools and Integrated Development Environments (IDE) for developing C/C++ programs. Today, almost everyone uses free tools - you will never have to pay to code and compile in C/C++, or even get an institutional license like you would for MATLAB.
  • They are some of the oldest languages still in heavy use. You may think "If it's old, isn't it deprecated?" but contrary to that, its legacy is a benefit! :
    • They both have collossal library support; chances are, if you're trying to do something simple like implement multidimensional arrays, that problem has not only been solved (and the solution is sitting on Stack Overflow) but maybe even optimized. This includes libraries for scientific programming, like Fast Fourier Transforms, signal analysis, etc.
    • They're one of the few cases where they're still around due to the usefulness of their core DESIGN, as opposed to getting locked-in by a language before everyone realized how bad the language was. New projects are started in C/C++ almost every day, still.
    • Many people "speak" these languages. While they're not as human readable as Python, so many people know and write C/C++ that others can often understand your code.
    • Their best practices are well-defined after many years of use by many people, and the books that teach them the best have, in general, stood the test of time. Additionally, there are TONS of free online resources for learning the languages going back decades; for some, see the bottom of the page.
  • They are still used as benchmarks for speed tests with new languages, and offer some of the best computer efficiency and speed per development time around (FORTRAN is still used for extreme/supercomputer-requiring efficiency and speed requirements, but lacks much of both the ease of use in C and abstraction in C++).
    • Their effiency per development time comes from the mixture of low-level memory management (powerful pointers to arrays of data objects that are contiguous in computer memory), compile-time static typing of data (except for C++ templates), and high-level abstraction such as custom data types in both and very powerful Object-oriented Programming (OOP) in C++.
  • In fact, in the required learning about how to use memory with C/C++, you will have to learn more about how computers handle memory! This low-level understanding of memory will then be applicable to what is "really going on" in other languages, and enable you to write more efficient and faster programs across the board in other languages.
  • They work well with other languages: Python, for example, is written from C, and can directly call C-compiled functions and code.
  • They are very widely used outside of scientific programming, and so non-scientist programmers can help you solve coding problems...or even offer you a job coding C/C++ if you decide science isn't for you!

Disadvantages of C/C++ for sci programming

  • Neither are very easily human readable, as opposed to Python. It can be very difficult sometimes to figure out what someone else's (or even your own old code!) is doing if it is insufficiently clear or explained. Thus, formatting and good style are even more important than in other common languages.
  • Some uses of pointers, for example several-pointers-deep math, are notorious for occasionally baffling even the most seasoned programmer, and lack of care when using pointers can actually damage your computer!
  • There is no automated garbage collection, as anything you create on the heap must be explicitly destroyed by yourself (this is where Valgrind comes in handy).
  • C is not difficult to learn, as there are many tutorials for people from all levels of computer understanding (including those who have NO IDEA where to begin, like here), but greater than average understanding of how computers work (which is almost always covered in anything that teaches C) is needed to really use C to its full extent. It is harder to "get going" in C than MATLAB or Python, and possibly even C++.
  • C++ is a VERY large language. This means C++ is great for large-scale, many-programmers, enterprise-level code, but that there is most likely a large difference between what a software engineer would need to learn about C++ versus what someone who just wants to do scientific programming would require. In fact, it is possible that the best scientific programming usage of C++ for certain applications can infringe on "C-style C++", wherein you only use the C++ you need but organize code or do calculations in a simple C style way. This is considered bad practice in software engineering but may be acceptable use in scientific programming, thus diverging how you write code from the skills a programming employer would look for.
    • Regardless of how much C++ you want to actually implement, C++ is difficult to learn to the point where doing simple tutorials like "learn C++ in 10 days" will not teach you to use C++ as it was designed; you really need to invest in one of the good tomes listed at the Stack Overflow thread on the bottom like Accelerated C++ or The C++ Primer. Unless you really really don't care about best practices (and shame on you) and just want enough C++ to get by, you should make a concerted, months-long effort (a little bit at a time) to learn C++ using a well-known good book.
    • Relatedly, because C++ is so large and usage is so spread out, there are actually BAD manuals and tutorials that teach faulty practices. It has been claimed that the C++ Primer Plus series, not to be confused with the C++ Primer series, is negligent in these aspects; this is hearsay, though, so try to look for crowdsourced reviews of different texts, like this best-thread-on-the-internet: The Definitive C++ Book Guide and List. Look at the "Beginner" section of that. See also the list at the end of this page.
  • While C/C++ are great for efficient calculation, for scientific plotting and visualization of data you might as well not even try. Even if C/C++ offerings were adequate in that regard, they would still be greatly inferior to the capability and ease of use of MATLAB's plotting or the "matplotlib" module of Python. Thankfully though, it is not difficult to use C/C++ for simulation, and then have Python/whathaveyou plot the results later in the toolchain; this combination of different languages is very common in science.

C vs. C++

C:

  • Pros:
    • Much easier to learn en totale (for instance, one of the bibles of the language, The C Programming Language, clocks in at 274 pages and is still used), and takes far less time to learn.
    • Since it is less complicated, there is less to get wrong, and thus tutorials are less likely to be incorrect.
    • It is the best at interfacing with Unix/Linux system calls.
    • It works greatly with Python (though C++ can do this to a certain extent now), being the core language of Python.
    • It is very lightweight, and is usually what's chosen for embedded systems or small devices, like Arduinos, which are increasingly popular in science.
  • Cons:
    • Does NOT include OOP, i.e. classes, class methods, inheritance, polymorphism, operator overloading, etc. This enforces simplicity in code organization, but there are many instances in scientific programming where just using OOP simplifies things greatly. C does, however, include structures, or "structs", and therefore custom data types. Thus, in scientific programming it is almost always used alongside a higher-level language.

C++:

  • Pros:
    • Has complete capabilities for OOP, template meta-programming, exception handling, etc. It is capable of far greater abstraction than C.
    • It is easier to make much larger, collaborative programs with C++, e.g. one person designs a class and many other programmers use it. That's not to say C can't do large programs- the Linux kernel is a counterexample.
    • There are some parts of C++ and its standard library, e.g. I/O streams, that are considered by some to be better than their equivalent in C.
  • Cons:
    • There is a TON to learn about the language, and you won't know whether the language already has some specific functionality that's what you need unless you're familiar with much of the language landscape. A good introductory C++ book can easily clock in at at least 800 pages (that's not to say there aren't good shorter books).
    • In spite of that, unless you're a professional programmer for 20 years, you will likely not use 90% of what C++ has to offer.
    • C++ is not quite as ubiquitous in terms of being able to develop on different machines as C, but only to a small extent.
    • Many people think C++ was designed to do too many things, is too complicated, and is not well-designed (e.g. Linus Torvalds, creator of Linux, has a rant (NSFW language) on why he thinks C is far better than C++), whereas no one makes the same complaints about C.
    • There are so many books written on how to use it that a substantial fraction of them either use poor practices or sometimes are outright incorrect.

External Resources

C

Books for learning C, and as references for C

The end-all, be-all list for C: http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list

One of the best: The C Programming Language

Tutorials on C

http://www.computerscienceforeveryone.com/ (This is the best if you have NO programming experience)

http://c.learncodethehardway.org/book/

C++

Books for learning C++, and as references for C++

It is highly recommended that C++ be learned through reliable books and not brief tutorials for reasons stated above.

The end-all, be-all list for C++: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

One of the best: C++ Primer, 5th edition

C++ for scientific programming

http://wbell.web.cern.ch/wbell/HepCppIntro/HepCppIntroGuide-2009-06-03.pdf

http://www.cs.indiana.edu/pub/techreports/TR542.pdf

The writer of this does in no way claim to be a C/C++ expert.