- C++ Course Basics
- C++ Tutorial
- C++ Basic Syntax
- C++ Identifiers
- C++ Character Set
- C++ Input/Output Operator
- C++ Variables
- C++ Data Types
- C++ Formatting Output
- C++ Operators
- C++ Assignment Operator
- C++ Type Conversion
- C++ Program Control
- C++ if and if-else
- C++ switch
- C++ loops
- C++ break and continue
- C++ Functions
- C++ Functions
- C++ Prototype and Definition
- C++ Function Call
- C++ Function Types
- C++ Friend Function
- C++ Function Overloading
- C++ Arrays and Strings
- C++ Arrays
- C++ One-Dimensional Arrays
- C++ Strings
- C++ String Functions
- C++ Structures
- C++ Structures
- C++ Nested Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ Pointers
- C++ Pointers
- C++ Memory Map
- C++ Declare Initialize Pointers
- C++ Pointers and Structures
- C++ Object-Oriented
- C++ Object-Oriented
- C++ Classes and Objects
- C++ Constructors and Destructors
- C++ Objects as Function Arguments
- C++ Pointers and Objects
- C++ Data Structure
- C++ Linked List
- C++ Stack
- C++ Queues
- C++ File Handling
- C++ File Handling
- C++ Opening and Closing Files
- C++ Steps to Process Files
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Misc
- C++ typedef
- C++ #define
- C++ Date and Time
- C++ Examples
- C++ Examples
C++ Identifiers and Keywords with Examples
In this article, you will learn about "identifiers" and "keywords," the two most important topics when discussing C++ programming. So, without further ado, let us start with the identifiers.
What are identifiers?
Identifiers are the basic building blocks of a program. Identifiers are used as a general name given to different parts of the program, namely variables, objects, classes, functions, arrays, etc.
Since C++ is a case-sensitive language, therefore, "fresherearth," "fresherearth," "fresherearth," and "fresherearth" are all four different identifiers.
Before moving further, let's first understand the rules for creating an identifier in C++.
Rules to create or name an identifier in C++
To create an identifier in the C++ language, you need to follow the following rules:
- An identifier must begin with a letter from A to Z, a-z, or _ (an underscore character).
- Then we can add letters (A-Z, a-z), digits (0-9), and underscores to its name.
- Reserved words (keywords) are not allowed to be used as identifiers.
Following is a list of some valid identifiers. This list is included for your convenience because examples help to make the topic more understandable.
- codes_cracker
- Myvar
- _myvar
- _my34var_
- sum_of_two_numbers
Tip: You may either use underscore in variable names to separate parts of the name, such as last_name, first_name, and main_balance, or you may go for "capital style" notation, such as lastName, firstName, and mainBalance, i.e., capitalizing the first letter of the next word.
C++ Identifiers Example
The following C++ program is an example program of C++ identifiers that will give you an idea of how identifiers are used in C++ programming:
#include<iostream> using namespace std; int main() { char ch; char name[20]; int num; cout<<"Enter a character: "; cin>>ch; cout<<"You entered: "<<ch<<endl; cout<<"\nEnter your name: "; cin>>name; cout<<"Your name is: "<<name<<endl; cout<<"\nEnter a number: "; cin>>num; cout<<"You entered: "<<num<<endl; return 0; }
When the above C++ program is compiled and executed, it will produce the following output:
Now supply the input, say "c," as a character, and hit the "ENTER" key. Then supply another input, say "fresherearth," as a name, and again hit the "ENTER" key. The following snapshot shows the sample run of the above C++ example program.
The cout<<endl;
and cout<<"\n";
both insert a line break on the output console. You can refer to "endl" as
"end-line."
Keywords in C++
To a language compiler, certain words have a heightened significance, and these are known as keywords. Because these are reserved words that are used for specific purposes, you are not allowed to use them as regular identifier names.
I previously stated that reserved words or keywords may not be used as identifiers. As a result, it is critical to remember the list of keywords so that you do not accidentally use them as identifiers. As C++ keywords, keep the following list in mind:
- alignas
- decltype
- namespace
- struct
- alignof
- default
- new
- switch
- and
- delete
- noexcept
- template
- and_eq
- do
- not
- this
- asm
- double
- not_eq
- thread_local
- auto
- dynamic_cast
- nullptr
- throw
- bitand
- else
- operator
- true
- bitor
- enum
- or
- try
- bool
- explicit
- or_eq
- typedef
- break
- export
- private
- typeid
- case
- extern
- protected
- typename
- catch
- false
- public
- union
- char
- float
- register
- unsigned
- char16_t
- for
- reinterpret_cast
- using
- char32_t
- friend
- return
- virtual
- class
- goto
- short
- void
- compl
- if
- signed
- volatile
- const
- inline
- sizeof
- wchar_t
- constexpr
- int
- static
- while
- const_cast
- long
- static_assert
- xor
- continue
- mutable
- static_cast
- xor_eq
More Examples
« Previous Tutorial Next Tutorial »