First Program – Hello World
पहला Program – Hello World
Your First C Program
The tradition in programming is that the first program you write in any language prints "Hello, World!" on the screen. It tests that your development environment is set up correctly and shows you the basic structure of a program.
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
Line-by-Line Explanation
Line 1: #include <stdio.h>
This is a preprocessor directive. It tells the compiler to include the contents of the stdio.h (Standard Input Output) header file. This file contains the definition of printf() and scanf(). Without this line, printf would be unknown to the compiler.
Line 2: int main() { }
This is the main function — the entry point of every C program. When you run a C program, execution always starts from main(). The int before main means the function returns an integer value.
Line 3: printf("Hello, World!\n");
printf() is a library function that prints text to the screen. The text inside double quotes is called a string literal. \n is an escape sequence that means "newline" — it moves the cursor to the next line after printing.
Line 4: return 0;
The return 0; statement returns the value 0 to the operating system, indicating that the program ran successfully. A non-zero return value typically indicates an error.
How to Compile and Run
# Step 1: Save your file as hello.c # Step 2: Compile gcc hello.c -o hello # Step 3: Run (Linux/Mac) ./hello # Step 3: Run (Windows) hello.exe
Variations
#include <stdio.h> int main() { printf("Welcome to CodeKaFunda!\n"); printf("Learning C Programming\n"); printf("Beginner to Advanced\n"); return 0; }
Common Errors in First Program
| Error | Cause | Fix |
|---|---|---|
| error: stdio.h not found | Missing #include | Add #include <stdio.h> |
| warning: implicit declaration of printf | Missing stdio.h | Add #include <stdio.h> |
| Missing ; before } | Forgot semicolon | Add ; after printf statement |
| undefined reference to main | No main function | Write int main() { } |
Summary
- Every C program starts execution from
main() #include <stdio.h>is needed to use printf and scanf- Statements end with semicolon
; - Code blocks are enclosed in curly braces
{ } printf()displays text,\nmoves to next linereturn 0;signals successful program completion
आपका पहला C Program
Programming में tradition है कि किसी भी language में पहला program "Hello, World!" screen पर print करता है। यह test करता है कि आपका development environment सही setup हुआ है।
#include <stdio.h> int main() { printf("Namaste Duniya!\n"); return 0; }
Line-by-Line Explanation
Line 1: #include <stdio.h>
यह एक preprocessor directive है। यह compiler को कहता है कि stdio.h (Standard Input Output) header file को include करे। इस file में printf() और scanf() की definition है। इस line के बिना, compiler printf को नहीं पहचानेगा।
Line 2: int main() { }
यह main function है — हर C program का entry point। जब आप C program run करते हैं, execution हमेशा main() से शुरू होती है।
Line 3: printf("Namaste Duniya!\n");
printf() एक library function है जो text screen पर print करता है। Double quotes के अंदर का text string literal कहलाता है। \n newline escape sequence है — यह cursor को अगली line पर ले जाता है।
Line 4: return 0;
return 0; operating system को 0 return करता है, जो बताता है कि program successfully चला। Non-zero value typically error indicate करती है।
Compile और Run कैसे करें
# Step 1: hello.c नाम से save करें # Step 2: Compile करें gcc hello.c -o hello # Step 3: Run करें (Linux/Mac) ./hello # Step 3: Run करें (Windows) hello.exe
Common Errors
| Error | कारण | Fix |
|---|---|---|
| stdio.h not found | #include missing | #include <stdio.h> add करें |
| Missing ; | Semicolon भूल गए | printf के बाद ; add करें |
| undefined reference to main | main function नहीं है | int main() { } लिखें |
सारांश
- हर C program
main()से execute होना शुरू होता है #include <stdio.h>printf और scanf के लिए ज़रूरी है- Statements semicolon
;से खत्म होते हैं - Code blocks curly braces
{ }में होते हैं printf()text display करता है,\nअगली line पर जाता हैreturn 0;successful completion signal करता है