C में Type Conversion
In this page:
target_type variable_name = (target_type) expression; // explicit cast
double result = int_value * double_value; // implicit conversion
Implicit Conversion
जब एक expression types मिलाता है, C evaluate करने से पहले narrower operand को wider वाले में promote करता है -- एक double में add किया गया int पहले double में convert होता है, ताकि result fractional part रखे, चुपचाप truncated होने की बजाय।
उदाहरण: Implicit Conversion
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare a and set it to 5
int a = 5;
// Declare b and set it to 2.0
double b = 2.0;
// Print formatted text to the screen
printf("%.1f", a + b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Explicit Casts
किसी value के आगे (int), (float), या (char) लिखना एक conversion force करता है जो compiler खुद नहीं करता, useful जब आपको integer division की बजाय एक fractional result चाहिए या जानबूझकर किसी value को उसके integer part तक strip करना चाहते हैं।
उदाहरण: Explicit Casts
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 7, b = 2;
// Declare result and set it to (double)a / b
double result = (double)a / b;
// Print formatted text to the screen
printf("%.1f", result);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Truncation और Data Loss
एक float को int में cast करना round नहीं करता -- यह decimal point के बाद सब कुछ discard कर देता है, इसलिए (int)9.9 9 बनता है, 10 नहीं; एक बड़ी value को एक छोटे type में cast करना भी बिना कोई error उठाए चुपचाप wrap around हो सकता है।
उदाहरण: Truncation and Data Loss
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare f and set it to 9.9f
float f = 9.9f;
// Declare truncated and set it to (int)f
int truncated = (int)f;
// Print formatted text to the screen
printf("%d", truncated);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Signed और Unsigned Conversion
एक negative signed value को एक unsigned variable को assign करना fail नहीं होता -- यह modular arithmetic इस्तेमाल करके wrap around होता है, इसलिए एक unsigned int में store -1 type की maximum representable value बन जाता है, loop counters में subtle bugs का एक frequent source।
उदाहरण: Signed and Unsigned Conversion
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare negative and set it to -1
int negative = -1;
// Declare wrapped and set it to negative
unsigned int wrapped = negative;
// Print formatted text to the screen
printf("%u", wrapped);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
C++ के मुकाबले Casting
Plain C में सिर्फ यह एक C-style cast syntax है, (type)value, हर तरह के conversion पर uniformly apply होता है; C++ ने बाद में इसे चार distinct operators में बांटा -- static_cast, dynamic_cast, const_cast, और reinterpret_cast -- specifically हर conversion की intent और safety को explicit बनाने के लिए।
उदाहरण: Casting Compared to C++
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare d and set it to 9.5
double d = 9.5;
// Declare i and set it to (int)d
int i = (int)d;
// Print formatted text to the screen
printf("%d", i);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- दो ints divide करना और एक double को assign करना, जैसे
double d = 5 / 2;, जो 2.0 देता है क्योंकि division पहले ही integers की तरह हो चुका था। - एक बड़े double को
intमें cast करना, जो fraction truncate करता है और overflow हो सकता है। - एक comparison में signed और unsigned values मिलाना, जहाँ एक negative number एक huge unsigned value में बदल जाता है।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers