4

I've been exposed to numerous coding guidelines, every one of them giving guidelines for naming 'things'. One specific C89 naming rule triggered this question: It was explicitly forbidden to use the name 'new' for a variable, because in C++ this is a keyword.

This started me thinking about the next standards, C99 is well documented, but C11 is relatively new (2014). And the Standards committee has several extensions still in the works.

Therefore I'm wondering if someone can provide a list with keywords and their origins, in order to make a set of naming rules, including the reasons why several names are to be avoided.

And although strictly spoken complex is not a keyword, similar definitions would best be included. And C++ information is also appreciated.

Johan Bezem
  • 2,582
  • 1
  • 20
  • 47
  • 1
    You should give coding standards little weight. There are so many of them, if you followed half of them you'd never be able finish a project. – Dietrich Epp Nov 15 '11 at 16:40
  • Might be true, but being a consultant I get asked sometimes (and most of the time I have to follow one as well). I'd rather give well founded advice, than giving them a set of rules without reasons. I've seen software using `new`, `complex`, `decimal`, and in the light of the next set of standards, or porting to C++, I would love to have such a list. – Johan Bezem Nov 15 '11 at 16:45
  • 3
    The keywords are listed in the standards. What extra do you want to know? – David Heffernan Nov 15 '11 at 16:47
  • In C++ all the types/objects are in the namespace std:: so will not conflict with your types names (such as complex). So all you have to worry about is keywords. Which are defined in the standard http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents/4653479#4653479 – Martin York Nov 15 '11 at 16:52
  • 2
    K&R1 had a keyword `entry`, TR19769 is defining new character data types, `volatile` was introduced (IIRC) in C94, C1X adds loads of stuff. Maybe it doesn't exist, but I'd be interested in having all this available in one location. Or would you know to avoid a variable name `decimal128`? – Johan Bezem Nov 15 '11 at 16:53
  • @LokiAstari I'm in embedded, and on occasion I do not have access to a modern compiler. And a consistent definition and implementation of namespaces isn't quite that old, is it? – Johan Bezem Nov 15 '11 at 16:55
  • @LokiAstari: Some popular C++ compilers put the names defined in the C library in the global namespace, as well as `std`. Also, some of the C library names are macros, and stomp on all namespaces. – Mike Seymour Nov 15 '11 at 17:05
  • I believe all the new keywords in C11 are "reserved" names (beginning with an underscore and a capital letter), as some were in C99, so you'll only get conflicts with non-evil existing code if you include the new header files. – Mike Seymour Nov 15 '11 at 17:11
  • @Mike Seymour: Yes they can put the **C** stuff in the global namespace but the C++ stuff is in std:: . Also when you are programming in C you are already aware of the C stuff so no extra care is required. – Martin York Nov 15 '11 at 19:08
  • @Johan Bezem: Sure some older compilers (and embedded ones) do not support namespaces. But it has been standard since 03 (so nearly 10 years). – Martin York Nov 15 '11 at 19:09
  • @LokiAstari ... and being in automotive development for 20+ years, I still am required to use mostly pure C89, which was brand new when I started professionally, and now more than 20 years old. The same goes for C++, I sometimes need to use pre-namespace, pre-standard C++ compilers for legacy projects that somehow managed to survive longer than anyone expected. – Johan Bezem Nov 29 '11 at 21:33

4 Answers4

6

From n3242 (ie C++11)

2.12 Keywords

Table 4 — Keywords

alignas  alignof          asm           auto         bool
break    case             catch         char         char16_t
char32_t class            const         constexpr    const_cast 
continue decltype         default       delete       double 
do       dynamic_cast     else          enum         explicit 
export   extern           false         float        for 
friend   goto             if            inline       int 
long     mutable          namespace     new          noexcept 
nullptr  operator         private       protected    public 
register reinterpret_cast return        short        signed 
sizeof   static           static_assert static_cast  struct 
switch   template         this          thread_local throw 
true     try              typedef       typeid       typename 
union    unsigned         using         virtual      void
volatile wchar_t          while

Table 5 — Alternative representations

and      and_eq           bitand        bitor       compl
not      not_eq           or            or_eq       xor
xor_eq

From n1256 (C99)

A.1.2 Keywords

auto     enum             break         extern      case
float    char             for           const       goto
continue if               default       inline      do
int      double           long          else        register
restrict return           short         signed      sizeof
static   struct           switch        typedef     union
unsigned void             volatile      while       _Bool
_Complex _Imaginary
Martin York
  • 257,169
  • 86
  • 333
  • 562
2
  • and
  • and_eq
  • alignas
  • alignof
  • asm
  • auto
  • bitand
  • bitor
  • bool
  • break
  • case
  • catch
  • char
  • char16_t
  • char32_t
  • class
  • compl
  • const
  • constexpr
  • const_cast
  • continue
  • decltype
  • default
  • delete
  • double
  • dynamic_cast
  • else
  • enum
  • explicit
  • export
  • extern
  • false
  • float
  • for
  • friend
  • goto
  • if
  • inline
  • int
  • long
  • mutable
  • namespace
  • new
  • noexcept
  • not
  • not_eq
  • nullptr
  • operator
  • or
  • or_eq
  • private
  • protected
  • public
  • register
  • reinterpret_cast
  • return
  • short
  • signed
  • sizeof
  • static
  • static_assert
  • static_cast
  • struct
  • switch
  • template
  • this
  • thread_local
  • throw
  • true
  • try
  • typedef
  • typeid
  • typename
  • union
  • unsigned
  • using
  • virtual
  • void
  • volatile
  • wchar_t
  • while
  • xor
  • xor_eq

from cppreference.com

  • auto
  • _Bool
  • break
  • case
  • char
  • _Complex
  • const
  • continue
  • default
  • do
  • double
  • else
  • enum
  • extern
  • float
  • for
  • goto
  • if
  • _Imaginary
  • inline
  • int
  • long
  • register
  • restrict
  • return
  • short
  • signed
  • sizeof
  • static
  • struct
  • switch
  • typedef
  • union
  • unsigned
  • void
  • volatile
  • while

from wikipedia

Formatting this was fun.

Tom Kerr
  • 10,444
  • 2
  • 30
  • 46
0

C11 has new key words. Some of them are _Alignas, _Alignof. You can find the complete list at http://www.sdlczone.com/complete-list-of-c-keywords/

0

Thanks for all the answers and comments, but simply having a list of keywords was not quite the intention of my question. Especially,

... and their origins ...

has apparently been overlooked. But when drafting a coding guideline, you're always required to give a rationale for each restriction.

That's why I've started to collect my own data, and put it in a document. I'm still working on it, especially the AM1 amendment to the C90 Standard in 1995 is not fully included, for C++ only the latest C++11 Standard has been used (partly), several Technical Reports still need inclusion, etc. But since I'm currently not pursuing many updates because of workload, I've put a draft-version for download on my website.

If you're interested, look here: Reserved Words In C (pdf). Additions and corrections are always welcome, contact info on my website.

FWIW.

Johan Bezem
  • 2,582
  • 1
  • 20
  • 47
  • Since there are no more additions since more than a year, and my document has not been commented on in any way, I give myself the mark for the best answer. If this is not seemly, let me know. – Johan Bezem Nov 29 '12 at 16:57