Tokens in C++ क्या हैं? Types, Examples और Complete Notes
C++ में program लिखते समय हम अलग-अलग keywords, names, numbers, symbols और operators का इस्तेमाल करते हैं। लेकिन क्या आपने कभी सोचा है कि Compiler पूरे C++ program को समझता कैसे है? Compiler के लिए program एक बड़ी line नहीं होता, बल्कि वह उसे छोटे-छोटे meaningful parts में बाँटकर समझता है। इन्हीं छोटे parts को Tokens कहा जाता है।
अगर आप C++ सीख रहे हैं, तो Tokens को समझना बहुत जरूरी है क्योंकि यही C++ program की basic building blocks हैं। चाहे आप variable बनाएं, calculation करें, condition लगाएं या function लिखें—हर जगह किसी न किसी Token का इस्तेमाल होता है।
इस article में Tokens in C++ को Class 8–10 के student के level से समझाया गया है, ताकि आपको definition याद करने के बजाय इसका actual use समझ आए।
Token क्या होता है?
Token C++ program की सबसे छोटी meaningful unit होती है, जिसे Compiler पहचान और समझ सकता है। आसान भाषा में कहें तो किसी C++ statement को छोटे-छोटे meaningful pieces में बाँटने पर मिलने वाले parts Tokens कहलाते हैं।
उदाहरण के लिए:
int age = 18;
इस statement में कई अलग-अलग Tokens हैं:
- int – Keyword
- age – Identifier
- = – Operator
- 18 – Constant
- ; – Special Symbol / Punctuator
इस तरह एक छोटी सी C++ statement में भी कई प्रकार के Tokens मौजूद हो सकते हैं।
C++ में Tokens के मुख्य प्रकार
C++ में सामान्य रूप से Tokens को निम्न categories में समझा जाता है:
- Keywords
- Identifiers
- Constants
- String Literals
- Operators
- Special Symbols / Punctuators
अब हर type को example के साथ समझते हैं।
1. Keywords in C++
Keywords वे reserved words हैं जिनका C++ में पहले से एक निश्चित meaning होता है। Programmer इनका इस्तेमाल अपनी इच्छा से किसी दूसरे purpose के लिए नहीं कर सकता।
कुछ common C++ Keywords हैं:
- int
- float
- char
- double
- if
- else
- for
- while
- return
- class
- public
- private
उदाहरण:
int marks = 90;
यहाँ int एक Keyword है। यह Compiler को बताता है कि marks नाम का variable integer type का है।
ध्यान रखने वाली बात
Keyword को variable या function का नाम नहीं बनाया जा सकता। उदाहरण के लिए int, class या return को identifier की तरह इस्तेमाल करना गलत होगा।
2. Identifiers in C++
Identifier वह नाम है जिसे programmer variables, functions, classes या दूसरे program elements को पहचानने के लिए देता है।
उदाहरण:
int age;
float salary;
char grade;
यहाँ age, salary और grade Identifiers हैं।
Identifier बनाने के Rules
- Identifier में letters, digits और underscore (
_) का इस्तेमाल किया जा सकता है। - Identifier की शुरुआत digit से नहीं हो सकती।
- Space का इस्तेमाल नहीं किया जा सकता।
- Keyword को identifier नहीं बनाया जा सकता।
- C++ में identifiers case-sensitive होते हैं।
उदाहरण:
studentName
total_marks
age1
ये valid identifiers हैं। जबकि 1age और student name valid identifiers नहीं हैं।
3. Constants
Constant वह fixed value है जिसका program में कोई निश्चित value होता है। सामान्य तौर पर integer, floating-point, character और दूसरे literal values को इस category में समझा जाता है।
उदाहरण:
int age = 18;
float price = 99.50;
char grade = 'A';
यहाँ 18, 99.50 और 'A' values हैं।
Constants के examples
- Integer Constant: 10, 25, 100, -50
- Floating Constant: 10.5, 3.14, -2.5
- Character Constant: 'A', 'b', '5'
ध्यान रखें कि 5 और '5' एक जैसे नहीं हैं। 5 एक numeric value है, जबकि '5' एक character है।
4. String Literals
Double quotation marks के अंदर लिखे गए characters के group को String Literal कहा जाता है।
"Hello"
"Welcome to C++"
"Computer Science"
उदाहरण:
cout << "Hello World";
इस statement में "Hello World" एक String Literal है।
String और Character में फर्क याद रखें:
'A'→ Character"A"→ String Literal
5. Operators
Operators वे symbols हैं जिनका इस्तेमाल calculation, comparison, assignment या logical operations करने के लिए किया जाता है।
Arithmetic Operators
+ - * / %
उदाहरण:
int a = 10;
int b = 3;
int sum = a + b;
int remainder = a % b;
यहाँ + और % Operators हैं।
Relational Operators
== != > < >= <=
इनका इस्तेमाल दो values की तुलना करने के लिए होता है।
if (age >= 18)
{
cout << "Eligible";
}
Logical Operators
&& || !
इनका इस्तेमाल multiple conditions को combine करने के लिए किया जाता है।
Assignment Operator
=
उदाहरण:
int marks = 80;
यहाँ = assignment operator है। यह marks को 80 value assign करता है।
6. Special Symbols और Punctuators
C++ program में कुछ symbols program की structure को define करने में मदद करते हैं। इन्हें Special Symbols या Punctuators के रूप में समझा जाता है।
उदाहरण:
; , ( ) { } [ ] #
Semicolon (;) C++ में statement के अंत को बताने के लिए बहुत महत्वपूर्ण है।
int age = 20;
यहाँ semicolon statement को समाप्त करता है।
Parentheses ( ) functions और conditions में इस्तेमाल होते हैं।
cout << "Hello";
Curly Braces { } किसी block of statements को दर्शाती हैं।
if (age >= 18)
{
cout << "Adult";
}
C++ Token का आसान Diagram
किसी statement को Tokens में इस तरह समझ सकते हैं:
int studentAge = 15 ;
| | | | |
Keyword Identifier Operator Constant Punctuator
Compiler इसी तरह program के अलग-अलग meaningful elements को पहचानकर आगे processing करता है।
एक Complete Example से Tokens समझें
#include <iostream>
using namespace std;
int main()
{
int age = 16;
cout << "Age = " << age;
return 0;
}
इस program में कई प्रकार के Tokens हैं:
intऔरreturn→ Keywordsmainऔरage→ Identifiers16और0→ Constants"Age = "→ String Literal=और<<→ Operators( ),{ }और;→ Punctuators/Special Symbols
Real-Life Example से Tokens समझें
मान लीजिए आप किसी दुकान पर जाते हैं और कहते हैं—“दो notebooks की कीमत 100 रुपये है।” इस sentence को अलग-अलग meaningful parts में बाँटा जा सकता है। Programming में भी यही concept लागू होता है। Compiler पूरे program को एक साथ याद नहीं करता; वह उसे छोटे meaningful elements में समझता है।
इसलिए Tokens को C++ language की building blocks कहना गलत नहीं होगा।
Tokens और White Space
Space, Tab और New Line को सामान्यतः program की readability के लिए इस्तेमाल किया जाता है। कई जगह Compiler इनके बीच अंतर नहीं करता, लेकिन Tokens को अलग-अलग पहचानने में whitespace मदद कर सकता है।
उदाहरण के लिए:
int age = 18;
इसे ज्यादा readable तरीके से भी लिखा जा सकता है:
int age = 18;
दोनों का meaning समान है। लेकिन अच्छी coding practice के लिए unnecessary spaces से बचना बेहतर है।
Common Mistakes in Tokens
1. Keyword को Identifier बनाना
int class = 10;
यह गलत है क्योंकि class एक Keyword है।
2. Identifier की शुरुआत digit से करना
int 2marks = 90;
यह valid identifier नहीं है। इसे इस तरह लिखा जा सकता है:
int marks2 = 90;
3. Character और String को confuse करना
char grade = "A";
यह गलत है। Character के लिए single quotes इस्तेमाल करें:
char grade = 'A';
4. Semicolon भूल जाना
int age = 18
यह statement incomplete है। सही form:
int age = 18;
Expert Tips
- Keywords और Identifiers को हमेशा अलग समझें।
- Identifier के नाम meaningful रखें, जैसे
studentNameयाtotalMarks। - Character के लिए single quotes और String के लिए double quotes इस्तेमाल करें।
- Operators का meaning examples के साथ सीखें, केवल list याद न करें।
- हर C++ statement में semicolon की जरूरत है या नहीं, यह syntax के अनुसार check करें।
- Token की पहचान करने की practice करें—यह आगे Compiler और Syntax समझने में बहुत मदद करेगी।
Key Points – Short Notes
- Token C++ program की smallest meaningful unit है।
- Keywords का meaning C++ में predefined होता है।
- Identifiers programmer द्वारा दिए गए names होते हैं।
- Constants fixed values को represent करते हैं।
- String Literals double quotes में लिखे जाते हैं।
- Operators operations perform करते हैं।
- Punctuators program की structure और statements को व्यवस्थित करते हैं।
Interview Questions on Tokens in C++
- Token क्या है?
Token C++ program की smallest meaningful unit है जिसे Compiler पहचान सकता है। - C++ Tokens के मुख्य types कौन-कौन से हैं?
Keywords, Identifiers, Constants, String Literals, Operators और Special Symbols/Punctuators। - क्या Keyword को Identifier बनाया जा सकता है?
नहीं, क्योंकि Keywords reserved words होते हैं। - क्या C++ में identifiers case-sensitive होते हैं?
हाँ।Ageऔरageअलग identifiers हैं। - 'A' और "A" में क्या अंतर है?
'A'Character Literal है, जबकि"A"String Literal है। - Semicolon का क्या काम है?
यह कई C++ statements के अंत को दर्शाता है।
MCQs – Tokens in C++
1. इनमें से कौन सा Keyword है?
A) student B) age C) int D) marks
उत्तर: C) int
2. इनमें से valid Identifier कौन सा है?
A) 2name B) student_name C) student name D) class
उत्तर: B) student_name
3. "Hello" क्या है?
A) Keyword B) Operator C) String Literal D) Identifier
उत्तर: C) String Literal
4. इनमें से Arithmetic Operator कौन सा है?
A) + B) && C) == D) !
उत्तर: A) +
5. C++ में comparison के लिए कौन सा operator इस्तेमाल होता है?
A) = B) == C) += D) ++
उत्तर: B) ==
Practical Exercises
अब खुद practice करके Tokens की पहचान करें। नीचे दिए गए statements में प्रत्येक Token का type पहचानने की कोशिश करें।
int marks = 95;float price = 25.5;char grade = 'B';if (age >= 18)cout << "Welcome";
Practice करते समय पहले Keyword, फिर Identifier, Constant, Operator और Punctuator को अलग-अलग mark करें। कुछ ही examples के बाद यह काम अपने आप आसान लगने लगेगा।
Mini Quiz
Question: इस statement में कितने प्रमुख Tokens दिखाई दे रहे हैं?
int total = marks + 10;
Answer: int, total, =, marks, +, 10 और ; — कुल 7 lexical elements हैं।
अब इसी तरह अपनी कोई C++ statement लेकर उसमें Tokens की पहचान करें। यही सबसे अच्छी practical practice है।
Quick Revision Notes
| Token Type | Example | काम |
|---|---|---|
| Keyword | int | Predefined meaning |
| Identifier | age | नाम बताता है |
| Constant | 18 | Fixed value |
| String Literal | "Hello" | Text को represent करता है |
| Operator | + | Operation करता है |
| Punctuator | ; | Program structure में मदद करता है |
FAQs – Tokens in C++
1. C++ में Token क्या है?
Token C++ source code की सबसे छोटी meaningful unit है। Compiler program को समझने के लिए उसके अलग-अलग lexical elements को पहचानता है। Keywords, identifiers, literals, operators और punctuators इसके सामान्य examples हैं।
2. C++ में कितने प्रकार के Tokens होते हैं?
Basic C++ learning में Tokens को Keywords, Identifiers, Constants, String Literals, Operators और Special Symbols/Punctuators में समझाया जाता है। अलग-अलग textbooks classification को थोड़ा अलग तरीके से प्रस्तुत कर सकती हैं।
3. क्या variable का नाम Keyword हो सकता है?
नहीं। Keyword C++ language द्वारा reserved होता है। इसलिए int int; या int class; जैसे declarations valid नहीं हैं।
4. Identifier और Keyword में क्या difference है?
Keyword का meaning language में पहले से तय होता है, जबकि Identifier programmer द्वारा किसी variable, function या class को दिया गया नाम होता है। उदाहरण के लिए int Keyword है और marks Identifier है।
5. क्या space एक Token है?
सामान्य C++ token classification में whitespace को meaningful token की तरह नहीं गिना जाता। Space, Tab और New Line मुख्यतः source code को अलग और readable बनाने में मदद करते हैं।
6. 'A' और "A" में क्या अंतर है?
'A' एक character literal है, जबकि "A" एक string literal है। Character और String को C++ में अलग तरीके से handle किया जाता है।
7. क्या C++ में uppercase और lowercase identifiers अलग होते हैं?
हाँ। C++ case-sensitive language है। इसलिए Marks, marks और MARKS तीन अलग identifiers माने जाएंगे।
8. Tokens सीखना क्यों जरूरी है?
Tokens C++ syntax की foundation समझने में मदद करते हैं। जब आपको पता होता है कि किसी statement में कौन सा हिस्सा Keyword, Identifier, Constant या Operator है, तब syntax errors को समझना और program पढ़ना काफी आसान हो जाता है।
Conclusion
C++ में Tokens को समझना शुरुआत में छोटा topic लग सकता है, लेकिन programming की foundation मजबूत करने के लिए यह काफी useful है। किसी भी C++ program को ध्यान से देखें तो उसमें Keywords, Identifiers, Constants, String Literals, Operators और Punctuators का combination दिखाई देगा।
सबसे अच्छा तरीका यह है कि केवल definitions याद करने के बजाय रोज कुछ छोटी C++ statements लेकर उनके Tokens की पहचान करें। जैसे-जैसे practice बढ़ेगी, आपको program की structure और Compiler द्वारा source code को समझने का basic idea भी साफ होता जाएगा।
अगर आपको Tokens का concept समझ आ गया है, तो अगला step C++ में Keywords, Identifiers और Operators को examples और programs के साथ detail में practice करना होना चाहिए।

टिप्पणियाँ
एक टिप्पणी भेजें