×

C++ Keywords

In this article, we will discuss keywords in C++ with their several features and functions.

What are Keywords in C++?

In C++, a keyword is a reserved word that has a predefined meaning in the language and is not suitable for use as an identifier (such as class names, variable names, or function names). The syntax and organization of C++ programs depend heavily on keywords because they define control statements, data types, and other language features.

Keywords are explicitly reserved identifier of C++ language features. It cannot be used as user-define program elements or other name for the program variables.

Reserved Identifiers:

In C++, keywords are reserved, defined concepts with certain meanings. The compiler recognizes them and utilizes them to interpret the structure and behavior of a program. Because they are reserved, they cannot be used to name classes, variables, functions, or other user-defined elements.

For example, int, while, return, and class are all reserved keywords in C++.

C++ Language Features:

Every keyword in C++ stands for a fundamental aspect of the language. Here are several instances:

  1. Data types: int, char, float, double, bool.
  2. Control structures: if, else, switch, while, for, do.
  3. Memory management: new, delete.
  4. Object-oriented programming: class, public, private, protected.
  5. Exception handling: try, catch, throw.

Keywords cannot be used as identifiers:

Reserved terms cannot be used as variable, function, or class names. A compilation error will appear if we attempt to do this.

Incorrect Example Syntax:

int class = 10;  // Error: 'class' is a reserved keyword

Correct Usage:

int myClass = 10;  // Allowed, since 'myClass' is not a keyword         

C++ programming language has several reserved keywords used in the program. ANSI C++ also uses the keywords available in ANSI C programming language.

ANSI C:

Numerous C compilers had their own versions of the language prior to its standardization. When programs were transferred between systems, this resulted in portability problems. A standard version of C, ANSI C (also referred to as C89 or C90), was developed by ANSI in 1989 to solve this problem.

ANSI C is the standard version of the C programming language developed by the American National Standards Institute. This kind of C was developed to address issues that arose from various C programs that were written and compiled across a wide variety of systems.

Versions of ANSI C:

ANSI C (C89/C90) was the first standardized version of C (1989/1990).

  • C99: In C99, new features including inline functions, flexible array members, and new data types (long long, _Bool) were added.
  • C11: Multi-threading capabilities, improved Unicode handling, and safer standard library operations were introduced.
  • C17: A small update with a few bug fixes and small improvements.
  • C23: The latest iteration, with enhanced usability and safety.

Key Features of ANSI C (C89):

Several key features of ANSI C are as follows:

  • Standard Data Types: int, char, float, double, void.
  • Control Statements: if, else, switch, for, while, do.
  • Functions & Libraries: printf(), scanf(), math.h, stdlib.h.
  • Pointers & Memory Management: malloc(), free().
  • File Handling: fopen(), fclose(), fread(), fwrite().

Example code for ANSI C (C89) Standard Code:

#include <stdio.h>


// Function prototype (ANSI C Standard)

void greet();

int main() {

    int num = 10;  // Standard integer declaration

    printf("Number: %d
", num);  // ANSI C standard function


    greet();  // Calling the function


    return 0;  // ANSI C requires return type in main()

}

// Function definition

void greet() {

    printf("Hello, this is an ANSI C program!
");

}

Output:

Number: 10

Hello, this is an ANSI C program!

Explanation:

  • #include <stdio.h>: Standard input/output header.
  • printf(): Standard ANSI C function.
  • Function prototype (void greet();): ANSI C style.
  • return 0; in main(): Required in ANSI C.

Example Code for C++ Version:

#include <iostream>  // C++ I/O library

using namespace std;

// C++ class (Not available in ANSI C)

class Greeter {

public:

    void greet() {

        cout << "Hello, this is a C++ program!" << endl;

    }

};

int main() {

    int num = 10;  // Same as ANSI C

    cout << "Number: " << num << endl;  // C++ uses cout instead of printf

    Greeter obj;  // Object-oriented feature

    obj.greet();  // Calling class method

    return 0;

}

Output:

Number: 10

Hello, this is a C++ program!

Differences with the above program from ANSI C:

  • Uses <iostream> instead of <stdio.h>.
  • Uses cout instead of printf().
  • Introduces OOP (class Greeter), which ANSI C does not support.
  • Uses public access modifier inside a class.

ANSI C programming language keywords:

Auto Double Int struct
break Else Long switch
Case Enum Register typedef
Char Extern Return union
const Float Short Unsigned
continue For Signed Void
default Goto Sizeof Volatile
Do If Static While

Keywords added by ANSI C++ programming language:

Asm Private bool reinterpret_cast
catch Protected const_cast static_cast
Class Public dynamic_cast True
delete Template explicit Typeid
friend This export Typename
inline Throw false Using
New Try mutable wchar_t
operator Virtual namespace

Conclusion:

In conclusion, the C++ keywords are essential parts of the C++ language. Because these reserved words are already assigned meanings, they cannot be used as variable, function, or class names. C++ has all the keywords of ANSI C and additional ones like class, public, private, new, and delete, which are necessary for object-oriented programming, memory management, exception handling, and other features which forward and backward compatibility. It is because these keywords class, public, private, new, and delete, give the encapsulation and dynamic memory management, which is the hallmark of any powerful C++ software. In addition, these keywords make it easy to produce efficient and defect-free C++ applications because the keywords define how the compiler will interpret and implement the code.


Related Topics

C++ Inline function

A C++ function that extends in line when called is known as an inline function. It reduces function call overhead by having the compiler use the function code rather than...

4 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.

Ways to Copy a Vector in C++

Vectors in C++ are the same as arrays, along with additional outstanding features than them, like array lists in Java programming language. In Vectors, the size constraint is eliminated, which...

5 minutes read.

Passing a Vector to a function in C++

A pointer is sent to the function when we feed it an array. However, there are two ways to pass a vector: Pass by Value Pass by Reference A copy of a vector...

3 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.

Top 5 IDEs for C++ That You Should Try Once

In the past decades, creating an application or interface from the very basic idea, the developer has to struggle a lot for it. Because an application is a combination of...

3 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned...

4 minutes read.

Size_t Data Type in C++

In C++, the type to express the object size in bytes is defined as Size_t, an unsigned integer type offered by the standard library for describing the object's size and...

3 minutes read.

Multiple Inheritance

C++ Multiple Inheritance Multiple inheritance is such an inheritance in which a derived class inherits properties of more than one base class.     C++ Multiple Inheritance Example In this example, two base classes Square and...

2 minutes read.

C++ Namespaces

An Overview In each scope, a name can only represent one entity. As a result, there cannot be two independent variables with the similar names in the same scope, as this may cause...

10 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.