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

Functions और Parameter Passing

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

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

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

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

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

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