Type Conversion or Type Casting in C Language with Examples
Type Conversion or Type Casting is the process of converting a value from one data type to another data type. C language allows expressions to contain variables and constants of different data types. During expression evaluation or assignment, values may be converted from one data type to another according to the rules of C language.
Type Conversion या Type Casting वह प्रक्रिया है जिसमें किसी Value को एक Data Type से दूसरे Data Type में परिवर्तित किया जाता है। C भाषा में किसी Expression में अलग-अलग Data Types के Variables तथा Constants का उपयोग किया जा सकता है। Expression का मूल्यांकन करने या Assignment के समय C भाषा के नियमों के अनुसार Values को दूसरे Data Type में Convert किया जा सकता है।
There are two main types of Type Conversion in C:
C भाषा में Type Conversion के दो मुख्य प्रकार होते हैं:
Implicit Type Conversion (Automatic Type Conversion)
Explicit Type Conversion (Type Casting)
1. Implicit Type Conversion or Automatic Type Conversion
Implicit Type Conversion is the process in which the compiler automatically converts a value from one data type to another without requiring an explicit cast operator. It generally occurs during assignments and expression evaluation.
Implicit Type Conversion वह प्रक्रिया है जिसमें Compiler बिना किसी Explicit Cast Operator के स्वतः ही एक Data Type की Value को दूसरे Data Type में Convert करता है। यह सामान्यतः Assignment तथा Expression Evaluation के समय होता है।
Rules of Implicit Type Conversion
The following important rules explain how implicit conversions work in C.
C भाषा में Implicit Type Conversion निम्नलिखित महत्वपूर्ण नियमों के अनुसार होता है।
Rule 1: Integer Promotions
Before performing many arithmetic operations, integer types with rank lower than int, such as char and short, are promoted to int if int can represent all their values; otherwise, they are promoted to unsigned int.
कई Arithmetic Operations से पहले char तथा short जैसे छोटे Integer Data Types को Integer Promotion के द्वारा int या आवश्यकता पड़ने पर unsigned int में Convert किया जाता है।
Rule 2: Usual Arithmetic Conversions
When two operands of different arithmetic types are used in an expression, C applies the usual arithmetic conversions to determine a common type.
जब किसी Expression में अलग-अलग Arithmetic Data Types के दो Operands का उपयोग किया जाता है, तब C भाषा दोनों के लिए एक Common Type निर्धारित करती है।
For floating-point operands, the general conversion order is:
Floating-point Operands के लिए सामान्य Conversion क्रम निम्न है:
If either operand is
long double, the other operand is converted tolong double.Otherwise, if either operand is
double, the other operand is converted todouble.Otherwise, if either operand is
float, the other operand is converted tofloat.यदि किसी एक Operand का Data Type
long doubleहै, तो दूसरे Operand को भीlong doubleमें Convert किया जाता है।अन्यथा यदि किसी एक Operand का Data Type
doubleहै, तो दूसरे Operand को भीdoubleमें Convert किया जाता है।अन्यथा यदि किसी एक Operand का Data Type
floatहै, तो दूसरे Operand को भीfloatमें Convert किया जाता है।
For integer operands, the conversion rules also consider integer ranks, signedness, and the ranges of the types. Therefore, it is not always correct to say that every integer operand is converted to long int or unsigned long int.
Integer Operands के लिए Conversion करते समय Integer Rank, Signedness तथा Data Types की Range को भी ध्यान में रखा जाता है। इसलिए यह कहना सही नहीं है कि प्रत्येक Integer Operand हमेशा long int या unsigned long int में Convert होता है।
Example 1: Converting int into float
#include <stdio.h>int main(void){int a = 20;float b;b = a;printf("Value of a = %d\n", a);printf("Value of b = %.2f\n", b);return 0;}
Output:
Value of a = 20Value of b = 20.00
Explanation (व्याख्या):
Here, the integer value 20 is automatically converted into the floating-point value 20.0 during assignment.
यहाँ Assignment के समय Integer Value 20 स्वतः ही Floating-point Value 20.0 में Convert हो जाती है।
Example 2: Integer Division Assigned to float
#include <stdio.h>int main(void){float ans;ans = 9 / 4;printf("Answer = %.2f\n", ans);return 0;}
Output:
Answer = 2.00Explanation (व्याख्या):
Both operands, 9 and 4, are integers. Therefore, integer division is performed first.
9 / 4 = 2
After that, the integer result 2 is converted into float and assigned to ans.
यहाँ 9 तथा 4 दोनों Integer हैं। इसलिए सबसे पहले Integer Division होता है।
9 / 4 = 2
इसके बाद Integer Result 2 को Float में Convert करके ans में Assign किया जाता है।
Important: Assigning an integer division result to a float variable does not make the division a floating-point division.
महत्वपूर्ण: Integer Division के Result को Float Variable में Assign करने से Division Floating-point Division नहीं बनता।
Example 3: Floating-Point Division
#include <stdio.h>int main(void){float ans;ans = 9.0f / 4;printf("Answer = %.2f\n", ans);return 0;}
Output:
Answer = 2.25Explanation (व्याख्या):
Here, one operand is a floating-point value (9.0f). Therefore, the other operand is converted to a compatible floating-point type before division.
यहाँ एक Operand Floating-point Value (9.0f) है। इसलिए Division से पहले दूसरा Operand भी उपयुक्त Floating-point Type में Convert हो जाता है।
2. Explicit Type Conversion or Type Casting
Explicit Type Conversion is the process in which the programmer manually converts a value from one data type to another using a cast operator.
It is useful when we want to specify the data type of an expression or control the result of a calculation.
Explicit Type Conversion वह प्रक्रिया है जिसमें Programmer स्वयं Cast Operator का उपयोग करके किसी Value को एक Data Type से दूसरे Data Type में Convert करता है।
इसका उपयोग तब किया जाता है जब हम किसी Expression का Data Type निर्धारित करना चाहते हैं या Calculation के Result को नियंत्रित करना चाहते हैं।
Syntax of Explicit Type Casting
(data_type) expression;Explanation:
data_type: The data type into which the value is to be converted.expression: The value or expression to be converted.
व्याख्या:
data_type: वह Data Type जिसमें Value को Convert करना है।expression: वह Value या Expression जिसे Convert करना है।
Example 1: Converting int into float using Type Casting
#include <stdio.h>int main(void){int a = 2000;float b;b = (float)a;printf("Value of a = %d\n", a);printf("Value of b = %.2f\n", b);return 0;}
Output:
Value of a = 2000Value of b = 2000.00
Explanation (व्याख्या):
Here, (float)a explicitly converts the integer value of a into a float value before assignment.
यहाँ (float)a द्वारा Integer Variable a की Value को Assignment से पहले Explicitly Float में Convert किया जाता है।
Example 2: Converting Integer Division into Floating-Point Division
#include <stdio.h>int main(void){float ans;ans = (float)9 / 4;printf("Answer = %.2f\n", ans);return 0;}
Output:
Answer = 2.25Explanation (व्याख्या):
The expression (float)9 converts the integer 9 into a floating-point value. Therefore, floating-point division is performed.
9.0 / 4 = 2.25
Expression (float)9 Integer 9 को Floating-point Value में Convert करता है। इसलिए यहाँ Floating-point Division होता है।
Important: The cast applies to the operand immediately following it. In this example, the conversion of 9 causes the division to be performed using floating-point arithmetic.
महत्वपूर्ण: Cast Operator का प्रभाव उसके तुरंत बाद आने वाले Operand पर होता है। इस उदाहरण में 9 को Convert करने के कारण Division Floating-point Arithmetic से होता है।
Example 3: Converting float into int
#include <stdio.h>int main(void){float a = 25.75f;int b;b = (int)a;printf("Value of a = %.2f\n", a);printf("Value of b = %d\n", b);return 0;}
Output:
Value of a = 25.75Value of b = 25
Explanation (व्याख्या):
When a floating-point value is converted into an integer type, the fractional part is discarded. The value is truncated toward zero; it is not rounded to the nearest integer.
जब Floating-point Value को Integer Type में Convert किया जाता है, तब उसका Fractional Part हटा दिया जाता है। Value को Zero की ओर Truncate किया जाता है, न कि निकटतम Integer में Round किया जाता है।
For example:
(int)25.75gives25.(int)-25.75gives-25.
Difference Between Implicit and Explicit Type Conversion
(Implicit तथा Explicit Type Conversion में अंतर)
| Implicit Type Conversion | Explicit Type Conversion |
|---|---|
| Conversion is performed automatically. | Conversion is specified manually by the programmer. |
| No cast operator is required. | Cast operator is required. |
| It is performed according to C conversion rules. | It is performed using the specified target type. |
Example: b = a; | Example: b = (float)a; |
| Conversion Compiler द्वारा स्वतः होता है। | Conversion Programmer द्वारा किया जाता है। |
| इसमें Cast Operator की आवश्यकता नहीं होती। | इसमें Cast Operator का उपयोग किया जाता है। |
Important Points About Type Conversion and Type Casting
(महत्वपूर्ण बिंदु)
Type Conversion changes a value from one data type to another.
Type Conversion किसी Value को एक Data Type से दूसरे Data Type में बदलता है।
Implicit Type Conversion is performed automatically according to C language rules.
Implicit Type Conversion C भाषा के नियमों के अनुसार स्वतः होता है।
Explicit Type Conversion is performed using the cast operator.
Explicit Type Conversion Cast Operator के द्वारा किया जाता है।
Integer division discards the fractional part before the result is assigned to a floating-point variable.
Integer Division में Fractional Part हट जाता है, उसके बाद ही Result को Floating-point Variable में Assign किया जाता है।
Converting a floating-point value to an integer discards the fractional part by truncating toward zero.
Floating-point Value को Integer में Convert करने पर Fractional Part Zero की ओर Truncate होकर हट जाता है।
Type conversion may cause loss of precision or information, depending on the source and destination types.
Type Conversion के दौरान Source तथा Destination Data Types के अनुसार Precision या Information का नुकसान हो सकता है।
Explicit casting does not guarantee that every conversion is safe. Some conversions may produce implementation-defined results or undefined behavior when the value is outside the destination type's representable range.
Explicit Casting से यह सुनिश्चित नहीं होता कि प्रत्येक Conversion सुरक्षित होगा। यदि Value Destination Data Type की Range से बाहर है, तो कुछ Conversions में Implementation-defined Result या Undefined Behavior हो सकता है।
Type Conversion and Type Casting are important concepts in C programming. Implicit conversion allows the compiler to perform automatic conversions, whereas explicit conversion gives the programmer control over the type of an expression. Understanding these concepts helps avoid unexpected results, especially in arithmetic expressions and assignments.
Type Conversion तथा Type Casting C Programming की महत्वपूर्ण Concepts हैं। Implicit Conversion में Compiler स्वतः Conversion करता है, जबकि Explicit Conversion में Programmer स्वयं Expression का Type निर्धारित करता है। इन Concepts को समझने से Arithmetic Expressions तथा Assignments में Unexpected Results से बचा जा सकता है।
Comments
Post a Comment