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

C का पहला Program

आपका पहला C program पहली बार hello कहने जैसा है। आप एक main function लिखते हैं जो screen पर एक message दिखाता है और फिर खत्म हो जाता है।
Syntax
c
#include <stdio.h>

int main() {
  printf("text");
  return 0;
}

Main Function

हर C program का execution main नाम के function के अंदर शुरू होता है -- operating system specifically इसी function को ढूंढता है और इसे पहले call करता है, चाहे आपकी file में कितने भी दूसरे functions define हों।

उदाहरण: The Main Function

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Execution starts here, inside main.");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Output Print करना

printf screen पर formatted text लिखता है और यही तरीका है जिससे एक C program कोई भी visible output produce करता है; इसके बिना (या किसी similar output call के बिना), आपका program perfectly चल सकता है और आपको कभी पता ही नहीं चलेगा, क्योंकि screen पर कुछ भी नहीं दिखेगा।

उदाहरण: Printing Output

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Hello, World!");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Zero Return करना

main से 0 return करना operating system (और आपका program launch करने वाले किसी भी script या tool) को एक signal है कि execution बिना errors के पूरा हुआ -- non-zero value return करना failure report करने का conventional तरीका है।

उदाहरण: Returning Zero

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Program completed successfully.");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Header Files

#include <stdio.h> C standard library से printf और scanf जैसे standard input/output functions की declarations ले आता है; इसके बिना, compiler को पता ही नहीं होगा कि ये functions exist करते हैं और यह इन्हें call करने से मना कर देगा।

उदाहरण: Header Files

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("stdio.h provides printf and scanf declarations.");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Program Execute करना

अपना program compile करना और actually run करना -- सिर्फ इसे cleanly compile करवाना नहीं -- यह confirm करने का इकलौता तरीका है कि यह वैसे ही behave करता है जैसी आपने उम्मीद की थी; zero compiler errors वाला program भी पूरी तरह गलत output produce कर सकता है।

उदाहरण: Executing the Program

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare result and set it to 5 / 2
	int result = 5 / 2;
	// Print formatted text to the screen
	printf("Result: %d", result);
	// 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. #include <stdio.h> भूल जाना, ताकि printf undeclared रहे और compiler warn करे या fail हो जाए।
  2. printf("Hello") में \n छोड़ देना, ताकि shell prompt text के ठीक बाद दिखे।
  3. int main() की बजाय void main() इस्तेमाल करना, जो portable standard C नहीं है।

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.