C और C++ के अंतर
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Function Overloading
C को एक file में हर function का एक unique नाम चाहिए, क्योंकि इसके पास दो same-named functions में फर्क बताने का कोई mechanism नहीं है, जबकि C++ function overloading support करता है, आपको कई functions define करने देते हुए जो एक नाम share करते हैं जब तक उनकी parameter lists type या count में अलग हों।
उदाहरण: Function Overloading
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि C code हमेशा C++ की तरह compile होता है, जब C++
void*conversion जैसी चीज़ों को लेकर stricter है। - C++ में
printfइस्तेमाल करना जहाँ types के लिएcoutsafer है। - यह भूल जाना कि C++ बिना एक cast के
mallocसे returnedvoid*से 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 कैसे अलग हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: