Best C++ Keywords All List Explanation in 2023

List of Keywords in C++ and their functions

Keyword: A C++ programming language, keywords are predefined, reserved words that have special meanings to the compiler. C++ Keywords cannot be used as identifiers (i.e., names of variables, functions, etc.) in your code.

Token: In the C++ programming language, a token is the smallest unit of a program that is meaningful to the compiler. Tokens can be individual words (such as keywords, identifiers, and constants), punctuation marks, or special symbols.


Here are some examples of tokens in C++:

Keywords: int, for, while

Identifiers: x, sum, my_function

Constants: 5, 3.14, 'a'

Punctuation marks: {, }, (, ), ;

Special symbols: +, -, *, /, %

The C++ compiler breaks a program down into tokens and then processes them according to the rules of the language. This process is known as lexical analysis or tokenization. The resulting tokens are then passed to the compiler's parser, which determines the structure and meaning of the program based on the relationships between the tokens.


Best C++ Keywords All List Explanation
Best C++ Keywords All List Explanation 

    keyword: In programming languages, a keyword is a word that is reserved by the language for a specific purpose and cannot be used as an identifier (i.e., a name for a variable, function, etc.). Keywords are typically used to represent programming constructs or language features that cannot be expressed using ordinary identifiers.

    Keywords are an important part of the syntax of a programming language, and they are often used to signal the beginning or end of a particular construct or block of code. For example, in C-style languages like C++, keywords such as for, while, and if are used to define loops and conditional statements. In object-oriented languages like Java and C#, keywords such as class, interface, and extends are used to define types and inheritance relationships.

    Different programming languages have different sets of keywords, and the exact set of keywords for a particular language may change over time as the language evolves. Some languages also allow users to define their own keywords or reserved words, although this is usually not recommended as it can lead to confusion and compatibility issues.

    Identifiers: In programming languages, an identifier is a name given to a variable, function, or other programming construct. Identifiers are used to refer to these constructs in the code, and they can be used to give names to things like variables, functions, classes, and objects.

    The rules for naming identifiers vary from language to language, but most languages have some basic rules in common. For example:

    1. Identifiers can typically contain letters, digits, and underscores, but they cannot begin with a digit.

    2. Identifiers are usually case-sensitive, so x and X are considered to be different identifiers.

    3. Most languages have a set of reserved words or keywords that cannot be used as identifiers.


    Constants: In programming languages, a constant is a value that cannot be changed during the execution of a program. Constants are typically used to represent fixed values that are used frequently in a program, such as mathematical constants, physical constants, or string literals.

    In most programming languages, constants are defined using a keyword or directive, and they are usually assigned a value when they are defined. For example, in C++, you can define a constant using the const keyword.

    const double PI = 3.14159;

    const int MAX_ITERATIONS = 100;

    const char* MESSAGE = "Hello, World!";


    How Many Keywords in C++ Wikipedia

    autobreakcase
    charcontinuedefault

    do

    doubleelse
    enumexternfloat
    forgotoif
    inlineintlong
    registerreturnshort
    signedsizeofstatic
    structswitchtypedef

                   union

    unsignedvoid
    volatilewhilefriend
    virtualpublicprivate
    thisclasstemplate
    trydeleteprotected

    Note that this list includes some keywords that are specific to C++ and not present in the C programming language. There are also a few additional keywords that are only available in certain C++ versions or in certain contexts.

    C++ Keywords List

    C++ auto Keyword

    auto keyword: In C++, the auto keyword is used to specify that the type of variable should be deduced from its initializer. This can be useful when the type of the variable is complex or not easily written out, or when the type is subject to change, and you want to avoid having to update the declaration every time it changes.

    auto x = 5; // x is an int

    auto y = 3.14; // y is a double

    auto z = "hello"; // z is a char* 

    In each of these cases, the type of the variable is deduced from the type of the initializer on the right-hand side of the assignment. x is an int because 5 is an int, y is a double because 3.14 is a double, and z is a char* because "hello" is a string literal of type char[6] that decays to a char*.

    break Statement in C++ Example

    break keyword: In C++ and many other programming languages, the break keyword is used to exit a loop or switch statement prematurely. When a break statement is encountered inside a loop or switch, control is immediately transferred to the statement following the loop or switch.

    for (int i = 0; i < 10; i++) {

      if (i == 5) {

        break;

      }

      cout << i << endl; // prints 0 1 2 3 4

    }

    In this example, the break statement is encountered when i is equal to 5, causing the loop to terminate and control to be transferred to the statement following the loop. The loop will iterate 5 times and print the values 0 through 4, but it will not print 5.

    case keyword: In C++ and many other programming languages, the case keyword is used in conjunction with the switch statement to specify a block of code to be executed when the value of a particular expression matches the value of the case.

    switch (x) {

      case 0:

        cout << "x is zero" << endl;

        break;

      case 1:

        cout << "x is one" << endl;

        break;

      default:

        cout << "x is something else" << endl;

    }

    In this example, the value of x is compared to the values specified in each case clause. If x is equal to 0, the block of code following the case 0: clause will be executed. If x is equal to 1, the block of code following the case 1: clause will be executed. If x is not equal to either 0 or 1, the block of code following the default: clause will be executed.

    The case keyword is typically followed by a constant expression or a variable of a type that can be compared using the == operator. If the value of the expression matches the value of the case, the block of code following the case will be executed.


    char keyword: In C++, the char keyword is used to declare variables of type char, which is a built-in data type that represents a single character.

    A char variable can store any character from the ASCII character set, which includes the letters of the alphabet, digits, punctuation marks, and special characters. A char variable is typically stored in a single byte of memory and can hold values from -128 to 127 (if signed) or 0 to 255 (if unsigned).

    char ch = 'A';


    continue Keyword in Cpp

    continue keyword: In C++ and many other programming languages, the continue keyword is used to skip the remainder of the current iteration of a loop and move on to the next iteration.

    for (int i = 0; i < 10; i++) {

      if (i % 2 == 0) {

        continue;

      }

      cout << i << endl; 

    }

    In this example, the continue statement is encountered when i is an even number, causing the loop to skip the remainder of the current iteration and move on to the next one. The loop will iterate 10 times and print the values 1, 3, 5, 7, and 9, but it will not print the even numbers.


    class keyword: class keyword is used to declare a class, which is a user-defined data type that can contain data members (variables) and member functions (methods). A class is a template for creating objects, and an object is an instance of a class.

    The class keyword is used to define a class in C++, and the class definition specifies the data members and member functions that the class will contain. Class definitions are typically placed in a separate header file, which can be included in other source files to use the class.

    class Example {

     private:

      int x;

      int y;


     public:

      Example(int x, int y) : x(x), y(y) {}


      int getX() const { return x; }

      int getY() const { return y; }

      void setX(int x) { this->x = x; }

      void setY(int y) { this->y = y; }

    };


    default access specifier in C++

    default keyword: In C++ and many other programming languages, the default keyword is used in a switch statement to specify a block of code to be executed when the value of the switch expression does not match any of the case labels.

    switch (x) {

      case 0:

        cout << "x is zero" << endl;

        break;

      case 1:

        cout << "x is one" << endl;

        break;

      default:

        cout << "x is something else" << endl;

    }

    In this example, the default clause is used to specify a block of code to be executed when the value of x does not match either 0 or 1. If x is equal to 0, the block of code following the case 0: clause will be executed. If x is equal to 1, the block of code following the case 1: clause will be executed. If x is not equal to either 0 or 1, the block of code following the default: clause will be executed.


    do keyword: In C++ and many other programming languages, the do keyword is used to start a do-while loop. A do-while loop is a type of loop that executes a block of code at least once and then continues to repeat the block as long as a certain condition is true.

    int i = 0;

    do {

      cout << i << endl;

      i++;

    } while (i < 10);

    In this example, the loop will execute the block of code at least once and then continue to repeat the block as long as i is less than 10. The loop will print the values 0 through 9 and then terminate.

    The do-while loop is similar to a while loop, but it guarantees that the loop body will be executed at least once. This can be useful in situations where you want to repeat a block of code until a certain condition is met, but you need to perform some initial setup or processing before the condition is checked.

    double keyword: The double keyword is used to declare a variable of type double, which represents a double-precision floating-point number. A double-precision floating-point number is a type of numerical value that can store a wide range of values with a high degree of precision. They are often used to represent decimal values in programs and are typically stored in 8 bytes (64 bits) of memory.

    double x = 3.14159;

    double y = 1.0 / 3.0;


    if-else keyword: if-else keyword is used to create a conditional statement that allows a program to execute different code blocks depending on whether a specified condition is true or false.

    if (x > 0) {

      // code block to execute if x is greater than 0

    } else {

      // code block to execute if x is not greater than 0

    }


    In the example above, the condition being tested is x > 0, which means "if x is greater than 0". If this condition is true, the code block inside the first set of curly braces ({ }) will be executed. If the condition is false, the code block inside the second set of curly braces (else { }) will be executed instead.

    enum keyword: In C++, the enum keyword is used to define an enumerated type, which is a user-defined data type that consists of a set of named constants called enumerators.

    enum Color { RED, GREEN, BLUE };


    int main() {

      Color c = RED;

      if (c == RED) {

        cout << "The color is red." << endl;

      }

      return 0;

    }


    In the example above, the enum keyword is used to define an enumerated type called Color, which consists of three enumerators: RED, GREEN, and BLUE. These enumerators can be used like variables of the Color type, and their values are assigned automatically based on the order in which they are listed. In this case, RED is assigned the value 0, GREEN is assigned the value 1, and BLUE is assigned the value 2.

    C++ extern Keyword

    extern keyword: extern keyword is used to declare a variable or function that is defined in another source file. It is typically used to specify that a variable or function is defined in a separate object file or library, rather than being defined in the current source file.

    // data.h

    extern int x;


    // data.cpp

    int x = 42;



    In the example above, the global variable x is defined in the source file foo.cpp, but it is declared using the extern keyword in the header file data.h. This allows other source files that include data.h to access the value of x without having to include the definition of x in their own source code.

    float keyword: float keyword is used to declare a variable of type float, which represents a single-precision floating-point number. A single-precision floating-point number is a type of numerical value that can store a wide range of values with a moderate degree of precision. They are often used to represent decimal values in programs and are typically stored in 4 bytes (32 bits) of memory.

    float x = 3.14159;

    float y = 1.0 / 3.0;


    for keyword: for keyword is used to create a loop that executes a block of code a specific number of times. The for loop is often used when you want to perform an operation on each element of an array or other data structure, or when you want to execute a block of code a specific number of times.

    for (int i = 0; i < 10; i++) {

      // code block to execute 10 times

    }


    go keyword: In programming languages, the goto keyword is used to transfer control to a labeled statement in the same function or block. It is often used as a way to jump to a specific point in the code and can be useful in certain circumstances, such as when you want to exit a loop or skip over a block of code.

    for (int i = 0; i < 10; i++) {

      if (i == 5) {

        goto skip;

      }

      cout << i << endl;

    }


    skip:

    cout << "Skipped the number 5." << endl;

    In the example above, the goto keyword is used to skip the number 5 when it is encountered in the loop. The loop iterates from 0 to 9, printing each number to the screen. However, when the loop reaches the number 5, it jumps to the labeled statement skip: using the goto keyword, and the number 5 is not printed.

    inline Keyword C++

    inline keyword: inline keyword is used to declare a function as an inline function. An inline function is a function that is expanded in place at the point of call, rather than being called through a function call. This can improve the performance of a program by reducing the overhead of calling a function, but it can also increase the size of the program if the inline function is called frequently.

    inline int max(int x, int y) {

      return (x > y) ? x : y;

    }


    int main() {

      int a = 10;

      int b = 20;

      int c = max(a, b);

      cout << c << endl;  // Output: 20

      return 0;

    }


    int keyword: int keyword is used to declare a variable of type int, which represents an integer value. An integer is a type of numerical value that represents a whole number, such as 0, 1, 2, 3, etc. In C++, integers are typically stored in 4 bytes (32 bits) of memory and can range in value from -2147483648 to 2147483647.

    int x = 42;

    int y = -1;

    int z = 0;


    long keyword: long keyword is used to declare a variable of type long, which represents a long integer value. A long integer is a type of numerical value that represents a whole number, similar to an int, but with a larger range of possible values. The size of a long integer value can vary depending on the programming language and the system it is running on, but it is typically stored in 4 bytes (32 bits) of memory and can range in value from -2147483648 to 2147483647.

    long x = 1234567890;

    long y = -9876543210;

    long z = 0;


    C++ register Keyword

    register keyword: register keyword is used to declare a variable as a register variable. A register variable is a type of variable that is stored in a processor's register rather than in main memory. This can improve the performance of a program by reducing the amount of time required to access the variable, as registers are typically faster to access than main memory.

    register int x = 42;

    register int y = 10;


    int main() {

      int z = x + y;

      cout << z << endl; 

      return 0;

    }


    **********OUTPUT**********

    52


    In the example above, the variables x and y are declared as register variables using the register keyword. When these variables are accessed in the main function, they will be stored in a processor's register, rather than in main memory, which can improve the performance of the program.

    return keyword: return keyword is used to exit a function and return a value to the calling function. It is typically used at the end of a function to specify the value that the function should return to the calling code.

    int add(int x, int y) {

      int z = x + y;

      return z;

    }


    int main() {

      int a = 10;

      int b = 20;

      int c = add(a, b);

      cout << c << endl;  

      return 0;

    }


    **********OUTPUT**********

    30


    short keyword: short keyword is used to declare a variable of type short, which represents a short integer value. A short integer is a type of numerical value that represents a whole number, similar to an int, but with a smaller range of possible values. The size of a short integer value can vary depending on the programming language and the system it is running on, but it is typically stored in 2 bytes (16 bits) of memory and can range in value from -32768 to 32767.

    short x = 12345;

    short y = -9876;

    short z = 0;


    signed keyword: signed keyword is used to declare a variable as a signed integer. A signed integer is a type of numerical value that represents a whole number, such as 0, 1, 2, 3, etc., but it can also represent negative values, such as -1, -2, -3, etc. The signed keyword is used to specify that a variable can store both positive and negative values.

    signed int x = 42;

    signed int y = -1;

    signed int z = 0;


    sizeof keyword: sizeof keyword is used to determine the size, in bytes, of a data type or variable. It is often used to determine the amount of memory that is required to store a specific data type or variable, or to ensure that a variable is large enough to hold a particular value.

    sizeof(int) = 4 bytes

    sizeof(double) = 8 bytes

    sizeof(x) = 4 bytes


    static keyword: static keyword is used to declare a static variable or a static member function. A static variable is a variable that is shared by all instances of a class, rather than being specific to a single instance. A static member function is a member function that is shared by all instances of a class, rather than being specific to a single instance.

    class Example {

     public:

      static int x;

      static int y;

    };


    int Example::x = 10;

    int Example::y = 20;


    int main() {

      Example e1;

      Example e2;

      cout << e1.x << endl; 

      cout << e2.y << endl;  

      return 0;

    }


    **********OUTPUT**********

    10

    20


    struct keyword: struct keyword is used to declare a structure, which is a composite data type that can contain a variety of data types, such as integers, floats, characters, and pointers. Structures are often used to store related data in a single unit, such as the data for a record in a database, or the data for an object in a program.

    struct Student {

      string name;

      int age;

      float grade;

    };


    int main() {

      Student s1;

      s1.name = "Ravi";

      s1.age = 18;

      s1.grade = 3.8;


      Student s2;

      s2.name = "Bob";

      s2.age = 19;

      s2.grade = 3.5;


      cout << s1.name << " is " << s1.age << " years old and has a grade of " << s1.grade << endl;

      cout << s2.name << " is " << s2.age << " years old and has a grade of " << s2.grade << endl;

      return 0;

    }


    switch keyword: switch keyword is used to create a switch statement, which is a type of control statement that allows you to execute different pieces of code based on the value of a given expression. The switch statement is often used as an alternative to a series of if-else statements, as it can make your code more readable and easier to maintain.

    int x = 2;


    switch (x) {

      case 1:

        cout << "x is 1" << endl;

        break;

      case 2:

        cout << "x is 2" << endl;

        break;

      case 3:

        cout << "x is 3" << endl;

        break;

      default:

        cout << "x is not 1, 2, or 3" << endl;

        break;

    }


    typedef keyword: typedef keyword is used to create an alias for a data type. It allows you to give a new name to an existing data type, which can make your code more readable and easier to understand.

    typedef int Integer;


    Integer x = 10;

    Integer y = 20;


    cout << x + y << endl; 


    **********OUTPUT**********

    30


    union keyword: union keyword is used to declare a union, which is a composite data type that can contain multiple data types, but only one data member can be active at a time. Unions are similar to structures, but they are typically smaller in size, as they only store one data member at a time.

    union Data {

      int i;

      float f;

      char c;

    };


    int main() {

      Data d;

      d.i = 10;

      cout << d.i << endl;  // Output: 10

      d.f = 3.14;

      cout << d.f << endl;  // Output: 3.14

      d.c = 'A';

      cout << d.c << endl;  // Output: A

      return 0;

    }


    unsigned keyword: unsigned keyword is used to declare an unsigned integer. An unsigned integer is a type of numerical value that represents a whole number, such as 0, 1, 2, 3, etc., but it can only store non-negative values, such as 0, 1, 2, 3, etc. The unsigned keyword is used to specify that a variable can only store non-negative values.

    unsigned int x = 42;

    unsigned int y = 0;

    unsigned int z = UINT_MAX;


    void keyword: void keyword is used to specify that a function or method does not return a value. It is often used as the return type for functions or methods that perform an action or perform a task, but do not need to return a result to the caller.

    void printMessage() {

      cout << "Hello, world!" << endl;

    }


    void main() {

      printMessage(); 

      return 0;

    }


    **********OUTPUT**********

    Hello, world!


    volatile keyword: volatile keyword is used to declare a volatile variable. A volatile variable is a type of variable that may change unexpectedly, and therefore cannot be optimized by the compiler.

    The volatile keyword is often used in programs that interact with hardware or devices, as it allows the compiler to generate code that accesses the variable directly from memory, rather than optimizing the access by storing the value in a register. This can be useful when the value of a volatile variable may change unexpectedly due to external factors, such as a hardware interrupt or a device register.

    volatile int x;


    void readValue() {

      x = readFromHardware();

    }


    int main() {

      readValue();

      cout << x << endl;

      return 0;

    }


    while keyword: while keyword is used to create a while loop, which is a type of control structure that allows you to repeatedly execute a block of code as long as a certain condition is true. The while loop is often used to perform an action or task repeatedly until a certain condition.

    int x = 0;

    while (x < 10) {

      cout << x << endl;

      x++;

    }


    friend keyword: friend keyword is used to declare a friend function or a friend class. A friend function is a function that has access to the private and protected members of a class, even though it is not a member of the class. A friend class is a class that has access to the private and protected members of another class, even though it is not a member of the class.

    The friend keyword is used to grant special privileges to a function or a class, allowing them to access the private and protected members of a class that they would not normally have access to. This can be useful when you want to allow a function or a class to perform certain operations on the members of another class, but you don't want to make those members publicly available.

    class Example {

     private:

      int x;

      int y;


     public:

      Example(int x, int y) : x(x), y(y) {}


      friend int add(const Example& e);

    };


    int add(const Example& e) {

      return e.x + e.y;

    }


    int main() {

      Example e(10, 20);

      cout << add(e) << endl;

      return 0;

    }


    **********OUTPUT**********

    30


    virtual keyword: virtual keyword is used to declare a virtual function or a virtual destructor. A virtual function is a member function that can be overridden by derived classes, allowing them to provide their own implementation of the function. A virtual destructor is a destructor that can be overridden by derived classes, allowing them to perform additional cleanup tasks when an object is destroyed.

    The virtual keyword is used to indicate that a function or destructor can be overridden by derived classes, which can be useful when you want to allow derived classes to customize the behavior of the base class in some way.

    class Shape {

     public:

      virtual double area() const = 0;  // Pure virtual function

    };


    class Circle : public Shape {

     private:

      double radius;


     public:

      Circle(double radius) : radius(radius) {}


      double area() const override {  // Overrides the virtual function in the base class

        return 3.14 * radius * radius;

      }

    };


    int main() {

      Circle c(10);

      cout << c.area() << endl;  // Output: 314

      return 0;

    }


    **********OUTPUT**********

    314


    public keyword: In object-oriented programming languages, the public keyword is used to specify the visibility of class members. Class members that are declared as public are accessible from anywhere, both within the class and outside the class.

    The public keyword is one of the three access specifiers in object-oriented programming languages, along with private and protected. The public keyword is used to make class members visible to all other classes and functions, while the private keyword is used to make class members visible only within the class, and the protected keyword is used to make class members visible only within the class and its derived classes.

    class Example {

     public:

      int x;  // Public member

      int y;  // Public member


     private:

      int z;  // Private member

    };


    int main() {

      Example e;

      e.x = 10;  // Valid

      e.y = 20;  // Valid

      e.z = 30;  // Invalid (private member)

      return 0;

    }


    this Keyword in C++

    this keyword: this keyword is a special keyword that refers to the current object within a class method or constructor. It is often used to distinguish between class members and local variables with the same name, or to pass a reference to the current object to a function or method.

    class Example {

     private:

      int x;

      int y;


     public:

      Example(int x, int y) : x(x), y(y) {}


      void setX(int x) {

        this->x = x;  // Assigns value to the x member of the object

      }


      void print() {

        cout << "x = " << x << ", y = " << y << endl;

      }

    };


    int main() {

      Example e(10, 20);

      e.setX(5);

      e.print();  // Output: "x = 5, y = 20"

      return 0;

    }


    **********OUTPUT**********
    x=5
    y=20

    try keyword: try keyword is used to define a block of code that may throw an exception. An exception is a special type of runtime error that can be handled by the program, rather than causing the program to terminate unexpectedly.

    The try keyword is used in conjunction with the catch keyword to create a try-catch block, which allows you to catch and handle exceptions that are thrown within the try block. The try block contains the code that may throw an exception, and the catch block contains the code that will be executed if an exception is thrown.

    #include <iostream>

    #include <stdexcept>


    int divide(int x, int y) {

      if (y == 0) {

        throw std::invalid_argument("divide by zero");

      }

      return x / y;

    }


    int main() {

      try {

        int result = divide(10, 0);

        std::cout << result << std::endl;

      } catch (const std::exception& e) {

        std::cerr << "Error: " << e.what() << std::endl;

      }

      return 0;

    }


    Faq Data Types Questions

    Q.1 What are keywords in C++?
    Ans: In C++, keywords are predefined identifiers that have special meaning in the language and cannot be used as names for variables, functions, or any other user-defined identifiers.

    Q.2 What are basics of C++?
    Ans: C++ is a general-purpose, object-oriented programming language that was developed in 1979 by Bjarne Stroustrup. It is an extension of the C programming language and provides additional features such as object-oriented programming, exception handling, and templates.

    Q.3 What are the 4 key aspects of C++?
    Ans: C++ is a programming language that has a number of key aspects, which include:

    Object-oriented programming: C++ supports object-oriented programming, which is a programming paradigm that focuses on organizing code around objects rather than actions and data.

    Type safety: C++ is a statically-typed language, which means that variables must be declared with a specific type, and the type of a variable cannot be changed after it is declared. This helps to prevent errors by ensuring that variables are used consistently throughout the code.

    Low-level programming: C++ is a high-level language, but it also provides many low-level features that allow programmers to directly manipulate memory and hardware. This makes it a good choice for writing systems-level code, such as operating systems or device drivers.

    Performance: C++ is known for its efficiency and performance, as it allows programmers to write code that is close to the machine level. This makes it a good choice for applications that require a lot of computation or that need to operate at a high speed.

    Q.4 Is coding C++ easy?
    Ans: C++ is a programming language that is considered to be relatively challenging to learn and use, especially for those who are new to programming. It has a steep learning curve and requires a good understanding of computer science concepts, such as data types, algorithms, and memory management.

    However, once you have learned the fundamentals of C++ and become proficient in the language, it can be a very powerful and efficient tool for solving a wide range of problems. Many experienced programmers consider C++ to be a valuable asset in their toolkit, and it is used in a variety of applications, including operating systems, desktop applications, and high-performance computing.

    Q.5 Can you learn C++ in 2 months?
    Ans: It is possible to learn C++ in two months, but it will depend on your prior experience with programming and your dedication to learning the language. If you are already familiar with programming concepts and have experience with other languages, you may be able to learn C++ more quickly. However, if you are new to programming, it may take longer to learn C++ and become proficient in the language.

    In general, learning any programming language requires a significant time investment, as it involves learning new concepts, practicing with exercises and projects, and continuously improving your skills. To learn C++ in two months, you will need to be dedicated and consistently set aside time to study and practice.

    Post a Comment

    0 Comments