शुरुआती C/C++ अभ्यास

Temperature Converter

यह शुरुआती exercise Temperature Converter के जरिए core syntax और problem solving का अभ्यास कराती है।

Temperature Converter क्या है?

यह शुरुआती exercise Temperature Converter के जरिए core syntax और problem solving का अभ्यास कराती है।

Temperature Converter को C और C++ दोनों में runnable code से हल करें।

महत्वपूर्ण बातें

  • Compiler warnings चालू करके सभी ठीक करें।
  • हर value का type और lifetime समझें।
  • Input और array bounds validate करें।

C और C++ code examples

C
कोड चलाएँ →
main.c
#include <stdio.h>
int main(void) {
    double celsius = 25.0;
    // Floating-point constants preserve fractional values.
    double fahrenheit = celsius * 9.0 / 5.0 + 32.0;
    printf("%.1f C = %.1f F\n", celsius, fahrenheit);
}
अपेक्षित output
25.0 C = 77.0 F
C++
कोड चलाएँ →
main.cpp
#include <iomanip>
#include <iostream>
int main() {
    double celsius = 25.0;
    // Floating-point constants preserve fractional values.
    double fahrenheit = celsius * 9.0 / 5.0 + 32.0;
    std::cout << std::fixed << std::setprecision(1) << celsius << " C = " << fahrenheit << " F\n";
}
अपेक्षित output
25.0 C = 77.0 F

C और C++ की तुलना

C और C++ implementations की तुलना करें, input बदलें और अतिरिक्त edge cases test करें।

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 अलग करें।