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

Decimal to Binary

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

Decimal to Binary क्या है?

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

Decimal to Binary को 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) {
    unsigned number = 13, value = number; int bits[32], count = 0;
    // Store remainders generated from right to left.
    do { bits[count++] = value % 2; value /= 2; } while (value);
    printf("%u = ", number); while (count) printf("%d", bits[--count]); puts("");
}
अपेक्षित output
13 = 1101
C++
कोड चलाएँ →
main.cpp
#include <bitset>
#include <iostream>
#include <string>
int main() {
    unsigned number = 13;
    // Remove leading zeroes from a fixed-width bitset string.
    std::string bits = std::bitset<32>(number).to_string();
    bits.erase(0, bits.find_first_not_of('0'));
    std::cout << number << " = " << bits << '\n';
}
अपेक्षित output
13 = 1101

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