Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Identifiers in C++: Differences between keywords and identifiers

Identifiers in C++: Differences between keywords and identifiers

21 May 2024
Beginner
8.2K Views
4 min read
Learn with an interactive course and practical hands-on labs

Free C++ Foundation Course: Learn C++ In 21 Days

Identifiers in C++: Overview

Understanding how to use identifiers in C++ can be a daunting task for any beginner. If you're a beginner in programming struggling to understand identifiers in C++, enroll in our C++ beginner course and get the guidance you need. Identifiers are essential concepts in C++. In this C++ tutorial, we'll look at understanding the common identifiers used in the C++ language, including their characteristics, examples, as well as important conventions.

What are Identifiers in C++?

Identifiers are names used to identify memory locations, functions in C++, and variables in C++. These names serve as labels to identify and access these elements within your C++ program.
  • It’s a collection of alphanumeric characters that begins with an alphabetical character or an underscore.
  • As they must be unique, they are case-sensitive.
  • There is a total of 63 alphanumerical characters that represent the identifiers.

Identifier Naming Rules in C++

  • It can only consist of letters (both uppercase and lowercase), digits, and underscores.
  • It must begin with a letter or an underscore. It cannot be a digit.
  • They are case-sensitive, which means uppercase and lowercase letters are considered different characters. E.g., "myVar" and "myvar" are two different identifiers.
  • They cannot be the same as any reserved words in the C++ language. Examples of reserved words in C++ include "if", "while", and "for".
  • Identifiers cannot contain any spaces or special characters such as @, #, $, %, ^, &, *, etc.

Examples of Valid Identifiers

total, sum, average, _m _, sum_1

Example of Invalid Identifiers

2sum (starts with a numerical digit) 
int (reserved word) 
char (reserved word) 
m+n (special character, i.e., '+')

Read More - Advanced C++ Interview Interview Questions and Answers

Types of Identifiers in C++

Types of Identifiers in C++

There are two types of identifiers in the C++ language

  1. Internal Identifiers

  • They aren’t used in external linkage. Ex. local variables
  • They are often used to assign significant meaning to portions of code that are shared between different functions in C++ language, classes, etc.
  • They make it easier to maintain, extend, and debug code in C++.
  • They discourage repetitive copy and paste of code between various program parts.
  • Internal identifiers for variable & function names improve the efficiency and structure of C++ development, resulting in clear, succinct, and error-free code.

  1. External Identifiers

  • Variables, constants, and functions that can be accessed outside of their scope in C++ are given distinctive names known as external identifiers.
  • These identifiers are essential for larger programs with several files and libraries since they ensure organization and proper reference of items outside of their original scope.
  • They are often written in lowercase and start with an underscore (_).

Differences between keywords and identifiers in C++

IdentifiersKeywords
Identifiers are mainly a particular kind of name that identifies elements of that program by the programmers.Keywords are mainly a particular type of word whose meaning is stored by compilers.
It contains digits, letters, and underscore.It contains letters only.
The name of the variable can be identified by identifiersThe types of entities in the program are specified by Keywords
The initial letter of the identifiers can be uppercase, lowercase, or underscoreThe initial letter of the keyword is started in lowercase
The only special character is used in identifiers are underscoreIn keywords, there are no special characters
There are two types of identifiers, those are internal and external identifiersThere are no classifications in keywords
Examples of identifiers are, results, sum, test, power, and many moreExamples of keywords are, if, for, break, else, and many more
Summary
Having reviewed the components of C++ programming, it is clear that identifiers are key elements to be familiar with when learning how to code in this language. These tools may appear daunting at first, however, over time they can become second nature. For hands-on and practical understanding, consider our C++ Certification Course.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this