Anatomy of main() — Your First C Program
hello.c-র প্রতিটি অংশের গভীর ব্যাখ্যা
1. The Smallest Complete C Program
Only six lines, but every single token has a purpose. Run it first, then we will dissect it together.
#include <stdio.h>
int main(void) {
printf("Hello, Bangladesh!\n");
return 0;
}
2. Line 1 — #include <stdio.h>
Lines starting with # are preprocessor directives. They run before the compiler. #include tells the preprocessor to paste the contents of stdio.h right here.
stdio.h is the Standard I/O header. It declares functions like printf, scanf and fopen. Without it, the compiler would not know what printf means.
# দিয়ে শুরু হওয়া লাইনগুলো preprocessor directive। এগুলো compiler চালু হওয়ার আগে execute হয়। #include <stdio.h> মানে হলো — stdio.h ফাইলের সম্পূর্ণ বিষয়বস্তু এই জায়গায় paste হয়ে যাবে। stdio.h-এ printf, scanf ইত্যাদি ফাংশনের ঘোষণা থাকে।<stdio.h> → system header, compiler-এর standard path থেকে খোঁজা হয়।"myheader.h" → আপনার নিজের header, source ফাইলের আশেপাশে খোঁজা হয়।
3. Line 3 — int main(void)
Every C program has exactly one function called main. It is the entry point — where the OS starts executing your program.
main ফাংশন থাকে। এটি হলো entry point — OS এখান থেকেই প্রোগ্রাম চালানো শুরু করে।main function signature-এর চারটি অংশ।
There is also a richer form for reading command-line arguments:
int main(int argc, char *argv[]) {
// argc = argument count, argv = array of strings
}
4. Line 4 — printf(...)
printf stands for "print formatted". It writes text to stdout — the standard output stream (your terminal by default).
| Piece | Meaning | বাংলায় |
|---|---|---|
printf | Function name (from stdio.h) | ফাংশনের নাম |
( ) | Parentheses hold the arguments | ফাংশনে দেওয়া input রাখে |
"..." | String literal (text) | টেক্সট (string) |
\n | Newline character | নতুন লাইন |
; | Statement terminator | statement শেষ করার চিহ্ন |
Common escape sequences:
\n // newline (নতুন লাইন)
\t // tab (ট্যাব)
\\ // literal backslash
\" // literal double quote
\0 // null character — string-এর শেষ
5. Line 5 — return 0;
When main returns, the program ends. The return value is the program's exit status, passed back to the operating system.
0= success (সফল)- non-zero = some kind of error (কোনো সমস্যা)
echo $? চালালে আগের প্রোগ্রামের exit code দেখা যায়। Windows-এ echo %ERRORLEVEL%।6. Printing Multiple Values — Format Specifiers
printf uses format specifiers (%d, %f, etc.) to embed values. Try the live demo below:
#include <stdio.h>
int main(void) {
int age = 20;
float gpa = 3.85f;
char grade = 'A';
const char *name = "Arif";
printf("Name : %s\n", name);
printf("Age : %d\n", age);
printf("GPA : %.2f\n", gpa);
printf("Grade: %c\n", grade);
return 0;
}
| Specifier | Type | বাংলায় |
|---|---|---|
%d | int (decimal) | পূর্ণসংখ্যা |
%f / %.2f | float/double | দশমিক সংখ্যা |
%c | single character | একটি ক্যারেক্টার |
%s | string | টেক্সট |
%x | int as hex | হেক্সাডেসিমাল |
%p | pointer address | পয়েন্টারের ঠিকানা |
%% | literal % | % চিহ্ন নিজেই |
7. Common First-Day Mistakes
❌ Wrong
include <stdio.h> // missing #
int Main(void) { } // capital M
printf("Hello") // missing ;
printf('Hello\n'); // single quotes
return 0 // missing ;
✅ Correct
#include <stdio.h>
int main(void) {
printf("Hello\n");
return 0;
}
main ≠ Main ≠ MAIN। Single quote ' ' শুধু একটি character-এর জন্য, string-এর জন্য double quote " " ব্যবহার করুন।
8. Practice Problems
-
Write a program that prints your name, university, and a fun fact on three separate lines.তিন লাইনে আপনার নাম, বিশ্ববিদ্যালয় এবং একটি মজার তথ্য প্রিন্ট করুন।
✨ Show Answer
ans1.c#include <stdio.h> int main(void) { printf("Name : Arif Hossain\n"); printf("University : North South University\n"); printf("Fun fact : I love building tiny compilers.\n"); return 0; } -
Print a right-angled triangle of
*that is 5 rows tall using onlyprintfand escape sequences.শুধুprintfও escape sequence ব্যবহার করে*দিয়ে ৫ সারির একটি ত্রিভুজ আঁকুন।✨ Show Answer
triangle.c#include <stdio.h> int main(void) { printf("*\n"); printf("**\n"); printf("***\n"); printf("****\n"); printf("*****\n"); return 0; } -
Write a program that uses
%d,%f,%cand%s— all four — in a singleprintf, separated by tabs.একটিprintf-এই চারটি format specifier (%d,%f,%c,%s) ব্যবহার করুন, values tab দিয়ে আলাদা করুন।✨ Show Answer
four.c#include <stdio.h> int main(void) { printf("%d\t%.2f\t%c\t%s\n", 25, 3.14, 'A', "Dhaka"); return 0; } -
Write a program that returns
42frommain. After running, read the exit code on your terminal.একটি প্রোগ্রাম লিখুন যাmainথেকে42return করবে। প্রোগ্রাম চালানোর পরে terminal-এ exit code দেখুন।✨ Show Answer
exit42.c#include <stdio.h> int main(void) { printf("Returning 42 to the OS.\n"); return 42; }Terminal-এ: Linux/macOS →
echo $?, Windows →echo %ERRORLEVEL%চালালে42দেখা যাবে। -
Trick question: what happens if you use
%dbut pass afloatvalue? Predict first, then run below.একটি চতুর প্রশ্ন:%dব্যবহার করে যদিfloatপাস করা হয় তাহলে কী হবে? প্রথমে অনুমান করুন, তারপর নিচে চালিয়ে দেখুন।✨ Show Answer
উত্তর: এটি undefined behavior — অর্থাৎ C standard অনুযায়ী যেকোনো output আসতে পারে। সাধারণত একটি বড় বা আজেবাজে সংখ্যা আসে, কারণ
printfমনে করে argument-এ একটি int আছে, কিন্তু float-এর binary layout সম্পূর্ণ ভিন্ন।mismatch.c#include <stdio.h> int main(void) { float pi = 3.14f; printf("Wrong : %d\n", pi); // undefined behavior printf("Right : %.2f\n", pi); return 0; }শিক্ষা: Format specifier এবং argument-এর type সবসময় মিল রাখুন।
-Wallব্যবহার করলে compiler এই ভুল ধরিয়ে দেয়। -
Write a simple tax calculator: given a price, print the price, the VAT (15%), and the total.একটি সাধারণ ট্যাক্স ক্যালকুলেটর লিখুন: একটি দাম দেওয়া হলে দাম, VAT (15%) এবং মোট প্রিন্ট করুন।
✨ Show Answer
vat.c#include <stdio.h> int main(void) { double price = 1200.00; double vat = price * 0.15; double total = price + vat; printf("Price : %.2f Taka\n", price); printf("VAT : %.2f Taka (15%%)\n", vat); printf("Total : %.2f Taka\n", total); return 0; }লক্ষ্য করুন:
%%দিয়ে শুধু একটি%চিহ্ন প্রিন্ট করা হয়েছে।
Glossary (শব্দকোষ)
| Term | Meaning | বাংলায় |
|---|---|---|
main() | The entry point of every C program. | প্রতিটি C প্রোগ্রামের শুরুর বিন্দু। |
| Entry point | The function the OS calls first when running a program. | OS যে ফাংশনটিকে সবার আগে কল করে। |
| Header file | A .h file containing declarations to be #included. | ঘোষণাসমূহ ধারণকারী .h ফাইল। |
#include | Preprocessor directive that pastes a header into your file. | Header file যুক্ত করার preprocessor directive। |
| Token | The smallest meaningful piece of source code. | সোর্স কোডের সবচেয়ে ছোট অর্থবহ অংশ। |
| Statement | A complete instruction ending with a semicolon. | সেমিকোলনে শেষ হওয়া একটি পূর্ণ নির্দেশ। |
| Expression | Any code fragment that produces a value. | মান উৎপন্নকারী যেকোনো কোড। |
return | Sends a value back from a function and exits it. | ফাংশন থেকে মান ফেরত পাঠিয়ে সেটি শেষ করার নির্দেশ। |
| Exit code | An integer the program returns to the OS (0 means success). | OS-কে ফেরত পাঠানো integer (০ মানে সফল)। |
void | Type meaning "no value" or "no parameters". | "কোনো মান নয়" বা "কোনো parameter নয়" বোঝানো type। |
| Format specifier | A code like %d, %s telling printf how to format output. | printf-কে output ফরম্যাট জানানোর কোড। |
| Standard output (stdout) | The default place text goes when you printf. | printf-এর ডিফল্ট output stream। |
Summary — Module 04
Every C program starts at main. #include brings in headers, printf writes to stdout using format specifiers, and return 0 tells the OS everything went well. You now understand every line of hello.c — and every error message that references them.
main থেকে শুরু হয়। #include header যোগ করে, printf format specifier ব্যবহার করে output লেখে, আর return 0 OS-কে জানায় প্রোগ্রাম সফল হয়েছে। এখন আপনি hello.c-র প্রতিটি লাইন বুঝতে পারবেন এবং তাদের সম্পর্কিত error message-ও।