C

From Alliance Doc
Revision as of 20:28, 17 November 2016 by Preney (talk | contribs) (Created page with "=C= C is a general-purpose, high-level, imperative programming language created by Dennis Ritchie between 1969 and 1973 at Bell Labs. C is now represented as a number of ISO...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

C[edit]

C is a general-purpose, high-level, imperative programming language created by Dennis Ritchie between 1969 and 1973 at Bell Labs. C is now represented as a number of ISO standards corresponding to the years 1989/1990, 1999, and 2011 and are usually referred to as C90 (or C89), C99, and C11. If you are new to C or wish to read an overview of the language and/or how each ISO standard impacted it, check out the following Wikipedia links:

  1. C, i.e., the language, history, C90.
  2. C99: Adds language and Standard Library features. int is no longer implicitly assumed.
  3. C11: Major release adding memory model and concurrency (e.g., threads, atomics, compare-and-swap) support.

Should you have a need to refer to the actual ISO standard document for C, you can obtain a link to the last draft (which may well have errors in it!) before each ISO C standard release in the aforementioned Wikipedia pages' reference sections. (If you need the official document, you may purchase it from Standards Council of Canada.)

Well-Defined Concurrency and Memory Models[edit]

It is important to realize that prior to 2011 ISO C and ISO C++ standards these languages had no definitions of concurrency and memory models. In pre-C11 and pre-C++11 code, while most of the time the "right" thing is done, understand there are no guarantees in the language specifications (and therefore compiler implementations of such) of the ordering of reads and writes under concurrency.

Prior to C11 and C++11, one would have used the pthreads / Windows threading APIs to implement concurrency. Although many compilers will implement C11 and C++11 threads, etc. in terms of pthreads / Windows threading APIs, using the ISO C++11 and ISO C11 definitions is better for two reasons:

  • the code is compiler and platform independent, and,
  • C11 and C++11 have well-defined memory models --continuing to compile code using pre-C11 and/or pre-C++11 compiler language flags implies that no memory models are formally in effect and that code is likely relying on undefined behaviour(s).

Pitfalls[edit]

The volatile Keyword[edit]

The reader should note that volatile in C and C++ has a very specific meaning, e.g., see this page. Actually needing to use volatile in C/C++ code is a rare event and it is typically limited to certain kinds of low-level code.

Misuse of volatile might arise because the Java programming language uses the volatile keyword as well. Java's volatile has a totally different meaning from C's volatile. Specifically, Java's volatile keyword in C corresponds to using atomic_* (i.e., where '*' corresponds to a fundamental type name such as int).

Compilers[edit]

GCC[edit]

The GCC compiler's -O3 option includes possibly unsafe optimizations for some types of code (e.g., code relying on aliasing). If unsure, compile and optimize code using the -O2 option instead. If you've more time, read the man page (e.g., man gcc) and unset the appropriate options by searching for "-O3" to see which options are turned on and turn off the settings that are not safe.

Intel[edit]

Intel C/C++ compilers may default to using possibly unsafe optimizations for floating-point operations. Users using the Intel compilers should read the Intel man pages (e.g., man icc) and are recommended to use one of two options, -fp-model precise or -fp-model source, for ANSI/ISO/IEEE standards-compliant floating-point support.