MCAWALA

C++ Vertical Menu

C++ Programming Topics

C++ Basic Syntax क्या है?

C++ एक structured programming language है। किसी भी C++ प्रोग्राम को लिखने के लिए कुछ basic syntax rules का पालन करना जरूरी होता है। आइए C++ के basic syntax को विस्तार से समझते हैं।

1. C++ Program Structure

हर C++ प्रोग्राम में कुछ जरूरी components होते हैं:

  • Header Files: Libraries को include करने के लिए (#include)
  • Namespace: Standard C++ functions को उपयोग में लाने के लिए (using namespace std;)
  • Main Function: प्रोग्राम की शुरुआत (entry point) जो int main() होता है।
  • Statements: प्रोग्राम के आदेश, जिन्हें semicolon (;) से खत्म किया जाता है।

2. Basic C++ Program Example

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

Explanation:

  • #include <iostream> — यह Input/Output के लिए Standard Library है।
  • using namespace std; — std namespace का उपयोग करता है ताकि cout, cin जैसे functions सीधे उपयोग हो सकें।
  • int main() — प्रोग्राम का starting point।
  • cout << "Hello, World!" << endl; — स्क्रीन पर टेक्स्ट print करता है।
  • return 0; — प्रोग्राम की सफल समाप्ति को दर्शाता है।

3. Comments (टिप्पणियाँ)

Comments का उपयोग प्रोग्राम में notes लिखने के लिए किया जाता है, जो कंपाइलर ignore करता है।

  • Single-line comment: // यह एक comment है
  • Multi-line comment: /* यह एक multi-line comment है */

4. Case Sensitivity

C++ case-sensitive language है, मतलब Variable और variable अलग-अलग होते हैं।

5. Semicolon (;) का महत्व

C++ में हर statement के अंत में semicolon (;) होना जरूरी है, जिससे कंपाइलर को पता चलता है कि statement खत्म हो गया।

6. Identifiers (पहचान चिन्ह)

Variables, functions, या objects के नाम identifiers कहलाते हैं।

  • अक्षर (A-Z, a-z), digits (0-9), और underscore (_) का उपयोग किया जा सकता है।
  • पहला character letter या underscore होना चाहिए, digit नहीं।
  • Keywords जैसे int, return, if identifiers नहीं हो सकते।

7. Keywords

C++ के कुछ reserved words होते हैं जिन्हें आप नाम के रूप में उपयोग नहीं कर सकते, जैसे:

int, float, return, if, else, while, for, switch, case, class, public, private, void, static, const आदि।

8. White Space

C++ में space, tab, newline जैसे white space को ignore किया जाता है। ये केवल code को readable बनाने के लिए होते हैं।

निष्कर्ष

C++ का basic syntax सीखना programming की शुरुआत के लिए बहुत जरूरी है। सही syntax के बिना प्रोग्राम कंपाइल नहीं होगा। ऊपर बताए गए नियमों का ध्यान रखकर आप आसानी से अपना पहला C++ प्रोग्राम लिख सकते हैं। अभ्यास से आपको और बेहतर समझ आएगी।