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

Arrays और Strings

C string null-terminated char array है; std::string storage own करके length रखता है।

Arrays और Strings क्या है?

C string null-terminated char array है; std::string storage own करके length रखता है।

Fixed arrays iterate करें और text length सुरक्षित निकालें।

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

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

C और C++ code examples

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

int main(void) {
    int values[] = {3, 5, 8, 13};
    char name[] = "Ada";
    size_t count = sizeof values / sizeof values[0];
    printf("%s: %zu values, %zu letters\n", name, count, strlen(name));
    return 0;
}
अपेक्षित output
Ada: 4 values, 3 letters
C++
कोड चलाएँ →
main.cpp
#include <array>
#include <iostream>
#include <string>

int main() {
    std::array values{3, 5, 8, 13};
    std::string name = "Ada";
    std::cout << name << ": " << values.size()
              << " values, " << name.size() << " letters\n";
}
अपेक्षित output
Ada: 4 values, 3 letters

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

sizeof local array गिनता है, strlen null तक scan करता है और std::string::size O(1) है।

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