← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 5 of 15

C++ Syntax और Structure

C++ के strict spelling और punctuation rules हैं, careful grammar वाली किसी language की तरह। Statements semicolons से खत्म होते हैं, curly braces steps को group करते हैं, और capital letters मायने रखते हैं।
Syntax
cpp
#include <header>

return_type function_name() {
  statement;
  statement;
}

Semicolons

C++ में हर complete statement को एक semicolon से खत्म होना चाहिए, जो compiler को exactly बताता है कि एक instruction कहाँ रुकता है और अगला कहाँ शुरू होता है।

एक भूल जाना beginners की सबसे common errors में से एक है, और यह आमतौर पर compiler को आपके actually miss की गई line की बजाय अगली line पर एक error report करने का कारण बनता है।

उदाहरण: Semicolons

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print to the console with cout
	std::cout << x << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Curly Braces

Curly braces { } कई statements को एक single block में group करते हैं, यह mark करते हुए कि एक function body, loop, या if-statement कहाँ शुरू और खत्म होता है।

Braces के बिना, if जैसे कुछ constructs सिर्फ इसके तुरंत बाद वाले single statement पर apply होते हैं, जो बाद में एक दूसरी line add होने पर subtle bugs का एक frequent source है।

उदाहरण: Curly Braces

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Check whether true
	if (true) {
		// Print to the console with cout
		std::cout << "Inside the block" << std::endl;
		// Print to the console with cout
		std::cout << "Both lines run" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Tokens

एक token सबसे छोटा meaningful हिस्सा है जिसे compiler आपकी source code पढ़ते समय पहचानता है — int जैसे keywords, variable names जैसे identifiers, literal values, और + जैसे operators सब tokens हैं।

Tokens समझना compiler error messages समझाने में मदद करता है, जो अक्सर उस specific token को point करते हैं जहाँ parsing fail हुई।

उदाहरण: Tokens

cpp
#include <iostream>

int main() {
	int total = 5 + 3; // int, total, =, 5, +, 3 are all tokens
	std::cout << total << std::endl;
	return 0;
}

Case Sensitivity

C++ uppercase और lowercase letters को पूरी तरह अलग characters मानता है, इसलिए main और Main interchangeable नहीं हैं — सिर्फ lowercase main को आपके program के entry point की तरह पहचाना जाता है।

यही rule मतलब है total और Total दो बिल्कुल अलग variables की तरह साथ exist कर सकते हैं, जो carelessly इस्तेमाल होने पर confusing bugs cause कर सकता है।

उदाहरण: Case Sensitivity

cpp
#include <iostream>

int main() {
	int total = 1;
	int Total = 2; // a completely different variable from 'total'
	std::cout << total << " " << Total << std::endl;
	return 0;
}

Indentation

Indentation का इस पर कोई असर नहीं होता कि compiler आपका code कैसे parse करता है — सिर्फ braces structure define करते हैं — लेकिन consistent indentation ही nested loops और conditionals को एक human के लिए readable बनाता है।

ज़्यादातर teams एक consistent indentation style enforce करते हैं exactly इसलिए क्योंकि compiler की यहाँ leniency बिना किसी style के code को unreadable बनाना आसान बना देती है।

उदाहरण: Indentation

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 2; i++) {
		// Check whether i == 0
		if (i == 0) {
			// Print to the console with cout
			std::cout << "Consistent indentation aids readability" << std::endl;
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक statement के बाद semicolon भूल जाना, ताकि compiler अगली line पर एक error report करे।
  2. cout और int की जगह Cout या INT लिखना, क्योंकि C++ case-sensitive है और वे नाम exist नहीं करते।
  3. एक { को matching } के बिना छोड़ना, file के अंत में 'expected }' जैसी errors cause करते हुए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.