C में Type Casting
In this page:
target_type variable_name = (target_type) expression; // explicit cast
double result = int_value * double_value; // implicit conversion
Type Casting क्या है?
Type casting एक value को एक data type से दूसरे में convert करता है ताकि एक expression उस precision या range के साथ evaluate हो जो आप actually चाहते हैं, न कि compiler default से जो चुनता।
यह सबसे ज़्यादा तब मायने रखता है जब mixed types same expression में मिलें, जैसे एक int को एक double के साथ combine करना।
उदाहरण: What is Type Casting?
// 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 (double)a
double b = (double)a;
// Print formatted text to the screen
printf("%.1f", b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Implicit Type Conversion
Implicit conversion automatically तब होता है जब compiler को एक expression में mismatched operand types reconcile करने हों, चुपचाप narrower type (जैसे char या int) को wider वाले (जैसे double) में promote करते हुए ताकि calculation में कोई precision न खोए।
यह convenient है, लेकिन अगर आप promotion की उम्मीद न करें तो यह bugs छिपा सकता है।
उदाहरण: Implicit Type 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.5
double b = 2.5;
// 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 Type Casting
Explicit casting का मतलब है value से ठीक पहले parentheses में target type लिखना, जैसे (double)x, एक conversion force करने के लिए जो compiler अन्यथा खुद नहीं करता।
जब भी आपको जानबूझकर C के default promotion rules override करने हों इसे इस्तेमाल करें, सिर्फ एक compiler warning चुप कराने के लिए नहीं।
उदाहरण: Explicit Type Casting
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 9.7
double x = 9.7;
// Print formatted text to the screen
printf("%d", (int)x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Integer Division Loss रोकना
क्योंकि C में integer division zero की ओर truncate होता है और कोई भी remainder drop कर देता है, 7/2 जैसे दो int variables divide करना हमेशा 3 देता है, 3.5 नहीं।
Division से पहले कम से कम एक operand को float या double में cast करना C को floating-point arithmetic इस्तेमाल करने और fractional part रखने पर मजबूर करता है।
उदाहरण: Preventing Integer Division Loss
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 7, b = 2;
// Print formatted text to the screen
printf("%.1f", (float)a / b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Float से Integer Truncation
एक float या double को एक int में convert करना value को round करने की बजाय zero की ओर truncate करता है, इसलिए 9.99, 9 बनता है और -9.99, -10 नहीं, -9 बनता है।
अगर आपको actually rounding चाहिए, cast करने से पहले math.h से round() call करें, क्योंकि अकेला cast हमेशा decimal part काट देगा।
उदाहरण: Float to Integer Truncation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d %d", (int)9.99, (int)-9.99);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Division के बाद cast करना, जैसे
(float)(a / b), जब cast इससे पहले apply होना चाहिए। - एक बड़ी value को एक छोटे type में cast करना और data खोना।
- यह मान लेना कि एक cast value को round करता है, जब यह truncate करता है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions