Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 Foundation  ·  Lesson 02

C++ vs C Language

C और C++ एक paragraph में

C को Dennis Ritchie ने 1972 में Bell Labs में UNIX operating system लिखने के लिए बनाया। यह छोटी, तेज़ और hardware के करीब है। C++ को Bjarne Stroustrup ने 1979 से "C with Classes" के रूप में बनाना शुरू किया: यह लगभग पूरी C रखती है और object-oriented programming तथा कई safety और productivity features जोड़ती है। C++ ISO द्वारा standardised है; मुख्य versions हैं C++98, C++11, C++14, C++17, C++20 और C++23।

C++ को C और एक बड़े toolbox की तरह समझें। C में जो जानते हैं वह लगभग सब चलता है, पर C++ अक्सर वही काम ज़्यादा सुरक्षित और छोटे तरीके से करने देती है।

C और C++ में अंतर (table)

#बिंदुCC++
1ParadigmProceduralMulti-paradigm: procedural + object-oriented + generic
2Classes और objectsनहीं (सिर्फ data वाला struct)हाँ — data और functions वाली classes
3Encapsulation / data hidingAccess control नहींprivate, protected, public
4Inheritance और polymorphismSupport नहींSupport (virtual functions सहित)
5Input / output<stdio.h> के printf(), scanf()<iostream> के cout, cin (C functions भी चलते हैं)
6Dynamic memorymalloc(), calloc(), free()new, delete; smart pointers
7Function overloadingAllowed नहींAllowed
8Default argumentsनहींहाँ
9Referencesनहीं (सिर्फ pointers)हाँ: int& r = x;
10Namespacesनहींहाँ: std::, अपने भी
11Exception handlingनहीं (error codes)try, catch, throw
12Templates / generic codeनहीं (सिर्फ macros)Function और class templates
13Standard libraryC library (string.h, math.h …)C library + STL: vector, map, sort…
14String typechar arraysstd::string class
15File extension.c.cpp, .cc, .cxx

साथ-साथ: input और output

C++
/* C */
#include <stdio.h>
int main(void) {
    int age;
    printf("Enter age: ");
    scanf("%d", &age);
    printf("Next year you will be %d\n", age + 1);
    return 0;
}
C++
// C++
#include <iostream>
int main() {
    int age;
    std::cout << "Enter age: ";
    std::cin >> age;
    std::cout << "Next year you will be " << age + 1 << "\n";
    return 0;
}

C++ streams हर variable का type जानते हैं, इसलिए %d जैसे format specifiers गलत होने का डर नहीं, और input के लिए & भी नहीं चाहिए।

साथ-साथ: dynamic memory

C++
/* C */
int *marks = (int *) malloc(5 * sizeof(int));
/* ... */
free(marks);
C++
// C++ (classic)
int* marks = new int[5];
// ...
delete[] marks;

// C++ (modern — manual free की ज़रूरत ही नहीं)
std::vector<int> marks(5);

new constructors call करता है और उसमें cast या sizeof नहीं चाहिए। Modern C++ और आगे जाती है: containers और smart pointers memory अपने-आप free करते हैं।

C का struct vs C++ की class

C++
/* C: सिर्फ data; functions बाहर */
struct Student { char name[30]; int marks; };
void print(struct Student s) { printf("%s %d\n", s.name, s.marks); }
C++
// C++: data और व्यवहार साथ में, access control के साथ
class Student {
    std::string name;
    int marks;
public:
    Student(std::string n, int m) : name(n), marks(m) {}
    void print() const { std::cout << name << " " << marks << "\n"; }
};

C++ में struct के भी member functions हो सकते हैं; class से फ़र्क सिर्फ इतना है कि उसके members default रूप से public होते हैं।

Overloading और references

C++
// सिर्फ C++ में संभव
int area(int side)            { return side * side; }
int area(int l, int b)        { return l * b; }
void addBonus(int& marks)     { marks += 5; }   // reference: * या &x नहीं चाहिए

int m = 70;
addBonus(m);                  // m 75 हो जाता है

C में दो अलग function नाम (area_square, area_rect) और pointer parameter (void addBonus(int *m), call addBonus(&m)) चाहिए होते।

दोनों में क्या एक जैसा है

  • Basic data types (int, char, float, double), operators और expressions।
  • if, switch, for, while, do-while, break, continue।
  • Arrays, pointers और pointer arithmetic।
  • Functions, recursion और preprocessor (#include, #define)।
  • C standard library, जो C++ में <cstdio>, <cmath>, <cstring> आदि के रूप में मिलती है।

कौन-सी इस्तेमाल करें?

C लें जब…C++ लें जब…
बहुत छोटा embedded/firmware code या OS kernel लिखना होबड़े applications, games, desktop software बनाने हों
Platform पर सिर्फ C compiler होClasses, STL containers और algorithms चाहिए
Memory और hardware को सबसे निचले स्तर पर समझना होआपका syllabus (CBSE Class 11–12 CS, कई B.Tech courses) C++ पढ़ाता हो

आम भ्रम

  • "C++ बस classes वाली C है" — modern C++ में templates, STL, lambdas, smart pointers और बहुत कुछ है।
  • "C code हमेशा C++ में compile होता है" — नहीं, अगर वह C++ keywords को नाम बनाए या implicit void* conversion पर टिका हो।
  • "cout धीमा है, तो C++ धीमी है" — I/O speed std::ios::sync_with_stdio(false) जैसी settings पर निर्भर है; calculation की speed बराबर है।
  • "C++ में struct के functions नहीं हो सकते" — हो सकते हैं।
🏋️ अभ्यास

5 students के नाम और marks store करने वाला program लिखें। पहले C में struct, char[] और printf से। फिर C++ में class, std::string, std::vector और cout से दोबारा लिखें। दोनों की lines गिनें और देखें किसे सही लिखना आसान था।

सारांश

  • C procedural है; C++ OOP, generic programming और कई safety features जोड़ती है।
  • C++ में classes, references, overloading, namespaces, exceptions, templates और STL हैं।
  • printf/scanf → cout/cin; malloc/free → new/delete या बेहतर, containers और smart pointers।
  • ज़्यादातर C code valid C++ है, पर सब नहीं।
  • एक ही algorithm के लिए दोनों बराबर तेज़ हैं।

संबंधित: C++ का परिचय, OOP concepts, C tutorial: C vs C++।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C और C++ में मुख्य अंतर क्या है?
C procedural language है: programs functions से बनते हैं जो data पर काम करते हैं। C++ लगभग पूरी C रखती है पर object-oriented programming (classes, inheritance, polymorphism) जोड़ती है, साथ में references, function overloading, templates, exceptions, namespaces और Standard Template Library भी।
क्या C program C++ program की तरह चल सकता है?
ज़्यादातर C code C++ में compile हो जाता है, पर पूरा नहीं। C++ ज़्यादा सख़्त है: जैसे malloc से मिले void* को int* में रखने के लिए C++ में cast चाहिए, और new, class, delete C++ में keywords हैं, इसलिए इन्हें variable नाम बनाने वाला C code fail होगा।
C और C++ में कौन तेज़ है?
एक ही algorithm के लिए दोनों लगभग बराबर तेज़ machine code बनाती हैं। Templates और inline functions जैसे C++ features का अक्सर run time पर कोई खर्च नहीं होता। Speed का अंतर आमतौर पर program लिखने के तरीके से आता है, language से नहीं।
क्या C++ से पहले C सीखनी चाहिए?
ज़रूरी नहीं। पहले C सीखने से memory, pointers और machine कैसे काम करती है यह समझ आता है, जो बाद में काम आता है। सीधे modern C++ (std::string, std::vector और classes के साथ) से शुरू करना भी ठीक है, और कई schools Class 11–12 में पहले C++ ही पढ़ाते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicC++ Setup: GCC, MinGW, VS Code and Online Compiler आपकी प्रगति: 2 / 81 (2%) · Lesson 3 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें