C++ Syntax और Structure
In this page:
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int total = 5 + 3; // int, total, =, 5, +, 3 are all tokens
std::cout << total << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int total = 1;
int Total = 2; // a completely different variable from 'total'
std::cout << total << " " << Total << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक statement के बाद semicolon भूल जाना, ताकि compiler अगली line पर एक error report करे।
coutऔरintकी जगहCoutयाINTलिखना, क्योंकि C++ case-sensitive है और वे नाम exist नहीं करते।- एक
{को matching}के बिना छोड़ना, file के अंत में 'expected }' जैसी errors cause करते हुए।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: