← Back to C Course | Chapter 1: Introduction & Basics | Lesson 15 of 21

C में Type Conversion

Type conversion एक type की value को दूसरी में बदलना है, एक cup से जूस को bottle में डालने जैसा। कभी-कभी C यह आपके लिए करता है, और कभी-कभी आप इसे मांगते हैं, और details खो सकती हैं।
Syntax
c
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

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.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;
}

Explicit Casts

किसी value के आगे (int), (float), या (char) लिखना एक conversion force करता है जो compiler खुद नहीं करता, useful जब आपको integer division की बजाय एक fractional result चाहिए या जानबूझकर किसी value को उसके integer part तक strip करना चाहते हैं।

उदाहरण: Explicit Casts

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

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

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

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

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

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++

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;
}
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. दो ints divide करना और एक double को assign करना, जैसे double d = 5 / 2;, जो 2.0 देता है क्योंकि division पहले ही integers की तरह हो चुका था।
  2. एक बड़े double को int में cast करना, जो fraction truncate करता है और overflow हो सकता है।
  3. एक comparison में signed और unsigned values मिलाना, जहाँ एक negative number एक huge unsigned value में बदल जाता है।

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.