1.7 — Keywords and naming identifiers


C++ reserves a set of 84 words (as of C++17) for its own use. These words are called keywords (or reserved words), and each of these keywords has a special meaning within the C++ language.

Here is a list of all the C++ keywords (through C++17):

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

The keywords marked (C++11) were added in C++11. If your compiler is not C++11 compliant (or does have C++11 functionality, but it’s turned off by default), these keywords may not be functional.

C++11 also adds two special identifiers: override and final. These have a specific meaning when used in certain contexts but are not reserved.

You have already run across some of these keywords, including int and return. Along with a set of operators, these keywords and special identifiers define the entire language of C++ (preprocessor commands excluded). Because keywords and special identifiers have special meaning, your IDEs will likely change the text color of these words (often to blue) to make them stand out from other identifiers.

By the time you are done with this tutorial series, you will understand what almost all of these words do!

Identifier naming rules

As a reminder, the name of a variable (or function, type, or other kind of item) is called an identifier. C++ gives you a lot of flexibility to name identifiers as you wish. However, there are a few rules that must be followed when naming identifiers:

  • The identifier can not be a keyword. Keywords are reserved.
  • The identifier can only be composed of letters (lower or upper case), numbers, and the underscore character. That means the name can not contain symbols (except the underscore) nor whitespace (spaces or tabs).
  • The identifier must begin with a letter (lower or upper case) or an underscore. It can not start with a number.
  • C++ is case sensitive, and thus distinguishes between lower and upper case letters. nvalue is different than nValue is different than NVALUE.

Identifier naming best practices

Now that you know how you can name a variable, let’s talk about how you should name a variable (or function).

First, it is a convention in C++ that variable names should begin with a lowercase letter. If the variable name is one word, the whole thing should be written in lowercase letters.

Most often, function names are also started with a lowercase letter (though there’s some disagreement on this point). We’ll follow this convention, since function main (which all programs must have) starts with a lowercase letter, as do all of the functions in the C++ standard library.

Identifier names that start with a capital letter are typically used for user-defined types (such as structs, classes, and enumerations, all of which we will cover later).

If the variable or function name is multi-word, there are two common conventions: words separated by underscores, called snake_case, or intercapped (sometimes called camelCase, since the capital letters stick up like the humps on a camel).

In this tutorial, we will typically use the intercapped approach because it’s easier to read (it’s easy to mistake an underscore for a space in dense blocks of code). But it’s common to see either -- the C++ standard library uses the underscore method for both variables and functions. Sometimes you’ll see a mix of the two: underscores used for variables and intercaps used for functions.

It’s worth noting that if you’re working in someone else’s code, it’s generally considered better to match the style of the code you are working in than to rigidly follow the naming conventions laid out above.

Best practice

When working in an existing program, use the conventions of that program (even if they don’t conform to modern best practices). Use modern best practices when you’re writing new programs.

Second, you should avoid naming your identifiers starting with an underscore, as these names are typically reserved for OS, library, and/or compiler use.

Third, your identifiers should make clear what the value they are holding means (particularly if the units isn’t obvious). Identifiers should be named in a way that would help someone who has no idea what your code does be able to figure it out as quickly as possible. In 3 months, when you look at your program again, you’ll have forgotten how it works, and you’ll thank yourself for picking variable names that make sense.

However, giving a trivial variable an overly complex name impedes overall understanding of what the program is doing almost as much as giving a widely used identifier an inadequate name. Therefore, a good rule of thumb is to make the length of a identifier proportional to how widely it is used. An identifier with a trivial use can have a short name (e.g. such as i). An identifier that is used more broadly (e.g. a function that is called from many different places in a program) should have a longer and more descriptive name (e.g. instead of open, try openFileOnDisk).

int ccount Bad What does the c before “count” stand for?
int customerCount Good Clear what we’re counting
int i Either Okay if use is trivial, bad otherwise
int index Either Okay if obvious what we’re indexing
int totalScore Either Okay if there’s only one thing being scored, otherwise too ambiguous
int _count Bad Do not start names with underscore
int count Either Okay if obvious what we’re counting
int data Bad What kind of data?
int time Bad Is this in seconds, minutes, or hours?
int minutesElapsed Good Descriptive
int value1, value2 Either Can be hard to differentiate between the two
int numApples Good Descriptive
int monstersKilled Good Descriptive
int x, y Either Okay if use is trivial, bad otherwise

In any case, avoid abbreviations. Although they reduce the time you need to write your code, they make your code harder to read. Even if the abbreviation is unambiguous, it takes the reader a moment to figure out what you meant. Code is read more often than it is written, the time you saved while writing the code is time that every reader, including the future you, wastes when reading it. If you’re looking to write code faster, use your editor’s auto-complete feature.

Finally, a clarifying comment can go a long way. For example, say we’ve declared a variable named numberOfChars that is supposed to store the number of characters in a piece of text. Does the text “Hello World!” have 10, 11, or 12 characters? It depends on whether we’re including whitespace or punctuation. Rather than naming the variable numberOfCharsIncludingWhitespaceAndPunctuation, which is rather lengthy, a well placed comment on the declaration line should help the user figure it out:

Quiz time

Question #1

Based on how you should name a variable, indicate whether each variable name is correct (follows convention), incorrect (does not follow convention), or invalid (will not compile), and why.

int sum; // assume it’s obvious what we’re summing
Show Solution

int _apples;
Show Solution

int VALUE;
Show Solution

int my variable name;
Show Solution

int TotalCustomers;
Show Solution

int void;
Show Solution

int numFruit;
Show Solution

int 3some;
Show Solution

int meters_of_pipe;
Show Solution

1.8 -- Introduction to literals and operators
1.6 -- Uninitialized variables and undefined behavior

202 comments to 1.7 — Keywords and naming identifiers

  • Elie

    Quiz -> number 8 ... whoever wrote this is dirty minded ;D

  • Darren

    I would disagree about *always* starting an identifier with a lower-case letter. I use the convention of naming variables using underscore separated lower-case names for variables e.g. 'scale_factor'; camel-case starting with a lower-case character for function names e.g. 'fullSolve'; and camel-case starting with an UPPER-CASE for class names e.g 'RootSearch'. That way I know what the identifier is referring to when I come to use it later on in the code. I did play around with adding single characters to the beginning of variable names to show what type they represented, e.g. 'd' for double, 'f' for float, 'i' for int and so on with an additional 'p' for pointer variables, but found my code less readable (I think this is called Hungarian style). I do use an 'm_' to represent data members (variables) of a class as this is useful when writing the implementation of a class separate to its interface.

    Essentially the naming style is important to code readability, however I would say that consistency is more important than the style itself; the style you choose to adopt is personal to you if coding as a hobby, or will be set by the company for which you work (and its usually sensible practice not to upset those who pay your wage).

    • Alex

      Thanks for the additional thoughts. Unless I've misspoken somewhere (and please point out if I did so I can correct it), I've only indicated that variable and function names should start with a lower case letter, not all identifiers. Class names do typically start with an upper case letter, which I point out much later in the tutorials once I explain what a class is. I used to recommend Hungarian notation, because it does help avoid some potentially stupid mistakes (like doing integer divisions) but agree that it came at the cost of general comprehension.

      I agree that consistency is maybe more important than anything. I'll add a comment about that into the lesson.

  • Domenic D

    I would like to leave you a thank you for taking the time to create and maintain this site. I appreciate it a lot.
    When I started to learn C++ I thought it would be easier than it was. I believe it was my impatience or the previous language I use to use which was Visual Basic, however that was over a decade ago. I believe I lost most of the stuff I have learned. Trying all the other online courses seem to address the impatience however I would always reach a point where the material stopped being absorbed.

    I have been taking my time while going through your course and most of it is sticking so far. So I will continue along slowly while considering it was all possible due to your efforts which I will say I appreciate again as it is deserved.

    No need to respond as I just feel it is necessary to say when people do good work.

    PS I disabled adblocker for your site and actually checked out some of your sponsors. Hopefully one day I could take advantage of their services.

  • Luke

    int VaLuE; // incorrect (see your psychiatrist) ;) i really like that you make this tutorial really easy to understand and have good time learning it.
    this one is very funny
    thank you for this awesome tutorial

  • I know I might sound silly but just a question- Are keywords also stored in a header file like inbuilt functions?

  • Jens Larsen

    I would say number Number 7 in the quiz "nAngle" is BAD.

    It is like:
    int ccount    Bad    Nobody knows what a ccount is

    What is n? It is only acceptable if it is used in an obvious way.
    n can be:
    - number of angles, but should be called "numberOfAngles" instead
    - the n'th angle
    - a random letter because the variable angle is already taken (Yes, I have seen that before)

  • Jacob

    Number 7 in the quiz doesn't follow convention and should throw a compile error since it starts with a number.

  • Devashish

    Quiz solution #6

    "Void is a keyword"

    "Void" should be "void"

  • Anurag

    Hi Alex,

    Wonderful tutorial, and absolutely loving it. I have a question, I have used C++ for MS DOS environment, using turbo C IDE. In that, I could see the list of header files from Help menu. Also could see all the functions, their syntaxes etc. How can I do the same with these modern IDE. I have eclipse, and codelite and Netbeans 8.0.2.

    I have tried searching google for it, but all it shows is how to find list of functions and header files used in the given program, from navigation panel. I want the whole list, which I was able to see from turbo C. I can see the header files from cygwin folder, but I want to see it from inside the IDE.

    Your help/answer would be highly appreciated

    • Alex

      Sorry, I'm afraid I can't help you with this one. Any documentation related functionality around language features is really up to the individual IDE to provide or Sounds like Turbo C was pretty good in that regard. I suspect most other IDEs are not.

  • david coggins

    Sorry for the caps ,im at the back of the class you might not hear me..

  • bobz

    Thanks again, Alex.


  • bobz

    I am confused regarding "main"; it must be neither a keyword nor a user-defined function, right?  Is there something that you would call it if it is neither keyword nor user-defined?

    BTW this business of comments and conventions ring very true to electronic circuit design also.  You have no idea how many times I have looked at some work which I did months ago (or maybe even weeks ago).  In many cases I have built and tested; confirmed that it works as intended.  But I go back and look at my drawing and wonder how the heck that is supposed to work?  My wife thinks I am going senile.  :-(  I have been doing this for many years.

    So without being a programmer (at least not yet) I can say: Do not doubt Alex's wisdom regarding commenting and naming conventions.  If you really get into programming, you will be glad you did.  (Unless you have a great memory and like to write mysterious code.  But do not expect to get hired.)

    Wraith: IMHO it is more than just manner and etiquette. It is a matter of saving yourself and others a lot of difficulty down the road.

    • Alex

      main is not a keyword because keywords can't be used for function names.

      main is a user-defined function, actually (because you get to define what it does). However, it has been predeclared by the compiler (the compiler sets the name and parameters that it must have). I talk about the difference between defined vs declared in a few lessons.

      Main unique in this regard.

  • Wraith

    Good day sir.

    Just noticed, when you cited the rules for identifiers, it stated that "identifiers must begin with a letter (lower case or UPPER case)."

    Yet in the examples you shown, making a variable starting with an upper case letter is incorrect.

    Am I missing something? Or is just a mistake?

    Thank you. And I love the tutorials by the way.

  • Sam Tan

    I have a question regarding using the _ character at the beginnings of variable names. There have been a few occasions where I have seen someone begin the name of a private variable with an _. Is this an acceptable naming convention or not?

    • Alex

      Some developers use an underscore prefix to denote that a variable is private to a class. However, I strongly do not recommend this practice, as variable names starting with an underscore or double underscore are typically reserved for library implementations.

      Personally, I start my private member variables with the prefix "m_" (m for member).

  • siddhesh amrale

    One thing is wrong . Identifiers can be named with uppercase first letter.

    update it ..
    and also  add donate feature so ppl can donate you for good work.thanks anyway!

    • Alex

      From a syntax standpoint, you are correct that C++ will allow variables to start with either a lower case or upper case letter (or an underscore).

      However, it is convention that variable names start only with a lower case letter, and we advise that you follow this convention. Otherwise, anybody who views your code may be confused about why you are not following convention.

      There is a donation link in the Support LearnCpp section of the website.

  • Descaii

    I disagree with some of this, it is an OPINION that you need to use variables with the first letter lower case, or  camelCase, a person can name their variables any way they want, it doesn't matter what other people like to read. You should at least mention that making a variable with the first letter capitalized will not make an error or any real issue with their code, and that this is just suggested.

    • Alex

      I updated the article to make it clearer that identifier names can use both lower or upper case letters.

      The article already makes it sufficiently clear what C++ requires and what is convention. You are always free to not follow convention, though doing so is of questionable benefit.

  • Winnie33

    Alex, i just wanted to say your lessons are amazing.
    They are well written, with lots of examples to make sure the reader understands it fully.
    (A thing you don't see with most course sites :))
    You must have put a lot of time in these explanations, and I really appreciate it.
    Glad you exist.

    btw this is just genius xD

  • Confused about the function named main. how does the computer know where to start a cpp program if not at the function named main? I thought all cpp programs had to have this.

    • Alex

      C++ programs always start at function main(), and all C++ programs must have this function. What made you think otherwise?

      • My dear c++ Teacher,
        Please let me say that, indeed, I can use intetifier "main" as variable's name. For example following program runs fine.

        with regards and friendship.

    • Alex, Loganathan said all c++ programs SHOULD have the main function, and questioned why main was not a key word. You responded with this
      "My best guess is that keywords can’t be used for function names. So if main were a keyword, you wouldn’t be able to use it as a function name. Then you’d be in quite the pickle."
      So I though wait, they don't HAVE to have a function called main.  My bad! Thanks for a great, great tutorial and your help. Awesome!

  • Loganathan

    All the C++ programs should have a function named main. But main is not a keyword. Any reason behind this?

    • Alex

      My best guess is that keywords can't be used for function names. So if main were a keyword, you wouldn't be able to use it as a function name. Then you'd be in quite the pickle.

  • Paulo

    I Loled soooo HARD at this:

    Great guide btw. You have remarkable teaching qualities! I'm glad i found this website.


  • Farhad

    thank you for the useful website alex.!!

  • codeez

    The keywords table should be updated for C++11. It's cool you have added new chapters for the update and I totally get that you don't have time to edit every single page to match it, but I think the table at least should be updated, for reference's sake. :)

  • captain119

    "The identifier must begin with a letter or an underscore. It can not start with a number."
    Why is number 2 is marked as wrong if it said the tutorial that it must begin with a letter or an underscore?

  • PReinie

    How about acronymns in variable names? Should they be lower case (or lower case following the first character?

    In the above tutorial statement extraction: "numberOfCharsIncludingWhitespaceAndPunctuation, which is rather lengthy", could an acronym be used... "numberOfASCIIChars" (except ASCII may include more than intended)?

    Even "numberOfAsciiChars" would be good and follow the suggested convention, but do we violate acronym naming conventions? (Acronyms are often upper case to differentiate them from "real" words.)

    • rameye

      In my experience the less capital letters in an identifier the more readable it is.

      To keep the acronymn capitalized perhaps this would be better: numberOf_ASCII_Chars

  • Gabrola

    Well you stated that an identifier shouldn't begin with a capital letter right?
    On 1.11, the Comprehensive Quiz, your function identifiers were beginning in capital letters.

    I also did it that way before reaching this part of the tutorial and I didn't get any compile errors!

    • Alex

      Looks like I broke my own rule. :) It's fixed now.

      Starting variable or function names with a capital letter is valid C++, and so the compiler will accept it. It's only by convention that we don't do this.

  • JD

    Right on... I HATE when people type like that!

  • wrong:


    Check the comment in this code ^^

  • Steve

    lol @ quiz question 8.
    You're very slick, Alex. ;)

  • Argon

    Naming conventions are *SO* important.
    Before beginning a bigger project one should clearly define how one are going to build up variable names.
    If one have a clear definition of this, reading code are so much easy, tidy and so on.



    type tNameChar
    type tAgeChar
    type tCharHomePhone
    type tCharWorkPhone


    type tCharName
    type tCharAge
    type tCharPhoneHome
    type tCharPhoneWork

    Thank you for great tutorial.

  • Farheen Anis

    number 2 in quiz is fine as it started with an underscore.
    In the rules that are mentioned above, it is written that an indentifier can begin
    with an underscore

    • The quiz is talking about how you should name a variable, not how you can name a variable legally. I reworded the question to make it more obvious that is the intention.

      While technically it is technically legal under C++ syntax to name your variables starting with an underscore, generally it's not a good idea for several reasons:
      1) It's harder to read.
      2) Variables starting with underscores (usually a double underscore) are typically used to denote variables used by system libraries.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]