cppreference.com > C/C++ Keywords auto The keyword auto is used to declare local variables, and is purely optional. Related topics: register

asm
Syntax:

      asm( "instruction" );

The asm command allows you to insert assembly language commands directly into your code. Various different compilers allow differing forms for this command, such as

       asm {
         instruction-sequence
       }

or

       asm( instruction );

const_cast
Syntax:

      const_cast<type> (object);

The const_cast keyword can be used to remove the "const-ness" of some datum. The target data type must be the same as the source type, except (of course) that the target type doesn't have to be const.

Related topics:
const
dynamic_cast
reinterpret_cast
static_cast

bool

The keyword bool is used to declare Boolean logic variables; that is, variables which can be either true or false.

For example, the following code declares a boolean variable called done, initializes it to false, and then loops until that variable is set to true.

       bool done = false;
       while( !done ) {
       ...
       }

Also see the data types page.

Related topics:
char
double
false
float
int
long
short
signed
true
unsigned
wchar_t

break

The break keyword is used to break out of a do, for, or while loop. It is also used to finish each clause of a switch statement, keeping the program from "falling through" to the next case in the code. An example:

       while( x < 100 ) {
         if( x < 0 )
           break;
         cout << x << endl;
         x++;
       }

A given break statement will break out of only the closest loop, no further. If you have a triply-nested for loop, for example, you might want to include extra logic or a goto statement to break out of the loop.

Related topics:
continue
do
for
goto
switch
while

case

The case keyword is used to test a variable against a certain value in a switch statement.

Related topics:
default
switch

catch

The catch statement handles exceptions generated by the throw statement.

Related topics:
throw
try

char

The char keyword is used to declare character variables. For more information about variable types, see the data types page.

Related topics:
bool
double
float
int
long
short
signed
unsigned
void
wchar_t

class
Syntax:

      class class-name : inheritance-list {
      private-members-list;
      protected:
      protected-members-list;
      public:
      public-members-list;
      } object-list;

The class keyword allows you to create new classes. class-name is the name of the class that you wish to create, and inheritance-list is an optional list of classes inherited by the new class. Members of the class are private by default, unless listed under either the protected or public labels. object-list can be used to immediately instantiate one or more instances of the class, and is also optional. For example:

       class Date {
         int Day;
         int Month;
         int Year;
       public:
         void display();
       };

Related topics:
friend
private
protected
public
struct
this
typename
union
virtual

const

The const keyword can be used to tell the compiler that a certain variable should not be modified once it has been initialized.

It can also be used to declare functions of a class that do not alter any class data.

Related topics:
const_cast
mutable

dynamic_cast
Syntax:

      dynamic_cast<type> (object);

The dynamic_cast keyword casts a datum from one type to another, performing a runtime check to ensure the validity of the cast. If you attempt to cast between incompatible types, the result of the cast will be NULL.

Related topics:
const_cast
reinterpret_cast
static_cast

continue

The continue statement can be used to bypass iterations of a given loop.

For example, the following code will display all of the numbers between 0 and 20 except 10:

       for( int i = 0; i < 21; i++ ) {
         if( i == 10 ) {
           continue;
         }
         cout << i << " ";
       }

Related topics:
break
do
for
while

default

A default case in the switch statement.

Related topics:
case
switch

delete
Syntax:

      delete p;
      delete[] pArray;

The delete operator frees the memory pointed to by p. The argument should have been previously allocated by a call to new. The second form of delete should be used to delete an array.

Related topics:
(Standard C Memory) free
(Standard C Memory) malloc
new

do
Syntax:

      do {
      statement-list;
      } while( condition );

The do construct evaluates the given statement-list repeatedly, until condition becomes false. Note that every do loop will evaluate its statement list at least once, because the terminating condition is tested at the end of the loop.

Related topics:
break
continue
for
while

double

The double keyword is used to declare double precision floating-point variables. Also see the data types page.

Related topics:
bool
char
float
int
long
short
signed
unsigned
void
wchar_t

explicit

When a constructor is specified as explicit, no automatic conversion will be used with that constructor --it will only be used when an initialization exactly matches a call to that constructor.

else

The else keyword is used as an alternative case for the if statement.

Related topics:
if

enum
Syntax:

      enum name {name-list} var-list;

The enum keyword is used to create an enumerated type named name that consists of the elements in name-list. The var-list argument is optional.

Example code:

       enum color {red, orange, yellow, green, blue, indigo, violet};
       color c1 = indigo;
       if( c1 == indigo ) {
         cout << "c1 is indigo" << endl;
       }

namespace
Syntax:

      namespace name {
      declaration-list;
      }

The namespace keyword allows you to create a new scope. The name is optional, and can be omitted to create an unnamed namespace. Once you create a namespace, you'll have to refer to it explicitly or use the using keyword.

Example code:

       namespace CartoonNameSpace {
         int HomersAge;
         void incrementHomersAge() {
           HomersAge++;
         }
       }
       int main() {
         ...
         CartoonNameSpace::HomersAge = 39;
         CartoonNameSpace::incrementHomersAge();
         cout << CartoonNameSpace::HomersAge << endl;
         ...
       }

Related topics:
using

extern

The extern keyword is used to inform the compiler about variables declared outside of the current scope. Variables described by extern statements will not have any space allocated for them, as they should be properly defined elsewhere.

Extern statements are frequently used to allow data to span the scope of multiple files.

false

The Boolean value of "false".

Related topics:
bool
true

float

The float keyword is used to declare floating-point variables. Also see the data types page.

Related topics:
bool
char
double
int
long
short
signed
unsigned
void
wchar_t

for
Syntax:

      for( initialization; test-condition; increment ) {
      statement-list;
      }

The for construct is a general looping mechanism consisting of 4 parts:

1. the initialization, which consists of 0 or more comma-delimited variable initialization statements
2. the test-condition, which is evaluated to determine if the execution of the for loop will continue
3. the increment, which consists of 0 or more comma-delimited statements that increment variables
4. and the statement-list, which consists of 0 or more statements that will be executed each time the loop is executed.

For example:

       for( int i = 0; i < 10; i++ ) {
         cout << "i is " << i << endl;
       }
       int j, k;
       for( j = 0, k = 10;
            j < k;
            j++, k-- ) {
         cout << "j is " << j << " and k is " << k << endl;
       }
       for( ; ; ) {
         // loop forever!
       }

Related topics:
break
continue
do
if
while

friend

The friend keyword allows classes or functions not normally associated with a given class to have access to the private data of that class.

Related topics:
class

goto
Syntax:

      goto labelA;
      ...
      labelA:

The goto statement causes the current thread of execution to jump to the specified label. While the use of the goto statement is generally considered harmful, it can occasionally be useful. For example, it may be cleaner to use a goto to break out of a deeply-nested for loop, compared to the space and time that extra break logic would consume.

Related topics:
break

if
Syntax:

      if( conditionA ) {
        statement-listA;
      }
      else if( conditionB ) {
        statement-listB;
      }
      ...
      else {
        statement-listN;
      }

The if construct is a branching mechanism that allows different code to execute under different conditions. The conditions are evaluated in order, and the statement-list of the first condition to evaluate to true is executed. If no conditions evaluate to true and an else statement is present, then the statement list within the else block will be executed. All of the else blocks are optional.

Related topics:
else
for
switch
while

inline
Syntax:

      inline int functionA( int i ) {
      ...
      }

The inline keyword requests that the compiler expand a given function in place, as opposed to inserting a call to that function. Functions that contain static data, loops, switch statements, or recursive calls cannot be inlined. When a function declaration is included in a class declaration, the compiler should try to automatically inline that function.

int

The int keyword is used to declare integer variables. Also see the data types page.

Related topics:
bool
char
double
float
long
short
signed
unsigned
void
wchar_t

long

The long keyword is a data type modifier that is used to declare long integer variables. For more information on long, see the data types page.

Related topics:
bool
char
double
float
int
short
signed
void

mutable

The mutable keyword overrides any enclosing const statement. A mutable member of a const object can be modified.

Related topics:
const

operator
Syntax:

      return-type class-name::operator#(parameter-list) {
      ...
      }
      return-type operator#(parameter-list) {
      ...
      }

The operator keyword is used to overload operators. The sharp sign (#) listed above in the syntax description represents the operator which will be overloaded. If part of a class, the class-name should be specified. For unary operators, parameter-list should be empty, and for binary operators, parameter-list should contain the operand on the right side of the operator (the operand on the left side is passed as this).

For the non-member operator overload function, the operand on the left side should be passed as the first parameter and the operand on the right side should be passed as the second parameter.

You cannot overload the #, ##, ., :, .*, or ? tokens.

Related topics:
this

new
Syntax:

      pointer = new type;
      pointer = new type( initializer );
      pointer = new type[size];

The new operator allocates a new chunk of memory to hold a variable of type type and returns a pointer to that memory. An optional initializer can be used to initialize the memory. Allocating arrays can be accomplished by providing a size parameter in brackets.

Related topics:
delete
(Standard C Memory) free
(Standard C Memory) malloc

protected

Protected data are private to their own class but can be inherited by derived classes. The protected keyword can also be used as an inheritance specifier, which causes all public and protected members of the base class to become protected members of the derived class.

Related topics:
class
private
public

private

Private data of a class can only be accessed by members of that class, except when friend is used. The private keyword can also be used to inherit a base class privately, which causes all public and protected members of the base class to become private members of the derived class.

Related topics:
class
protected
public

register

The register keyword requests that a variable be optimized for speed, and fell out of common use when computers became better at most code optimizations than humans.

Related topics:
auto

public

Public data in a class are accessible to everyone. The public keyword can also be used as an inheritance specifier, which causes all public and protected members of the base class to become public and protected members of the derived class.

Related topics:
class
private
protected

typedef
Syntax:

      typedef existing-type new-type;

The typedef keyword allows you to create a new type from an existing type.

try

The try statement attempts to execute exception-generating code. See the throw statement for more details.

Related topics:
catch
throw

reinterpret_cast
Syntax:

      reinterpret_cast<type> (object);

The reinterpret_cast operator changes one data type into another. It should be used to cast between incompatible pointer types.

Related topics:
const_cast
dynamic_cast
static_cast

return
Syntax:

      return;
      return( value );

The return statement causes execution to jump from the current function to whatever function called the current function. An optional value can be returned. A function may have more than one return statement.

short

The short keyword is a data type modifier that is used to declare short integer variables. See the data types page.

Related topics:
bool
char
double
float
int
long
signed
unsigned
void
wchar_t

signed

The signed keyword is a data type modifier that is usually used to declare signed char variables. See the data types page.

Related topics:
bool
char
double
float
int
long
short
unsigned
void
wchar_t

sizeof

The sizeof operator is a compile-time operator that returns the size, in bytes, of the argument passed to it. For example, the following code uses sizeof to display the sizes of a number of variables:

      struct EmployeeRecord {
        int ID;
        int age;
        double salary;
        EmployeeRecord* boss;
      };

      ...

      cout << "sizeof(int): " << sizeof(int) << endl
           << "sizeof(float): " << sizeof(float) << endl
           << "sizeof(double): " << sizeof(double) << endl
           << "sizeof(char): " << sizeof(char) << endl
           << "sizeof(EmployeeRecord): " << sizeof(EmployeeRecord) << endl;

      int i;
      float f;
      double d;
      char c;
      EmployeeRecord er;

      cout << "sizeof(i): " << sizeof(i) << endl
           << "sizeof(f): " << sizeof(f) << endl
           << "sizeof(d): " << sizeof(d) << endl
           << "sizeof(c): " << sizeof(c) << endl
           << "sizeof(er): " << sizeof(er) << endl;

When run, the above code displays this output:

      sizeof(int): 4
      sizeof(float): 4
      sizeof(double): 8
      sizeof(char): 1
      sizeof(EmployeeRecord): 20
      sizeof(i): 4
      sizeof(f): 4
      sizeof(d): 8
      sizeof(c): 1
      sizeof(er): 20

Note that sizeof can either take a variable type (such as int) or a variable name (such as i in the example above).

It is also important to note that the sizes of various types of variables can change depending on what system you're on. Check out a description of the C and C++ data types for more information.

Related topics:
C/C++ Data Types

static

The static data type modifier is used to create permanent storage for variables. Static variables keep their value between function calls. When used in a class, all instantiations of that class share one copy of the variable.

static_cast
Syntax:

      static_cast<type> (object);

The static_cast keyword can be used for any normal conversion between types. No runtime checks are performed.

Related topics:
const_cast
dynamic_cast
reinterpret_cast

struct
Syntax:

      struct struct-name : inheritance-list {
      public-members-list;
      protected:
      protected-members-list;
      private:
      private-members-list;
      } object-list;

Structs are like `classes`, except that by default members of a struct are public rather than private. In C, structs can only contain data and are not permitted to have inheritance lists. For example:

       struct Date {
         int Day;
         int Month;
         int Year;
       };

Related topics:
class
union

switch
Syntax:

      switch( expression ) {
      case A:
      statement list;
      break;
      case B:
      statement list;
      break;
      ...
      case N:
      statement list;
      break;
      default:
      statement list;
      break;
      }

The switch statement allows you to test an expression for many values, and is commonly used as a replacement for multiple if()...else if()...else if()... statements. break statements are required between each case statement, otherwise execution will "fall-through" to the next case statement. The default case is optional. If provided, it will match any case not explicitly covered by the preceding cases in the switch statement. For example:

       char keystroke = getch();
       switch( keystroke ) {
         case 'a':
         case 'b':
         case 'c':
         case 'd':
           KeyABCDPressed();
           break;
         case 'e':
           KeyEPressed();
           break;
         default:
           UnknownKeyPressed();
           break;
       }

Related topics:
break
case
default
if

template
Syntax:

      template <class data-type> return-type name( parameter-list ) {
      statement-list;
      }

Templates are used to create generic functions and can operate on data without knowing the nature of that data. They accomplish this by using a placeholder data-type for which many other data types can be substituted.

Example code:

For example, the following code uses a template to define a generic swap function that can swap two variables of any type:

       template<class X> void genericSwap( X &a, X &b ) {
         X tmp;

         tmp = a;
         a = b;
         b = tmp;
       }
       int main(void) {
         ...
         int num1 = 5;
         int num2 = 21;
         cout << "Before, num1 is " << num1 << " and num2 is " << num2 << endl;
         genericSwap( num1, num2 );
         cout << "After, num1 is " << num1 << " and num2 is " << num2 << endl;
         char c1 = 'a';
         char c2 = 'z';
         cout << "Before, c1 is " << c1 << " and c2 is " << c2 << endl;
         genericSwap( c1, c2 );
         cout << "After, c1 is " << c1 << " and c2 is " << c2 << endl;
         ...
         return( 0 );
       }

Related topics:
typename

this

The this keyword is a pointer to the current object. All member functions of a class have a this pointer.

Related topics:
class
operator

throw
Syntax:

      try {
      statement list;
      }
      catch( typeA arg ) {
      statement list;
      }
      catch( typeB arg ) {
      statement list;
      }
      ...
      catch( typeN arg ) {
      statement list;
      }

The throw statement is part of the C++ mechanism for exception handling. This statement, together with the try and catch statements, the C++ exception handling system gives programmers an elegant mechanism for error recovery.

You will generally use a try block to execute potentially error-prone code. Somewhere in this code, a throw statement can be executed, which will cause execution to jump out of the try block and into one of the catch blocks. For example:

       try {
         cout << "Before throwing exception" << endl;
         throw 42;
         cout << "Shouldn't ever see this" << endl;
       }
       catch( int error ) {
         cout << "Error: caught exception " << error << endl;
       }

Related topics:
catch
try

true

The Boolean value of "true".

Related topics:
bool
false

typename

The typename keyword can be used to describe an undefined type or in place of the class keyword in a template declaration.

Related topics:
class
template

typeid
Syntax:

      typeid( object );

The typeid operator returns a reference to a type_info object that describes `object`.

unsigned

The unsigned keyword is a data type modifier that is usually used to declare unsigned int variables. See the data types page.

Related topics:
bool
char
double
float
int
short
signed
void
wchar_t

union
Syntax:

      union union-name {
      public-members-list;
      private:
      private-members-list;
      } object-list;

A union is like a class, except that all members of a union share the same memory location and are by default public rather than private. For example:

       union Data {
         int i;
         char c;
       };

Related topics:
class
struct

volatile

The volatile keyword is an implementation-dependent modifier, used when declaring variables, which prevents the compiler from optimizing those variables. Volatile should be used with variables whose value can change in unexpected ways (i.e. through an interrupt), which could conflict with optimizations that the compiler might perform.

using

The using keyword is used to import a namespace (or parts of a namespace) into the current scope.

Example code:

For example, the following code imports the entire std namespace into the current scope so that items within that namespace can be used without a preceeding "std::".

     using namespace std;

Alternatively, the next code snippet just imports a single element of the std namespace into the current namespace:

     using std::cout;

Related topics:
namespace

virtual
Syntax:

      virtual return-type name( parameter-list );
      virtual return-type name( parameter-list ) = 0;

The virtual keyword can be used to create virtual functions, which can be overridden by derived classes.

- A virtual function indicates that a function can be overridden in a subclass, and that the overridden function will actually be used.
- When a base object pointer points to a derived object that contains a virtual function, the decision about which version of that function to call is based on the type of object pointed to by the pointer, and this process happens at runtime.
- A base object can point to different derived objects and have different versions of the virtual function run.

If the function is specified as a pure virtual function (denoted by the = 0), it must be overridden by a derived class.

Example code:

For example, the following code snippet shows how a child class can override a virtual method of its parent, and how a non-virtual method in the parent cannot be overridden:

    class Base {
    public:
     void nonVirtualFunc() {
       cout << "Base: non-virtual function" << endl;
     }
     virtual void virtualFunc() {
       cout << "Base: virtual function" << endl;
     }
    };

    class Child : public Base {
    public:
     void nonVirtualFunc() {
       cout << "Child: non-virtual function" << endl;
     }
     void virtualFunc() {
       cout << "Child: virtual function" << endl;
     }
    };

    int main() {
     Base* basePointer = new Child();
     basePointer->nonVirtualFunc();
     basePointer->virtualFunc();
     return 0;
    }

When run, the above code displays:

    Base: non-virtual function
    Child: virtual function

Related topics:
class

void

The void keyword is used to denote functions that return no value, or generic variables which can point to any type of data. Void can also be used to declare an empty parameter list. Also see the data types page.

Related topics:
char
double
float
int
long
short
signed
unsigned
wchar_t

wchar_t

The keyword wchar_t is used to declare wide character variables. Also see the data types page.

Related topics:
bool
char
double
float
int
short
signed
unsigned
void

while
Syntax:

      while( condition ) {
      statement-list;
      }

The while keyword is used as a looping construct that will evaluate the statement-list as long as condition is true. Note that if the condition starts off as false, the statement-list will never be executed. (You can use a do loop to guarantee that the statement-list will be executed at least once.) For example:

       bool done = false;
       while( !done ) {
         ProcessData();
         if( StopLooping() ) {
           done = true;
         }
       }

Related topics:
break
continue
do
for
if