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

C++ का पहला Program

आपका पहला C++ program पहली बार hello कहने जैसा है। आप एक main function लिखते हैं जो screen पर एक message दिखाता है और फिर खत्म हो जाता है।
Syntax
cpp
#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

cpp
// 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;
}

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

cpp
// 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;
}

Header Files

Header files उस functionality को declare करती हैं जो core language में built-in नहीं है, इसलिए आपको इन्हें #include से explicitly ले आना होता है।

<iostream> specifically cout, cin, और बाकी stream objects declare करता है — इसे include किए बिना, compiler cout को बिल्कुल नहीं पहचानेगा।

उदाहरण: Header Files

cpp
// 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;
}

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

cpp
// 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;
}

Zero Return करना

main() के अंत में return 0; सिर्फ एक formality नहीं है — यह वह exit code है जो operating system को मिलता है, और convention से 0 का मतलब है program successfully खत्म हुआ।

इसकी बजाय एक nonzero value return करना signal देता है कि कुछ गलत हुआ, जिसे shell scripts और दूसरे programs check कर सकते हैं।

उदाहरण: Returning Zero

cpp
#include <iostream>

int main() {
	std::cout << "Program finished successfully" << std::endl;
	return 0; // 0 = success exit code
}
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. #include <iostream> भूल जाना, जो cout को undefined बना देता है और एक compile error produce करता है।
  2. std:: या using namespace std; के बिना cout << "Hello" लिखना, ताकि compiler report करे कि cout declared नहीं है।
  3. int main() की बजाय void main() इस्तेमाल करना, जो standard C++ नहीं है और strict compilers पर compile नहीं हो सकता।

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.