C/C++ की मूल बातें

Functions और Parameter Passing

Pass-by-value, pointers और references की तुलना करें।

Functions और Parameter Passing क्या है?

Parameters default रूप से copy होते हैं; original object बदलने के लिए explicit access चाहिए।

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

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

C और C++ code examples

C
कोड चलाएँ →
main.c
#include <stdio.h>

void swap_ints(int *left, int *right) {
    int temporary = *left;
    *left = *right;
    *right = temporary;
}

int main(void) {
    int a = 4, b = 9;
    swap_ints(&a, &b);
    printf("%d %d\n", a, b);
    return 0;
}
अपेक्षित output
9 4
C++
कोड चलाएँ →
main.cpp
#include <iostream>

void swap_ints(int& left, int& right) {
    int temporary = left;
    left = right;
    right = temporary;
}

int main() {
    int a = 4, b = 9;
    swap_ints(a, b);
    std::cout << a << ' ' << b << '\n';
}
अपेक्षित output
9 4

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

C address पास करके dereference करता है। C++ reference alias syntax देता है और const read-only access बताता है।

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 रिव्यू के साथ।

सभी चैलेंज