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

  • Ivan

    I know what game will have the variable 3some in it :)

  • Josh

    'decltype' keyword is since C++11.

  • I'm starting to get comfy. I just love the way everything is simply put up and everything so far has been a breeze.
    By the way, how does one upload a picture on their profile?

  • CyanCoding

    Are you going to update your tutorials for C++17 and later versions in the future?

  • Guozili

    Hi, Alex:
        I have a confusion and can not figure it out. That is, for such a statement:  int num = 5;  Whether num represents for identifier or variable? From this lecture, I know that num is called a variable's name, then what is the variable itself?

    • nascardriver

      Hi Guozili!
      A variable consists of three parts:
      - Type
      - Identifier (Name)
      - Value
      In your example int is the type, num the identifier and 5 the value.
      A variable is typically referred to by it's identifier. We say "the variable num" and we mean "the variable with identifier num".

  • Thomas Aunvik

    On Visual Studio (Unity C#) when it comes functions it gives me a suggestion to use uppercase first letter instead.
    void someFunction();
    And the suggestion is:
    void SomeFunction();

    • nascardriver

      Hi Thomas!
      What you use is up to you, according to Google's C++ style guide function names should start with a capital letter:

      "Ordinarily, functions should start with a capital letter and have a capital letter for each new word (a.k.a. "Camel Case" or "Pascal case")."

      I recommend going with upper case functions to make them easier distinguishable from variables.

      • Thomas Aunvik

        Alright, thanks for the reply.

        • Alex

          I find the google style guidelines for functions to be a bit weird -- they recommend capital letter function names, but then they allow lower case function names for accessors and mutators. Seems more consistent to just lower case all the function names. Not to mention that main() and the standard library user lower case names. Also if you provide a custom function to make your class work with range-based for loops, that function must be named begin or end, and it's case sensitive.

          You can, of course, do whatever you want, and the most important thing is to be consistent with the code around yours. But I personally think Google's recommendation leads to inconsistency, and that's not great when there are more consistent choices.

  • Taksh

    Hey Alex,

    I wrote some code in which I declared a variable - time. It worked fine. But, when I ran the same code on an online platform, it threw some weird error about using time as a variable. When I changed the name of the variable to something else, it started to work fine.

    So, I went online to check if time is a reserved keyword but couldn't find any such evidence.

    Is time is reserved keyword or not ?

    Thanks in advance.

  • gSymer

    8) int *3some*;


  • mondomole

    This has been a bit distracting...

    "can not" ought to be "cannot"

    These lessons are absolutely fantastic! Very well written!

  • Azure

    5 is also following the rules, no?

  • William

    Do source files follow the same conventions as identifiers?

    Would this:

    Be preferred over the following:

    • Alex

      I'm not aware of any consistent recommendations in regards to filenames (other than use .cpp and .h as the extensions). Google recommends using all lower case, with dashes or underscores to separate words. Personally, I prefer capitalizing the first letter of each word in the name.

  • Ritesh Kumar

    Hi, I am learning C++ for my OpenSees project and find this site very handy to start with. The person clearly got funny bones here. This one made my day :)

    int VaLuE; // incorrect (see your psychiatrist) ;)

  • Fantastic web site. Penty οf helpful info Һere. I am sending іt to seveгal pals aans also sharing in delicious.

    Ꭺnd оf course, thannk yoou for your sweat!

  • coder_king

    3some :P HAHAH

  • Jeremy

    Seems there are 4 distinct styles, though no one seems to know where each came from, exactly:

    CamelCase (or PascalCase, or UpperCamelCase)
    camelCase (or lowerCamelCase)

    The level of disagreement on which style should be used for which type of identifier seems high enough to start WWIII.
    And most people seem to despise the Google Style Guide (
    We live in a quirky world.

  • My dear c++ Teacher,
    Please let me ask you something of the many I do not understand. At
    there is a list of Unicode character ranges that are allowed in identifiers. What does it mean?
    With regards and friendship.

    • Alex

      Since C++ tends to be English-language and ASCII-centric (all of the keywords are English words), most people name their variables and functions using standard ASCII characters. However, there are languages that use other symbols (such as Russian). To better support non-English languages, C++ allows you to name your variables and functions using unicode characters, as long as they fall within the accepted unicode range.

      This means you can do things like, "int Ж = 5;".

  • Felipe

    isn’t it generally a good practice to name a variable if it’s static in capital letters?

  • My dear c++ Teacher,
    Please let me express my difficulty in understanding your comment:

    How is it possible something incorrect be valid?
    With regards and friendship.

    • Alex

      Valid in that the C++ compiler will accept it, but incorrect according to best practices.

      • My dear c++ Teacher,
        Please let me say that best practices have exceptions as every rule have.
        For example, real part of complex number is denoted by "Re" and imaginary's by "Im". Then best names for corresponding variables are "Re" and "Im".
        With regards and friendship.

        • A. Sahni

          First of all I would like to thank you for this amazing tutorials. I appreciate your efforts. I would like to ask you  that( if I am not wrong) , in Identifier naming rules it is said that Identifier must begin with a letter or underscore(3rd point), but in Idetifier naming best is said that avoid naming your identifier with an underscore. So can you please help me! I will be very thankful to you.

      • My dear c++ Teacher,
        Please let me say more examples of exceptions from your rule. Suppose I want countries names be variables names. Then what should variable name for your country be? usa, uSa, or USA?
        Or, if you give variables names, names of USA Presidents, what do you will prefer?
        1. Washington or washington?
        2. Lincoln or lincoln?
        3. Roosevelt or roosevelt?
        4. Kennedy or kennedy?
        last but not least
        5. Obama or obama?
        With regards and friendship.

        • Alex

          Personally, I'd use lower case for everything: usa, washington, etc... Consistency leads to more efficiency, comprehension, and fewer errors.

          • My dear c++ Teacher,
            Please let me disagree on this point. I can not give variable name greece, for, it is my country, nor france for is the country I live. Also I can not use variables names, tsipras or hollande. I should use, Greece, France, Tsipras, Hollande.
            With regards and friendship.

            • Alex

              Let's not confuse "can not" with "will not". You _can_ use lower case letters for variables holding country names, and you _should_ (as that is the common convention). If you choose not to do so for some other reason, then you _won't_, and that's your personal choice.

              For what it's worth, capitalizing proper names is a spoken language convention. When you write code, you really should follow coding conventions, not speaking conventions.

  • My dear c++ Teacher,
    Please let me take position at disagreement side on that "Generally, functions are also started with a lowercase letter ...".
    Compilers online execute following program without complain.

    With regards and friendship.

    • Alex

      C++ does not require functions to start with a lower case letter, but it is a good idea. It is up to you if you want to follow best coding practices or not.

  • My dear c++ Teacher,
    Please let me my first comment on this section:
    You state "First, it is a convention in C++ that variable names should begin with a lowercase letter".
    I do not understand (am Greek living in France) what you mean by "convention" but by trial I found that compilers on line
    run fine following program without throw any warning.

    With regards and friendship.

    • Alex

      "Convention" means "something that people commonly do".

      In this case, C++ doesn't require that variables start with a lower case letter (which is why your program compiles), but most people do so because it's a good idea.

  • My dear c++ Teacher,
    Please let me ask my first question about this section.
    In subsection "Keywords" you present keywords through c++14. Also by one * those added in c++98 and by two ** those in c++11. My question:
    In c++14 no one added?
    With regards and friendship.

  • Tim

    When I first starting learning to code years ago the naming convention taught to me was prefixing with camelCase. (Although it wasn't called camelCase back then.)

    For example:

    (Integer variable)

    (String variable)

    I don't see that type of naming convention at all anymore. I really liked the fact that it told you what type of object it was right in the name. Is there a reason(s) this naming convention fell out of favor?

    • Alex

      The type prefixing you're talking about is generally referred to is generally called "Hungarian Notation", and I used to teach it myself. But as I've been updating the lessons, I've moved away from it in accordance with modern best practices. Hungarian notation doesn't add a lot of value in strongly-typed languages like C++, because it repeats information that can already be easily gleaned from the compiler. It makes naming more complicated, and making changing a variable's type painful (e.g. if you change a short to an int, now you have to rename all instances of the variable).

      Better is to add prefixes that impart information that can't be easily gleaned, like whether a position is absolute or relative, or whether a temperature is Celsius, Fahrenheit, or Kelvin.

      • Tim

        Thanks for the reply. I don't really understand why it adds less value to a strongly-typed language (or maybe I don't truly understand what a strongly-typed language is.)

        The compiler isn't going to care what I call an object I have created. If I try to put a number into an integer variable named myVariable, the compiler is going to be OK with that. On the other hand if I try to put a string into that variable the compiler is going to error out, but I won't know that until I try to compile. Now I need to go through the error log to find out were the error is and correct it. If I named it intVariable I would know it before I tried to compile. The whole point of naming objects is to make it easier for the person reading the code to understand what that piece of code is and does, not the compiler.

        Most of my coding experience is in batch files, VB 6, VB script, and a little (and I mean very little) Java. In some of my VB6 projects the prefixing with variable type saved me a lot of time and made it much easier to remember what I was doing 6 months after doing it. I am also a hobby coder, no formal coding education, so that may be part of the problem with me understanding.

        Again, thanks for the reply. And thanks for this website. I tried learning C++ several times before and this website has done more for me than everything else I have tried before it.

        • Alex

          C++ forces you to declare the type of your variables, and does all kinds of checking to ensure you're not doing things that are type-inappropriate. When you do something like this:

          prefixing your variable with "int" is redundant, you just declared that it was an int, so why put it in the name too? If you're looking at variables that weren't just defined, most modern IDEs will show you the type on hover. So encoding it in the name is mostly redundant. Many people feel putting type prefixes on your variables does not make your code easier to read or understand -- that it just adds needless clutter.

          Consider a weakly typed language like PHP. In a language like that, you can do this:

          In a language like that, where variables can hold different types, being more explicit about the type of value your variable is intended to hold can be useful, because the type is set (and can be changed) at runtime.

          There's a fair good discussion on the topic here on Stack Overflow. The top answer is fairly comprehensive and understandable.

  • Vlad

    i downloaded code::blocks but i got the 98 version, the version from their website is not completing the download, i don't know why, is there any way i can find the latest version somewhere else? and do you have an idea about the microsoft IDE ... it says to me that it does not work on windows 7, so which windows? 8 and above?

  • Garry E

    It's interesting to note that C has only 32 keywords compared to 73 as shown at the top of this tutorial or
    perhaps 90 found on the web at I imagine the 'how come?' will become clear to me as I proceed through your incredibly well written nybbles of text explaining the C++ programming language.

    • Alex

      I was missing 11 keywords that work as substitutes for operators. I've never seen anybody use these, but they are valid keywords so I've added them to the table.

      The other keywords listed on are part of experimental features that haven't been formally accepted into the C++ specification yet.


    I am beginner..
    I find your tutorials very interesting and easy to learn so far....
    I hope that this learning enable me to be able to write my own projects for some extra income some day...

    Thanks for great work...

  • Roua

    Help me
    Which if the following identifiers are right and which are wrong


    • My dear c++ Teacher,
      Please let me answer above question and correct me where I'm wrong.
      1. Age, R79J, Totaiincome (or Totalincome) are valid but not recommended, because first letter is capital.
      2. _Invalid is valid but not recommended for begins with underscore.
      3. !ex is invalid for begins with no letter or underscore.
      Tanks in advance.
      With regards and friendship.

  • John

    This code compiles without errors using g++ test.cpp


    So why isn't main a reserved keyword?

    • Alex

      Keywords can't be used as function names.

      • John

        Is it correct to say that main() is a built in function, and that main is a reserved keyword in addition to the table you started this section with?

        If this is the case, are there any additional built in functions? The trigonometric functions, e.g. sin and cos, are not built in, they require <math> to work.

        • Alex

          No, and no. main() is a user-defined function that just happens to be required. It is not a reserved keyword (as evidenced by the fact that you can create a variable named main).

          C++ does not have any built-in functions. But the C++ library contains tons of predefined functions (such as sin and cos) that you can include in your programs if you want.

Leave a Comment

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