Data Types और Formatted Output
Numbers declare करके सुरक्षित रूप से print करें।
Data Types और Formatted Output क्या है?
C और C++ fundamental numeric types साझा करते हैं, लेकिन formatting के सामान्य तरीके अलग हैं।
महत्वपूर्ण बातें
- 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 अलग करें।
चैलेंज से अभ्यास करें
इस पाठ में सीखी बात से एक असली समस्या हल करें। हर चैलेंज ब्राउज़र में चलता है, तुरंत टेस्ट और AI रिव्यू के साथ।
- घंटे, मिनट और सेकंड सेकंड में दी गई अवधि को एक संक्षिप्त पठनीय स्ट्रिंग में बदलें। आसान
- Celsius to Fahrenheit Convert a temperature from Celsius to Fahrenheit. आसान