Identifiers in C++: Differences between keywords and identifiers

Identifiers in C++: Differences between keywords and identifiers

18 Apr 2024
Beginner
4.6K Views
4 min read
Learn via Video Course & by Doing Hands-on Labs

C++ Programming For Beginners

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 underscore The 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
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this