← Back to C Course | Chapter 14: Additional Topics | Lesson 9 of 9

C और C++ के अंतर

C++ C से बढ़ा और objects, references, और overloading जैसी नई चीज़ें add कीं, एक car के नए model की तरह। यह lesson इन्हें compare करता है।

Procedural बनाम Object-Oriented

C एक procedural language है जो पूरी तरह functions के आस-पास बनी है जो data पर step by step operate करते हैं, जबकि C++ को C के एक extension की तरह design किया गया जो इसके ऊपर object-oriented programming की layer लगाता है, आपको data और उस पर operate करने वाले functions को classes और objects में साथ bundle करने देते हुए।

उदाहरण: Procedural vs. Object-Oriented

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", square(5));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Standard I/O Differences

C हर console input और output के लिए stdio.h से printf() और scanf() पर depend करता है, data interpret करने के लिए %d और %s जैसे format specifiers इस्तेमाल करते हुए, जबकि C++ additionally iostream से stream-based std::cout और std::cin offer करता है, जो format strings की बजाय << और >> operators इस्तेमाल करते हैं।

उदाहरण: Standard I/O Differences

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\n", 42);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Variable Declaration Rules

Strict पुराने C standards (जैसे C89) को किसी भी executable statement से पहले एक function के हर variable को इसके block के बिल्कुल top पर declare करना ज़रूरी था, जबकि C++ (और C99 और बाद के modern C standards) दोनों इस rule को relax करते हैं, आपको एक variable वहीं declare करने देते हुए जहाँ आपको इसकी पहले ज़रूरत है।

उदाहरण: Variable Declaration Rules

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("%d", x);
	// Declare y and set it to 10
	int y = 10;
	// Print formatted text to the screen
	printf("%d", y);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Function Overloading

C को एक file में हर function का एक unique नाम चाहिए, क्योंकि इसके पास दो same-named functions में फर्क बताने का कोई mechanism नहीं है, जबकि C++ function overloading support करता है, आपको कई functions define करने देते हुए जो एक नाम share करते हैं जब तक उनकी parameter lists type या count में अलग हों।

उदाहरण: Function Overloading

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int addInts(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", addInts(2, 3));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Memory Management

C explicit library function calls के जरिए dynamic memory manage करता है — allocate करने के लिए malloc() और release करने के लिए free() — जबकि C++ additionally new और delete operators देता है, जो memory allocation को साथ में एक class के constructor या destructor को automatically call करने के साथ combine करते हैं।

उदाहरण: Memory Management

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	int *ptr = malloc(sizeof(int));
	*ptr = 5;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Release the heap memory so it can be reused
	free(ptr);
	// 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. यह मान लेना कि C code हमेशा C++ की तरह compile होता है, जब C++ void* conversion जैसी चीज़ों को लेकर stricter है।
  2. C++ में printf इस्तेमाल करना जहाँ types के लिए cout safer है।
  3. यह भूल जाना कि C++ बिना एक cast के malloc से returned void* से implicit conversion allow नहीं करता।
चैप्टर सारांश
  • Additional C topics में goto, header files, multi-file programs, और static और inline functions शामिल हैं।
  • Signal handling, date और time functions, और random numbers system-level features add करते हैं।
  • C को C++ से compare करना दिखाता है कि दोनों languages कैसे अलग हैं।

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.