Learn C++ with Qt, Part 010:
Identifiers and reserved keywords


Before we start with the more interesting parts of C++ programming, I’d like to share some information about “identifiers” in C and C++.

What is an identifier?

As the name suggests, an identifier identifies something. This can be the declaration of a variable, a data type, a function or anything else that is allowed as part of a program source code.

In a computer program, an identifier is a keyword that identifies different operations or data descriptions within the given computer programming language.

In order to be valid, an identifier has to start with a letter. After that, it can contain a sequence of letters, digits or underscore characters. Blank space characters, punctuation marks and symbols are not allowed for identifiers.

The letters you use for an identifier can be small or capital letters. It is important to remember though that C++ is a “case sensitive” programming language.

This means that similar identifiers with a different combination of small and capital letters do not refer to the same object. If you name a variable “MYVAR” when it is defined and use “myVar” later in order to access its contents, the compiler tell you that “myVar” is an “undeclared identifier”.

Modern code editors and integrated development environments keep track of your identifiers and help you to use the right letters by often providing a pop-up list with the already existing identifiers from your code.

While you can use underscore characters (“_”) at the beginning of an identifier, you should not do that for your own identifiers.

Single or double underscore characters are usually used for special compiler-related or external identifiers. If you define an identifier that matches a compiler-identifier (which usually passes special instructions to the compiler), this can have unforeseen side effects, and the program may not work the way you intend it to. 

If you look at the programs from the earlier parts of this tutorial, you can find a lot of identifiers in their code.

Reserved keywords

Like most other programming languages, C++ uses certain identifiers as so called “reserved keywords”. These keywords are used for the syntax of the programming language, and can only be used in the way that is allowed by the syntax rules of the language. You cannot use these keywords as identifiers for your own variables, data types, functions etc.

You will get to know the meaning and usage of most reserved keywords during the upcoming parts of this tutorial (some were already used in earlier programs), but for now here is a list of the reserved keywords for C++ in alphabetical order that you can use as a reference:

Keyword
of bits
Keyword type Meaning
alignas declaration specifies the alignment requirement of a type or an object
alignof definition queries alignment requirements of a type
and operator Boolean (logical) “AND” operation between two expressions or values
and_eq operator alternative operator for “&=”, bit-wise “AND” operation for a value or expression (e.g. binary values “1100” and “0111” combined with “and_eq” will result in the binary value “0100” as the second bit is set to “1” and equal to the Boolean “true” value in both binary values, while the rest is “false” when each position of the value is compared)
asm declaration declaration of an inline assembly block (used to add assembler code inside C and C++ code)
auto declaration the type of a variable will be determined from the initialization of that variable (i.e. from the type of value that it is assigned)
bitand operator bit-by-bit logical “AND” operation between two expressions or values (similar to “and_eq”)
bitor operator bit-by-bit logical “OR” operation between two expressions or value
bool declaration defines a variable of the type “boolean” that can only have the values “true” or “false”
break control structure stop end exit a currently running loop or command block inside a program, returning to the code outside the loop or command block
case control structure declaration of the case labels for a switch statement
catch control structure part of a try-catch block of commands, declaring the expression to be catched
char declaration declares a character type variable
char16_t declaration declaration of a character type variable using 16 bits
char32_t declaration declaration of a character type variable using 32 bits
class declaration declaration of an object class
compl operator alternative operator for ~
const specifier declaration of a constant
constexpr declaration specifies that the value of a variable or function can appear in constant expressions>
const_cast declaration type conversion expression, declares an expression as constant
continue control structure causes the remaining portion of the enclosing loop body to be skipped, continue the program starting from the beginning of the loop with the next loop counter value
decltype declaration declare the data type of a variable depending on the type of a given expression
default control structure declare a specific case in a switch statement as the default option to be executed when no other cases are met
delete operator invokes an object destructor that deletes the object instance and releases the previously reserved memory (that was allocated with the “new” expression)
do control structure declares the start of a do-while loop
double declaration declares a double precision floating point type
dynamic_cast declaration safely converts pointers and references to classes up, down, and sideways along the inheritance hierarchy
else control structure declares the start of the second option part in an if-else control structure
enum declaration declares an enumeration type
explicit declaration specifies constructors and conversion operators that don't allow implicit conversions or copy-initialization
export declaration before the C++11 standard this used to mark a template definition to be exported, which allows the same template to be declared, but not defined, in other translation units
extern declaration defines a storage class as extern, with static or thread storage duration and external linkage
false literal boolean literal for the "false" boolean value
final specifier specifies that a virtual function cannot be overridden in a derived class or that a class cannot be inherited from
float declaration declares a variable or value to be of floating point type (can be single precision)
for control structure marks the start of a "for"-loop
friend declaration used for methods or attributes of an object class definition in order to make specific private or protected methods or attributes available for another object class or function
goto control structure unconditional branch to a different part of the same function or method marked by the referenced label
if control structure start of a conditional branch structure that is executed when the following condition is true
inline declaration definition of an inline function or namespace
int declaration defines a variable or value of the type integer
long declaration defines a variable or value of the type long integer for bigger integer values
mutable specifier defines a class member as non-static, specifies that the member does not affect the externally visible state of the class
namespace declaration declares a namespace for object methods and attributes from external libraries that are used in a program
new operator creates a new instance of an object class and reserves the necessary memory
noexcept operator performs a compile-time check that returns true if an expression is declared to not throw any exceptions
not operator Boolean operator for negating a Boolean expression
not_eq operator Boolean operator for “NOT EQUAL”
nullptr declaration declaration of a “null” pointer
operator declaration declaration of an overloaded operator
or operator Boolean “OR” operator
or_eq operator alternative Boolean operator for “OR EQUAL”
override specifier specifies that a virtual function overrides another virtual function
private declaration private access specifier for parts of an object class definition
protected declaration protected access specifier for parts of object classes
public declaration public access specifier for parts of object classes
register declaration automatic storage duration specifier
reinterpret_cast declaration defines a type conversion expression that does not compile to any CPU instructions
return declaration declares the return statement of a program, function or method
short declaration declaration of a “short” integer type variable
signed declaration declares a numeric variable to be signed (uses first bit to show if the value is positive or negative), so zero, positive and negative values are permitted
sizeof operator returns the memory size of a given type, expression or object
static declaration declares the storage duration of a variable or class member as static
static_assert operator performs compile-time assertion checking
static_cast operator converts between types using a combination of implicit and user-defined conversions
struct declaration definition of a custom data structure (made up of one or more variables of similar or different types or classes)
switch control structure declaration of a switch statement
template declaration declaration of a template
this operator ”this” pointer (pointer to current object instance)
thread_local declaration thread storage duration specifier
throw control structure signals an erroneous condition and executes an error handler
true literal literal for Boolean “true” value
try control structure start of a try-catch block; associates one or more exception handlers (catch-clauses) with a compound statement
typedef declaration used for user-defined data types
typeid operator queries information of a type
typename declaration definition of type template parameters, template template parameters or dependent names as a type
union declaration declaration of a union data type
unsigned declaration defines a numeric data type as unsigned, so only zero and positive values are permitted
using declaration references a type, namespace or function- or method-definition that is defined outside of the current declarative region (e.g. in a separate file or library)
virtual declaration specifies that a non-static member function or a base class is virtual and supports dynamic binding
void declaration declares a function or method return value or pointer to have no defined type, turning functions into procedures
volatile specifier defines a variable type to be volatile – every access of a volatile object is handled in a separate thread
wchar_t declaration definition of a character type for wide character representation
while control structure start of a while loop that is executed as long as the Boolean control expression returns true
xor operator Boolean “exclusive OR” operator
xor_eq operator Boolean “exclusive OR EQUAL” operator

Specific compilers may also have additional specific reserved keywords, but the above list shows all reserved standard C++ keywords up until the C++11 standard.

You can use the table above as a reference if you do not write your code in a specialized code editor. 

Specialized code editors – either separate ones like “Notepad++” or IDE-integrated editors (like in the “Qt Creator” IDE) usually have integrated support for C++ and other programming languages. If you use a specialized code editor, you can use the automatic code completion and syntax highlighting to see which words are reserved keywords.

What if…?

What happens when you use C++ identifiers or reserved keywords as names for your own variables, types, functions etc.?

Doing this automatically leads to errors in your program – not only syntax errors which are easy to fix, but also logical errors that can be difficult to locate and fix. 

So my recommendation is to use a dedicated code editor that supports the programming language you use (C++ in case of this tutorial). Identifiers and reserved keywords will be offered for auto-completion when you are typing in your code, and they will be highlighted in different colors and styles, making it easy to spot them in your code.

Final words

That should be enough for now. I hope you’ll join me in the next part of the C++ with Qt tutorial when things get a little more interesting…