← Back to C Course | Chapter 12: Advanced Topics | Lesson 2 of 20

C में Type Casting

Type casting एक type की value को दूसरी में बदलता है, juice को एक cup से एक bottle में डालने जैसा। एक छोटे type में जाना information खो सकता है।
Syntax
c
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?

c
// 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;
}

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

c
// 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;
}

Explicit Type Casting

Explicit casting का मतलब है value से ठीक पहले parentheses में target type लिखना, जैसे (double)x, एक conversion force करने के लिए जो compiler अन्यथा खुद नहीं करता।

जब भी आपको जानबूझकर C के default promotion rules override करने हों इसे इस्तेमाल करें, सिर्फ एक compiler warning चुप कराने के लिए नहीं।

उदाहरण: Explicit Type Casting

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Division के बाद cast करना, जैसे (float)(a / b), जब cast इससे पहले apply होना चाहिए।
  2. एक बड़ी value को एक छोटे type में cast करना और data खोना।
  3. यह मान लेना कि एक cast value को round करता है, जब यह truncate करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.