Data Types और Formatted Output
C और C++ fundamental numeric types साझा करते हैं, लेकिन formatting के सामान्य तरीके अलग हैं।
Data Types और Formatted Output क्या है?
C और C++ fundamental numeric types साझा करते हैं, लेकिन formatting के सामान्य तरीके अलग हैं।
Numbers declare करके सुरक्षित रूप से print करें।
महत्वपूर्ण बातें
- Compiler warnings चालू करके सभी ठीक करें।
- हर value का type और lifetime समझें।
- Input और array bounds validate करें।
C और C++ code examples
main.c
#include <stdio.h>
int main(void) {
int items = 7;
double price = 3.5;
printf("items=%d total=%.2f\n", items, items * price);
return 0;
}
items=7 total=24.50
C++
main.cpp
#include <iomanip>
#include <iostream>
int main() {
int items = 7;
double price = 3.5;
std::cout << "items=" << items << " total="
<< std::fixed << std::setprecision(2) << items * price << '\n';
}
items=7 total=24.50
C और C++ की तुलना
printf में specifier argument से match होना चाहिए; C++ stream type से overload चुनता है और iomanip precision नियंत्रित करता है।
C
C छोटे procedural APIs और representation details स्पष्ट करता है।
C++
C++ low-level model रखकर safer library types जोड़ता है।
अभ्यास
दोनों versions चलाकर बदलें और language guarantees की तुलना करें।
- Invalid और boundary inputs जोड़ें।
- -Wall -Wextra -Wpedantic से compile करें।
- Declarations और implementation अलग करें।