Keywords and Identifiers in C++

Keywords and Identifiers in C++

Keywords and identifiers in the C++ programming language are special reserved words or basic program statements that demonstrate the core of software development design in C++ programming. Keywords serve specific program purposes, and identifiers identify value data or information within a program.

Keywords and Identifiers in C++

So, let’s explore keywords and identifiers in the C++ programming language in detail.

Keywords in C++/Reserved word.

Keywords in the C++ programming language are reserved specific-purpose words or statements. Keywords in C++ are used for a specific, unique purpose in a program. They cannot be used as declared identifiers (names of program variables, user-defined functions, or other user-defined parameter items) in a program. Identifiers represent the syntax of the C++ language’s layout structure. Keywords in C++ programming are pre-reserved or defined during software development. Remember, every C++ keyword is case-sensitive. This means that lowercase and uppercase versions of keywords applied in C++ are treated in separate orders. For example, int and Int are defined separately in lowercase and uppercase.

The C++ language has some built-in reserved keywords stored or included since its development. The use and purpose of these keywords in a program are pre-defined or fixed, and C++ users cannot use or apply them for any purpose other than their defined role.

List of popular C++ language keywords.

As of the C++20 language version, these are some popular reserved keywords already stored.

C++ control flow keywords.

if, else, switch, case, default, break, continue, return, goto, etc.

Data Types Reserved Keywords.

int, char, float, double, bool, void, wchar_t, long, short, unsigned, signed, long long, long double, unsigned long, unsigned int, unsigned short, unsigned long long, etc.

Storage Class Reserved Keywords.

static, extern, register, mutable, thread_local,

Data Type Modifiers Reserved Keywords.

const, volatile, inline, strict,

Access Control Reserved Keywords.

public, private, protected,

Exception Handling Task Reserved Keywords.

try, catch, throw,

Function and variable declarations Reserved Keywords.

typedef, using, inline, explicit, virtual, friend, consteval, constinit,

Memory Management Reserved Keywords.

new, delete, new[], delete[],

Namespaces and class/struct/union definitions Reserved Keywords.

In the C++ language, namespaces, class, struct, union, enum, template, typename, explicit, constexpr, align, alignoff, etc. are user-defined custom data types or reserved keywords.

Contexpr and constinit are reserved keywords in C++11 and later.

constexpr, constinit, noexcept, etc.

Other C++ most-used reserved keywords.

sizeof, sizeof…, decltype, typeid, static_assert, export, friend, etc.

C++ version 11/14/17/20 /new keywords/reserved keywords.

constexpr, require, co_await, co_return, co_yield, alignof, consteval, constinit, noexcept, require, typename, etc.

Examples of basic keywords in C++ programming.

int main() {

int integer = 1; // Here the ‘int’ data type is a reserved keyword

if (integer > 0) { // Here the ‘if’ statement is a reserved keyword in C++

return 0; // Here the ‘return’ statement is a reserved keyword, which returns a value to the program

}

}

Important things about C++ program keyword declaration usage.

  • Reserved – Keywords in C++ programming software are pre-defined for a reserved or fixed unique purpose, and they cannot be used as multiple identifiers (names of user-declared parameters, variables, functions, etc.) in a program.
  • Not redefinable – C++ keywords have a pre-defined unique program use purpose, and they cannot be defined or reused multiple times in a program.
  • Not case-sensitive – Remember, declare-use keywords in a C++ program are always declared and used in lowercase character format. For example, int, if, return, void, for, switch, etc.

Identifier concept in C++.

In C++ programming, an identifier is a pre-defined reserved text statement. An identifier is used to identify a variable, function, array, class, object, or any other type of user-defined data item in a C++ program. An identifier is used to identify a specific entity in a specific order within a C++ program.

Rules for creating identifiers in C++.

Identifiers in C++ programs start with a lowercase character or an underscore. Identifiers in any C++ program must always begin with a lowercase or uppercase character (a to z or A to Z) or an underscore (_), followed by a character, digit, numeric character (0 to 9), or underscore when declared or defined.

Correct C++ program identifier declaration.

testVariable

_help_1

totalFunction

id

Incorrect C++ program identifier declaration.

  • 987pqr – The variable identifier here starts with a digit.
  • !testVar – A special character ! is used in this declaration.
  • No keyword – Always remember that an identifier can never be a keyword in a C++ program. Such as int, class, return, void, for, if, etc.
  • Case-sensitive – C++ programming is completely case-sensitive in nature, which means that all program-declared variables like EmployeeVariable, employeeVariable, and EMPLOYEEVARIABLE are treated as separate identifiers.
  • No length limit – As in C++ programs, there is no fixed or strict limit on the length of declared identifiers. However, identifiers should be short and in the proper order according to the program purpose. Many older C++ compiler software may have a limit on identifier declarations, such as a maximum of 31 characters.
  • No special characters – In C++ programming, special characters like !, @, $, or # can never be used as variables when declaring user-defined identifiers.

Examples of identifiers in C++ programming.

int main() {

int integer = 3; // Here ‘integer’ is a user-defined identifier variable

float temp = 98.89; // Here ‘temp’ is a user-defined identifier

string text = “Welcome to Vcanhelpsu”; // Here ‘text’ is a user-defined identifier

}

Types of identifiers in C++ programming.

Variable identifiers – These are used to declare or represent user-defined variables in a C++ program.

Example – int p = 7;

Function identifiers – These are used for user-defined function variable names in a C++ program.

Example – void totalValue() { /* declare function code here */ }

Class/Structure/Union identifiers – These are used in C++ programs to declare a class, structure, or union (custom user-defined data type variable element).

Example – class EmployeeClass { /* declare class code here */ };

Object identifiers – These are used in C++ programs to declare or define an object or instance of a class.

Example – TestClass obj;

Namespace identifiers – These are used to define and access declared namespaces in C++ programs.

Example – namespace ValueNamespace { /* declare namespace code here */ }

Examples of common invalid C++ identifiers.

Using keywords as C++ identifiers.

int int = 9; // This displays an error because ‘int’ is a reserved keyword. It cannot be used as an identifier in a program.

Identifiers starting with digits in a C++ program.

int 1stEmployee_id = 101; // This displays an error because identifiers in program variables can never start with digits.

Identifiers with special C++ characters.

int test!employee_id = 109; // This displays an error because the ‘!’ special character symbol is not allowed in identifiers in program variables.

Leave a Reply