C++ Character Set and Tokens
The basic building blocks every C++ program is made of.
C++ Character Set and Tokens
C++ program लिखने के लिए कुछ निश्चित characters (अक्षरों और symbols) तथा language elements का उपयोग किया जाता है। C++ में program बनाने वाले सबसे छोटे meaningful units को Tokens कहा जाता है। इसलिए C++ को समझने के लिए Character Set और Tokens की जानकारी महत्वपूर्ण है।
Tokens: C++ program के smallest meaningful units
What is Character Set?
Character Set उन सभी valid characters का collection (समूह) है जिनका उपयोग C++ program को लिखने के लिए किया जा सकता है। इसमें letters, digits, special symbols और whitespace characters शामिल होते हैं।
C++ source code लिखते समय programmer इन्हीं characters और symbols का उपयोग करके keywords, identifiers, constants, operators और अन्य program elements बनाता है।
Categories of C++ Character Set
C++ character set को सामान्यतः निम्नलिखित categories में समझा जा सकता है:
- Letters
- Digits
- Special Characters
- White Space Characters
1. Letters
C++ program में English alphabet के uppercase और lowercase letters का उपयोग किया जाता है।
इनमें शामिल हैं:
Lowercase: a, b, c, d, ... z
Letters का उपयोग identifiers, keywords और अन्य language elements में किया जा सकता है।
Example:
#include <iostream>
using namespace std;
int main()
{
int marks = 80;
cout << "Marks = " << marks;
return 0;
}
Marks = 80
यहाँ marks एक identifier है जिसमें lowercase letters का उपयोग किया गया है।
2. Digits
C++ में decimal digits 0 से 9 तक उपयोग किए जाते हैं। Digits का उपयोग numeric constants और identifiers के कुछ हिस्सों में किया जा सकता है।
Example:
#include <iostream>
using namespace std;
int main()
{
int number1 = 25;
int number2 = 15;
cout << number1 + number2;
return 0;
}
40
यहाँ 25 और 15 numeric constants हैं, जबकि number1 और number2 identifiers हैं।
3. Special Characters
C++ में विभिन्न special characters और symbols का उपयोग program की syntax और operations को define करने के लिए किया जाता है।
| Character/Symbol | Common Use |
|---|---|
+ |
Addition operator |
- |
Subtraction operator |
* |
Multiplication operator |
/ |
Division operator |
= |
Assignment operator |
< और > |
Comparison तथा stream operators के विभिन्न contexts में उपयोग |
( ) |
Function calls और expressions के लिए |
{ } |
Code blocks के लिए |
[ ] |
Array subscripting आदि के लिए |
; |
Statement terminator |
, |
Items को separate करने के लिए |
" " |
String literals के लिए |
' ' |
Character literals के लिए |
# |
Preprocessor directives में |
_ |
Identifiers में उपयोग किया जा सकता है |
4. White Space Characters
Whitespace characters में space, tab और newline जैसे characters शामिल होते हैं। इनका उपयोग source code को readable और properly formatted बनाने के लिए किया जाता है।
उदाहरण:
int a = 10;
int b = 20;
ऊपर दिए गए code में spaces और newline का उपयोग code को व्यवस्थित रखने के लिए किया गया है।
What are Tokens?
Token C++ program की सबसे छोटी meaningful unit (अर्थपूर्ण इकाई) है जिसे compiler पहचान सकता है। जब compiler source code को analyze करता है, तो वह program को विभिन्न tokens में identify करता है।
उदाहरण:
int sum = a + b;
इस statement में कई अलग-अलग tokens हैं:
int → sum → = → a → + → b → ;
Types of C++ Tokens
C++ tokens को मुख्य रूप से निम्नलिखित categories में समझा जाता है:
- Keywords
- Identifiers
- Constants/Literals
- String Literals
- Operators
- Punctuators
1. Keywords
Keywords वे reserved words हैं जिनका C++ language में predefined meaning होता है। इन्हें programmer अपने variable या function के नाम के रूप में सामान्यतः use नहीं कर सकता।
कुछ common C++ keywords हैं:
Example:
#include <iostream>
using namespace std;
int main()
{
int age = 17;
if (age >= 18)
{
cout << "Adult";
}
else
{
cout << "Minor";
}
return 0;
}
Minor
ऊपर int, if, else और return keywords हैं।
2. Identifiers
Identifiers वे names हैं जिन्हें programmer variables, functions, classes और अन्य user-defined entities के लिए देता है।
उदाहरण:
int studentAge = 16;
int totalMarks = 450;
यहाँ studentAge और totalMarks identifiers हैं।
Rules for Naming Identifiers
- Identifier में letters, digits और underscore (
_) का उपयोग किया जा सकता है। - Identifier का पहला character digit नहीं हो सकता।
- Identifier में spaces का उपयोग नहीं किया जा सकता।
- Keywords को identifier के रूप में use नहीं किया जा सकता।
- C++ case-sensitive है, इसलिए
ageऔरAgeअलग identifiers हैं।
Valid Identifiers:
Invalid Identifiers:
3. Constants and Literals
Constants/Literals ऐसे fixed values हैं जो program में directly लिखे जाते हैं। इनके विभिन्न forms हो सकते हैं, जैसे integer, floating-point, character और Boolean literals।
Examples:
Floating-point: 10.5, 3.14
Character: 'A', '7'
Boolean: true, false
उदाहरण:
#include <iostream>
using namespace std;
int main()
{
int age = 17;
double percentage = 85.5;
char grade = 'A';
bool passed = true;
cout << age << endl;
cout << percentage << endl;
cout << grade << endl;
cout << passed;
return 0;
}
17
85.5
A
1
bool value को default stream formatting में true के लिए 1 और false के लिए 0 के रूप में display किया जा सकता है।
4. String Literals
String literal characters का sequence होता है जिसे double quotation marks (" ") के अंदर लिखा जाता है।
उदाहरण:
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to C++";
return 0;
}
Welcome to C++
यहाँ "Welcome to C++" एक string literal है।
5. Character Literals
Character literal एक single character को single quotation marks (' ') के अंदर represent करता है।
उदाहरण:
#include <iostream>
using namespace std;
int main()
{
char grade = 'A';
cout << "Grade = " << grade;
return 0;
}
Grade = A
6. Operators
Operators ऐसे symbols या keywords हैं जो values या operands पर operations perform करते हैं।
कुछ common C++ operators:
| Operator | Purpose | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Remainder | a % b |
= |
Assignment | a = 10 |
== |
Equality comparison | a == b |
> |
Greater than | a > b |
< |
Less than | a < b |
उदाहरण:
#include <iostream>
using namespace std;
int main()
{
int a = 20;
int b = 6;
cout << "Sum = " << a + b << endl;
cout << "Remainder = " << a % b;
return 0;
}
Sum = 26
Remainder = 2
7. Punctuators
Punctuators ऐसे symbols हैं जो C++ program की structure और syntax को define करने में सहायता करते हैं।
| Symbol | Common Use |
|---|---|
; |
Statement समाप्त करने के लिए |
, |
Items को separate करने के लिए |
( ) |
Function और expressions के लिए |
{ } |
Code block के लिए |
[ ] |
Array indexing आदि के लिए |
: |
कुछ declarations और labels में |
:: |
Scope resolution के लिए |
Tokenization Example
निम्न statement को देखें:
int total = marks + 10;
इस statement को tokens में इस प्रकार समझा जा सकता है:
| Token | Type |
|---|---|
int |
Keyword |
total |
Identifier |
= |
Operator |
marks |
Identifier |
+ |
Operator |
10 |
Integer literal |
; |
Punctuator |
Case Sensitivity in C++
C++ एक case-sensitive language है। इसका अर्थ है कि uppercase और lowercase letters को अलग-अलग माना जाता है।
उदाहरण के लिए:
#include <iostream>
using namespace std;
int main()
{
int age = 17;
int Age = 20;
cout << age << endl;
cout << Age;
return 0;
}
17
20
यहाँ age और Age दो अलग-अलग identifiers हैं।
age, Age और AGE अलग-अलग names हो सकते हैं।
Character Set vs Tokens
| Character Set | Tokens |
|---|---|
| C++ program में उपयोग किए जा सकने वाले valid characters का collection है। | C++ program की smallest meaningful units हैं। |
| इसमें letters, digits, special characters और whitespace शामिल हो सकते हैं। | इसमें keywords, identifiers, literals, operators और punctuators आदि शामिल होते हैं। |
| Characters का उपयोग करके tokens और program elements बनाए जाते हैं। | Tokens मिलकर C++ statements और programs बनाते हैं। |
Important Points
- Character Set valid characters का collection है जिनका उपयोग C++ source code में किया जाता है।
- C++ character set में letters, digits, special characters और whitespace शामिल हो सकते हैं।
- Token C++ program की smallest meaningful unit है।
- Keywords reserved words होते हैं जिनका predefined meaning होता है।
- Identifiers programmer द्वारा दिए गए names होते हैं।
- Constants और literals fixed values को represent करते हैं।
- Operators operations perform करने के लिए उपयोग होते हैं।
- Punctuators program की syntax और structure में सहायता करते हैं।
- C++ case-sensitive language है।
- Identifier का पहला character digit नहीं हो सकता।
- Keywords को सामान्यतः identifiers के रूप में उपयोग नहीं किया जा सकता।
Board Focus
Character Set → Valid characters का collection
Token → Smallest meaningful unit of a program
Main Token Types → Keywords, Identifiers, Literals, Operators, Punctuators
Keyword → Reserved word
Identifier → User-defined name
Literal → Fixed value
Operator → Operation perform करता है
Punctuator → Program structure/syntax में सहायता करता है
C++ → Case-sensitive language
Board Important Questions
Very Short Answer Questions
Q1. Character Set क्या है?
Answer: Character Set उन valid characters का collection है जिनका उपयोग C++ program को लिखने के लिए किया जाता है।
Q2. Token क्या है?
Answer: Token C++ program की सबसे छोटी meaningful unit है जिसे compiler पहचान सकता है।
Q3. C++ tokens के कोई चार प्रकार लिखिए।
Answer: Keywords, Identifiers, Literals और Operators।
Q4. Keyword क्या है?
Answer: Keyword ऐसा reserved word है जिसका C++ language में predefined meaning होता है।
Q5. Identifier क्या है?
Answer: Identifier programmer द्वारा variable, function, class आदि को दिया गया name है।
Q6. क्या C++ case-sensitive language है?
Answer: हाँ, C++ एक case-sensitive language है।
Q7. String literal किसके अंदर लिखा जाता है?
Answer: String literal double quotation marks (" ") के अंदर लिखा जाता है।
Q8. Character literal किसके अंदर लिखा जाता है?
Answer: Character literal single quotation marks (' ') के अंदर लिखा जाता है।
Short Answer Questions
Q9. C++ Character Set की categories समझाइए।
Answer: C++ character set को मुख्य रूप से letters, digits, special characters और whitespace characters में समझा जा सकता है। Letters में uppercase और lowercase alphabets शामिल होते हैं। Digits में 0 से 9 तक के अंक शामिल होते हैं। Special characters में operators और अन्य symbols शामिल होते हैं। Space, tab और newline whitespace characters के उदाहरण हैं।
Q10. Tokens क्या हैं? इनके प्रमुख प्रकार लिखिए।
Answer: Tokens C++ program की सबसे छोटी meaningful units हैं। इनके प्रमुख प्रकार Keywords, Identifiers, Constants/Literals, String Literals, Operators और Punctuators हैं।
Q11. Identifier naming के नियम लिखिए।
Answer: Identifier में letters, digits और underscore का उपयोग किया जा सकता है। इसका पहला character digit नहीं हो सकता। इसमें spaces का उपयोग नहीं किया जा सकता। Keyword को identifier के रूप में उपयोग नहीं किया जा सकता और C++ case-sensitive होने के कारण uppercase तथा lowercase names अलग माने जाते हैं।
Q12. Keyword और Identifier में अंतर बताइए।
| Keyword | Identifier |
|---|---|
| C++ का reserved word होता है। | Programmer द्वारा दिया गया name होता है। |
| इसका predefined meaning होता है। | Variable, function, class आदि को identify करता है। |
उदाहरण: int, if, return |
उदाहरण: marks, total, studentName |
Q13. Character Set और Tokens में क्या अंतर है?
Answer: Character Set valid characters का collection है, जबकि Tokens program की smallest meaningful units हैं। Characters का उपयोग करके keywords, identifiers, literals और अन्य tokens बनाए जाते हैं। Tokens मिलकर C++ statements और programs बनाते हैं।
Long Answer Questions
Q14. C++ Tokens के विभिन्न प्रकारों को उदाहरण सहित समझाइए।
Answer: C++ program में tokens की विभिन्न categories होती हैं। Keywords reserved words होते हैं, जैसे int और return। Identifiers programmer द्वारा दिए गए names होते हैं, जैसे marks और total। Literals fixed values को represent करते हैं, जैसे 10, 3.14 और 'A'। Operators operations perform करते हैं, जैसे +, - और =। Punctuators program की structure और syntax में सहायता करते हैं, जैसे ;, ( ) और { }।
Q15. निम्न C++ statement में tokens को पहचानिए:
int total = marks + 10;
Answer:
| Token | Category |
|---|---|
int |
Keyword |
total |
Identifier |
= |
Operator |
marks |
Identifier |
+ |
Operator |
10 |
Integer literal |
; |
Punctuator |
Quick Revision
- Character Set: Valid characters का collection
- Token: Smallest meaningful unit
- Keyword: Reserved word
- Identifier: Programmer-defined name
- Literal: Fixed value
- Operator: Operation perform करने वाला symbol/keyword
- Punctuator: Syntax और structure में उपयोग होने वाला symbol
- Case Sensitive: Uppercase और lowercase को अलग माना जाता है
Practice Questions
- C++ Character Set क्या है?
- C++ Character Set की प्रमुख categories लिखिए।
- Token क्या है?
- C++ tokens के प्रमुख प्रकार लिखिए।
- Keyword और Identifier में अंतर बताइए।
- Identifier naming के नियम लिखिए।
- C++ को case-sensitive language क्यों कहा जाता है?
- String literal और Character literal में अंतर बताइए।
- Operators क्या हैं? उदाहरण दीजिए।
- Punctuators क्या हैं?
- निम्न statement में tokens को identify कीजिए:
int sum = a + 10; - Character Set और Tokens में अंतर समझाइए।
- C++ tokens के विभिन्न प्रकारों को उदाहरण सहित समझाइए।