×

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

How to Reverse a String in C++ using While Loop

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer...

6 minutes read.

Depth First Search Program to Traverse a Graph in C++

Depth First Search (DFS) is a technique that is used for transversing the graph. The process of Depth First Search (DFS) starts from the root node and then next comes...

6 minutes read.

C++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B. Example Input : a[] = {1, 2, 3, 5,...

3 minutes read.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

Template Specialization in C++

Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a...

4 minutes read.

Object Slicing in C++

In this article, we will learn about Object slicing. When an object from a derived class is assigned to an object from a base class in C++, these extra attributes...

3 minutes read.

C++ Switch

In C++, an expression or variable can be tested against a range of constant values using a switch statement, which is a control flow statement. It offers a productive method...

4 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

C++ Memory Management

Memory management is a method of controlling computer memory and allocating memory space to applications to increase overall system performance. What is the purpose of memory management? Because the array contains homogeneous...

4 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.

New Operator in C++

Dynamic memory allocation in C++ means manually allocating the memory by the developer duing run-time. The dynamic memory is allocated in the heap section of the RAM, whereas the static...

3 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.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.