Open In App

C++ Keywords

C++ is a powerful language. In C++, we can write structured programs and object-oriented programs also. C++ is a superset of C and therefore most constructs of C are legal in C++ with their meaning unchanged. However, there are some exceptions and additions.

Token

When the compiler is processing the source code of a C++ program, each group of characters separated by white space is called a token. Tokens are the smallest individual units in a program. A C++ program is written using tokens. It has the following tokens:



Keywords

Keywords(also known as reserved words)  have special meanings to the C++ compiler and are always written or typed in short(lower) cases. Keywords are words that the language uses for a special purpose, such as void, int, public, etc. It can’t be used for a variable name or function name or any other identifiers. The total count of reserved keywords is 95. Below is the table for some commonly used C++ keywords.



C++ Keyword

asm double new switch
auto else operator template
break enum private this
case extern protected throw
catch float public try
char for register typedef
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete int static volatile 
do long struct while 

Note: The keywords not found in ANSI C are shown here in boldface.

What is the identifier?

Identifiers refer to the name of variables, functions, arrays, classes, etc. created by the programmer. They are the fundamental requirement of any language.

Rules for naming identifiers:

Examples of good and bad identifiers

Invalid Identifier  Bad Identifier  Good Identifier
Cash prize C_prize cashprize
catch catch_1 catch1
1list list_1 list1

Example:




// C++ program to illustrate the use
// of identifiers
 
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Use of Underscore (_) symbol
    // in variable declaration
    int geeks_for_geeks = 1;
 
    cout << "Identifier result is: "
         << geeks_for_geeks;
 
    return 0;
}

Output
Identifier result is: 1

How keywords are different from identifiers?

So there are some main properties of keywords that distinguish keywords from identifiers:

Keywords  Identifiers
Keywords are predefined/reserved words  identifiers are the values used to define different programming items like a variable, integers, structures, and unions.
Keywords always start in lowercase identifiers can start with an uppercase letter as well as a lowercase letter.
It defines the type of entity. It classifies the name of the entity.
A keyword contains only alphabetical characters,  an identifier can consist of alphabetical characters, digits, and underscores.
It should be lowercase. It can be both upper and lowercase.
No special symbols or punctuations are used in keywords and identifiers.  No special symbols or punctuations are used in keywords and identifiers.  The only underscore can be used in an identifier.
Keywords: int, char, while, do. Identifiers: Geeks_for_Geeks, GFG, Gfg1.

Example: Below is the program for how to use different keywords in the program:




// C++ Program to demonstrate keywords
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Variable declaration and
    // initialization
    int n = 2;
 
    // Switch Case Statement
    switch (n) {
    case 1:
        cout << "Computer Network"
             << endl;
        break;
    case 2:
        cout << "C++" << endl;
        break;
    case 3:
        cout << "DBMS" << endl;
        break;
    case 4:
        cout << "Data Structure"
             << endl;
        break;
    case 5:
        cout << "Operating System"
             << endl;
        break;
    default:
        cout << "Enter Valid number"
             << endl;
    }
 
    // Return keyword returns an object
    // to a function's caller
    return 0;
}

Output
C++


Article Tags :