Expressions and Type Conversion
How C++ evaluates expressions, and what happens when different data types mix.
Expressions and Type Conversion
C++ में Expression एक combination (संयोजन) होता है जिसमें constants, variables, operators और function calls शामिल हो सकते हैं और जिसका कोई एक result (परिणाम) प्राप्त होता है। Type Conversion का अर्थ किसी value को एक data type से दूसरे data type में convert (परिवर्तित) करना है।
Type Conversion → एक data type की value को दूसरे data type में बदलना
What is an Expression?
Expression एक ऐसा statement या combination है जिसे C++ evaluate करके एक value या result प्राप्त करता है। Expression में operands और operators का उपयोग किया जा सकता है।
उदाहरण के लिए:
10 + 20
यह एक expression है क्योंकि इसे evaluate करने पर 30 result प्राप्त होता है।
इसी प्रकार variables का उपयोग करके भी expression बनाया जा सकता है:
int a = 10;
int b = 20;
int result = a + b;
यहाँ a + b एक expression है जिसका result 30 है।
Components of an Expression
एक expression में मुख्य रूप से operators और operands होते हैं।
| Component | अर्थ | Example |
|---|---|---|
| Operand | जिस value या variable पर operation होता है | a, b, 10 |
| Operator | जो operation perform करता है | +, -, * |
| Result | Expression से प्राप्त value | 30 |
Example of an Expression
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 5;
int result = a * b + 10;
cout << result;
return 0;
}
60
यहाँ a * b + 10 एक arithmetic expression है। पहले a * b का result 50 प्राप्त होता है और फिर 10 जोड़ने पर final result 60 होता है।
Types of Expressions
C++ में expressions को उनके उपयोग और operators के आधार पर विभिन्न प्रकारों में समझा जा सकता है। प्रमुख प्रकार निम्नलिखित हैं:
- Arithmetic Expression
- Relational Expression
- Logical Expression
- Assignment Expression
- Conditional Expression
1. Arithmetic Expression
जिस expression में arithmetic operators जैसे +, -, *, / और % का उपयोग किया जाता है, उसे Arithmetic Expression कहा जाता है।
#include <iostream>
using namespace std;
int main()
{
int a = 20;
int b = 5;
int result = a + b * 2;
cout << result;
return 0;
}
30
2. Relational Expression
जिस expression में relational operators का उपयोग करके दो values की comparison की जाती है, उसे Relational Expression कहा जाता है। इसका result सामान्यतः true या false होता है।
#include <iostream>
using namespace std;
int main()
{
int a = 20;
int b = 10;
cout << (a > b);
return 0;
}
1
यहाँ a > b true है, इसलिए output 1 प्राप्त होता है।
3. Logical Expression
जिस expression में logical operators &&, || या ! का उपयोग होता है, उसे Logical Expression कहा जाता है।
#include <iostream>
using namespace std;
int main()
{
int age = 25;
cout << (age >= 18 && age <= 60);
return 0;
}
1
4. Assignment Expression
जिस expression में assignment operator का उपयोग करके किसी variable को value assign या update किया जाता है, उसे Assignment Expression कहा जाता है।
#include <iostream>
using namespace std;
int main()
{
int a;
a = 25;
cout << a;
return 0;
}
25
5. Conditional Expression
Conditional Expression में conditional या ternary operator ? : का उपयोग किया जाता है। यह किसी condition के आधार पर दो expressions में से एक को select करता है।
#include <iostream>
using namespace std;
int main()
{
int a = 20;
int b = 30;
int largest = (a > b) ? a : b;
cout << largest;
return 0;
}
30
Expression Evaluation
C++ किसी expression को evaluate करते समय operator precedence और associativity के rules का पालन करता है। यदि expression में कई operators हों, तो higher-precedence operator पहले evaluate किया जाता है।
#include <iostream>
using namespace std;
int main()
{
int result = 10 + 5 * 2;
cout << result;
return 0;
}
20
यहाँ multiplication (*) की precedence addition (+) से अधिक है। इसलिए पहले 5 * 2 और फिर 10 + 10 किया जाता है।
() का उपयोग करना अच्छा programming practice है।
What is Type Conversion?
Type Conversion का अर्थ किसी value को एक data type से दूसरे data type में बदलना है। C++ में type conversion की आवश्यकता तब पड़ सकती है जब अलग-अलग data types की values को एक expression में use किया जाता है।
उदाहरण के लिए, यदि किसी int value को double variable में assign किया जाता है, तो value दूसरे type में convert हो सकती है।
#include <iostream>
using namespace std;
int main()
{
int a = 10;
double b = a;
cout << b;
return 0;
}
10
यहाँ int value 10 को double में convert किया गया है।
Types of Type Conversion
C++ में type conversion को मुख्य रूप से दो प्रकारों में समझा जाता है:
- Implicit Type Conversion
- Explicit Type Conversion
1. Implicit Type Conversion
Implicit Type Conversion वह conversion है जो compiler द्वारा automatically किया जाता है। इसमें programmer को conversion explicitly लिखने की आवश्यकता नहीं होती।
इसे Automatic Type Conversion भी कहा जाता है।
Example of Implicit Conversion
#include <iostream>
using namespace std;
int main()
{
int a = 10;
double b = 5.5;
double result = a + b;
cout << result;
return 0;
}
15.5
यहाँ a एक integer है और b एक double है। Expression को evaluate करते समय integer value को compatible floating-point type में convert किया जाता है ताकि calculation सही प्रकार से हो सके।
Example of Automatic Type Conversion
#include <iostream>
using namespace std;
int main()
{
int number = 10;
double result = number;
cout << result;
return 0;
}
10
2. Explicit Type Conversion
Explicit Type Conversion में programmer स्वयं यह specify करता है कि किसी value को किस data type में convert करना है। इसे Type Casting भी कहा जाता है।
C++ में explicit conversion के लिए C-style cast और C++ style casts का उपयोग किया जा सकता है। Basic programming में सामान्य syntax इस प्रकार है:
(data_type) value
Example of Explicit Conversion
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 3;
double result = (double)a / b;
cout << result;
return 0;
}
3.33333
यहाँ a को explicitly double में convert किया गया है। इसलिए division का result decimal form में प्राप्त होता है।
Integer Division and Type Conversion
जब दो integer values को divide किया जाता है, तो result integer division के अनुसार प्राप्त होता है। यदि decimal result चाहिए, तो type conversion का उपयोग किया जा सकता है।
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 4;
cout << a / b << endl;
cout << (double)a / b;
return 0;
}
2
2.5
पहली calculation में दोनों operands integer हैं, इसलिए result 2 है। दूसरी calculation में a को double में convert किया गया है, इसलिए result 2.5 है।
Type Conversion from int to double
जब किसी integer value को double variable में assign किया जाता है, तो integer value floating-point value के रूप में represent हो सकती है।
#include <iostream>
using namespace std;
int main()
{
int marks = 75;
double percentage = marks;
cout << percentage;
return 0;
}
75
Type Conversion from double to int
जब floating-point value को integer type में convert किया जाता है, तो fractional part हट जाता है। इसे narrowing conversion का उदाहरण माना जा सकता है।
#include <iostream>
using namespace std;
int main()
{
double number = 12.75;
int result = (int)number;
cout << result;
return 0;
}
12
12.75 को int में convert करने पर fractional part .75 हट जाता है। यह rounding नहीं है।
static_cast in C++
C++ में explicit conversion के लिए static_cast एक preferred C++-style cast है। इसका syntax है:
static_cast<data_type>(value)
Example of static_cast
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 4;
double result = static_cast<double>(a) / b;
cout << result;
return 0;
}
2.5
यहाँ a को double में explicitly convert किया गया है।
static_cast<type>(value) एक स्पष्ट और सुरक्षित C++-style syntax है।
Type Conversion in Expressions
जब एक expression में अलग-अलग data types की values होती हैं, तो C++ calculation के दौरान आवश्यक conversions perform कर सकता है।
#include <iostream>
using namespace std;
int main()
{
int a = 10;
float b = 2.5f;
float result = a + b;
cout << result;
return 0;
}
12.5
यहाँ integer value 10 को calculation के लिए compatible floating-point type में convert किया जाता है।
Conversion Between char and int
C++ में char और integer types के बीच भी conversion हो सकता है। Character का numerical value उसके character encoding के अनुसार represent किया जाता है। सामान्य ASCII examples में uppercase letters के codes होते हैं।
#include <iostream>
using namespace std;
int main()
{
char ch = 'A';
cout << static_cast<int>(ch);
return 0;
}
65
यहाँ 'A' को integer में convert किया गया है और सामान्य ASCII encoding में इसका value 65 है।
Type Conversion and Data Loss
एक बड़े या अधिक precise data type को छोटे data type में convert करने पर information या precision का loss हो सकता है।
#include <iostream>
using namespace std;
int main()
{
double value = 25.89;
int number = static_cast<int>(value);
cout << number;
return 0;
}
25
यहाँ decimal part .89 preserve नहीं होता और केवल integer part प्राप्त होता है।
Difference Between Implicit and Explicit Conversion
| Implicit Conversion | Explicit Conversion |
|---|---|
| Compiler द्वारा automatically किया जाता है। | Programmer स्वयं conversion specify करता है। |
| इसमें explicit cast लिखना आवश्यक नहीं होता। | इसमें cast या conversion syntax का उपयोग किया जाता है। |
Example: double x = 10; |
Example: double x = static_cast<double>(10); |
Example: Expression with Type Conversion
#include <iostream>
using namespace std;
int main()
{
int total = 450;
int subjects = 5;
double average = static_cast<double>(total) / subjects;
cout << "Average = " << average;
return 0;
}
Average = 90
यहाँ total और subjects दोनों integer हैं। static_cast<double> का उपयोग करके division को floating-point calculation में बदला गया है।
Important Points
- Expression values, variables, operators और अन्य elements का combination हो सकता है।
- Expression को evaluate करने पर कोई result प्राप्त होता है।
- Arithmetic, relational, logical, assignment और conditional expressions C++ में सामान्य हैं।
- Operator precedence expression evaluation में महत्वपूर्ण भूमिका निभाती है।
- Type conversion का अर्थ एक data type को दूसरे data type में बदलना है।
- Implicit conversion compiler द्वारा automatically किया जाता है।
- Explicit conversion programmer द्वारा specify किया जाता है।
- Explicit conversion को type casting भी कहा जाता है।
static_cast<type>(value)C++ में explicit conversion का एक standard तरीका है।- Floating-point value को integer में convert करने पर fractional part हट सकता है।
- Type conversion में कभी-कभी data या precision का loss हो सकता है।
- Integer division में decimal part प्राप्त नहीं होता जब दोनों operands integer हों।
Board Focus
Expression → Values/Variables और Operators का combination
Arithmetic Expression → Mathematical calculation
Relational Expression → Comparison
Logical Expression → Conditions को combine करना
Implicit Conversion → Compiler द्वारा automatic conversion
Explicit Conversion → Programmer द्वारा conversion
Type Casting → एक type को दूसरे type में explicitly convert करना
static_cast<double>(a) → a को double में convert करना
Board Important Questions
Very Short Answer Questions
Q1. Expression क्या है?
Answer: Expression values, variables और operators का ऐसा combination है जिसे evaluate करने पर एक result प्राप्त होता है।
Q2. Type Conversion क्या है?
Answer: किसी value को एक data type से दूसरे data type में बदलना Type Conversion कहलाता है।
Q3. Implicit Type Conversion क्या है?
Answer: Compiler द्वारा automatically किया गया type conversion Implicit Type Conversion कहलाता है।
Q4. Explicit Type Conversion क्या है?
Answer: Programmer द्वारा स्वयं specify किया गया type conversion Explicit Type Conversion कहलाता है।
Q5. Type Casting क्या है?
Answer: किसी value को explicitly एक data type से दूसरे data type में convert करना Type Casting कहलाता है।
Q6. C++ में explicit conversion के लिए कौन-सा cast उपयोग किया जा सकता है?
Answer: static_cast<type>(value) का उपयोग किया जा सकता है।
Short Answer Questions
Q7. Arithmetic Expression और Relational Expression में अंतर बताइए।
| Arithmetic Expression | Relational Expression |
|---|---|
| Mathematical calculations के लिए उपयोग होता है। | दो values की comparison के लिए उपयोग होता है। |
a + b, a * b इसके उदाहरण हैं। |
a > b, a == b इसके उदाहरण हैं। |
Q8. Implicit और Explicit Type Conversion में अंतर बताइए।
Answer: Implicit conversion compiler द्वारा automatically किया जाता है, जबकि explicit conversion programmer द्वारा स्वयं specify किया जाता है। Explicit conversion में type casting syntax जैसे static_cast<double>(value) का उपयोग किया जा सकता है।
Q9. Integer division में type conversion का क्या महत्व है?
Answer: यदि दोनों operands integer हैं तो division का result integer होता है। Decimal result प्राप्त करने के लिए किसी operand को floating-point type में convert किया जा सकता है।
Long Answer Questions
Q10. Expression क्या है? इसके विभिन्न प्रकारों को उदाहरण सहित समझाइए।
Answer: Expression values, variables और operators का combination होता है जिसका evaluation करने पर एक result प्राप्त होता है। C++ में Arithmetic Expression calculation के लिए, Relational Expression comparison के लिए, Logical Expression conditions को combine करने के लिए, Assignment Expression value assign करने के लिए और Conditional Expression condition के आधार पर value select करने के लिए उपयोग किया जाता है।
Q11. Implicit और Explicit Type Conversion को उदाहरण सहित समझाइए।
Answer: Implicit Type Conversion compiler द्वारा automatically किया जाता है। उदाहरण के लिए double x = 10; में integer value automatically double में convert हो जाती है। Explicit Type Conversion में programmer conversion को स्वयं specify करता है। उदाहरण के लिए double x = static_cast<double>(10); में conversion explicitly किया गया है।
Q12. निम्न program का output बताइए और type conversion समझाइए।
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 4;
cout << a / b << endl;
cout << static_cast<double>(a) / b;
return 0;
}
2
2.5
पहली division में दोनों operands integer हैं, इसलिए integer result 2 मिलता है। दूसरी division में a को double में convert किया गया है, इसलिए result 2.5 मिलता है।
Quick Revision
- Expression: Operators और operands का combination जो result देता है
- Arithmetic Expression: Mathematical calculations
- Relational Expression: Comparison
- Logical Expression: Conditions को combine करना
- Implicit Conversion: Automatic conversion by compiler
- Explicit Conversion: Conversion specified by programmer
- Type Casting: Explicitly data type बदलना
- static_cast: C++ में explicit conversion का standard तरीका
Practice Questions
- Expression की परिभाषा लिखिए।
- Expression के विभिन्न प्रकारों के नाम लिखिए।
- Arithmetic Expression और Logical Expression में अंतर बताइए।
- Type Conversion क्या है?
- Implicit Type Conversion को उदाहरण सहित समझाइए।
- Explicit Type Conversion को उदाहरण सहित समझाइए।
- Type Casting क्या है?
static_castका syntax लिखिए।- Integer division और floating-point division में अंतर बताइए।
- निम्न program का output बताइए:
int a = 15; int b = 4; cout << a / b; - निम्न program का output बताइए:
int a = 15; int b = 4; cout << static_cast<double>(a) / b; - एक C++ program लिखिए जो दो integers का average decimal form में calculate करे।