C++ का पहला Program
#include <iostream>
int main() {
// statements
std::cout << value << std::endl;
return 0;
}
Main Function
हर C++ program को exactly एक main() function चाहिए, क्योंकि यही वह function है जिसे operating system आपका program शुरू करने के लिए call करता है।
Execution हमेशा main() के अंदर पहली line पर शुरू होता है और वहाँ से जारी रहता है, चाहे file में दूसरे functions कहीं भी defined हों।
उदाहरण: The Main Function
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << "Execution starts here" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Output Print करना
cout, 'character output' का short, << stream insertion operator इस्तेमाल करके console को data भेजता है, जिसे आप cout << "Age: " << age जैसे एक statement में कई values print करने के लिए chain कर सकते हैं।
<< को एक arrow की तरह सोचें जो data को एक बार में एक piece output stream में push करता है।
उदाहरण: Printing Output
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Print to the console with cout
std::cout << "Age: " << age << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Header Files
Header files उस functionality को declare करती हैं जो core language में built-in नहीं है, इसलिए आपको इन्हें #include से explicitly ले आना होता है।
<iostream> specifically cout, cin, और बाकी stream objects declare करता है — इसे include किए बिना, compiler cout को बिल्कुल नहीं पहचानेगा।
उदाहरण: Header Files
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << "cout comes from <iostream>" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Standard Namespace
std namespace standard library द्वारा defined हर नाम को साथ group करता है, जो आपके अपने code के साथ naming collisions रोकता है।
using namespace std; लिखना आपको fully qualified std::cout की बजाय cout लिखने देता है, हालांकि बड़े projects अक्सर इसे skip करते हैं और ambiguity avoid करने के लिए std:: explicitly लिखते हैं।
उदाहरण: Standard Namespace
// Include std::cout and std::cin
#include <iostream>
// Lets us write cout instead of std::cout
using namespace std;
// Program execution starts in main()
int main() {
// Print to the console with cout
cout << "No need for std:: prefix now" << endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Zero Return करना
main() के अंत में return 0; सिर्फ एक formality नहीं है — यह वह exit code है जो operating system को मिलता है, और convention से 0 का मतलब है program successfully खत्म हुआ।
इसकी बजाय एक nonzero value return करना signal देता है कि कुछ गलत हुआ, जिसे shell scripts और दूसरे programs check कर सकते हैं।
उदाहरण: Returning Zero
#include <iostream>
int main() {
std::cout << "Program finished successfully" << std::endl;
return 0; // 0 = success exit code
}
Login to try C/C++/Java/PHP code in the editor
#include <iostream>भूल जाना, जोcoutको undefined बना देता है और एक compile error produce करता है।std::याusing namespace std;के बिनाcout << "Hello"लिखना, ताकि compiler report करे किcoutdeclared नहीं है।int main()की बजायvoid main()इस्तेमाल करना, जो standard C++ नहीं है और strict compilers पर compile नहीं हो सकता।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: