Anatomy of main() — Your First C Program

hello.c-র প্রতিটি অংশের গভীর ব্যাখ্যা

~25 min Beginner 6 practice problems Live code

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.

মাত্র ছয়টি লাইন, কিন্তু প্রতিটি token-এর একটি নির্দিষ্ট কাজ আছে। প্রথমে চালিয়ে দেখুন, এরপর আমরা ধাপে ধাপে প্রতিটি অংশ ব্যাখ্যা করবো।
hello.c
#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 ইত্যাদি ফাংশনের ঘোষণা থাকে।
<angle> vs "quotes" <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.

প্রতিটি C প্রোগ্রামে ঠিক একটি main ফাংশন থাকে। এটি হলো entry point — OS এখান থেকেই প্রোগ্রাম চালানো শুরু করে।
intreturn typeকী ফেরত দেয় mainfunction nameপ্রোগ্রামের শুরু (void)parametersকোনো ইনপুট নেই { ... }function bodyভিতরে কোড থাকে int main(void) { ... } Figure 4.1 — 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).

PieceMeaningবাংলায়
printfFunction name (from stdio.h)ফাংশনের নাম
( )Parentheses hold the argumentsফাংশনে দেওয়া input রাখে
"..."String literal (text)টেক্সট (string)
\nNewline characterনতুন লাইন
;Statement terminatorstatement শেষ করার চিহ্ন

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 (কোনো সমস্যা)
Linux/macOS-এ shell-এ 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:

format.c
#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;
}
SpecifierTypeবাংলায়
%dint (decimal)পূর্ণসংখ্যা
%f / %.2ffloat/doubleদশমিক সংখ্যা
%csingle characterএকটি ক্যারেক্টার
%sstringটেক্সট
%xint as hexহেক্সাডেসিমাল
%ppointer 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;
}
Case matters C ভাষা case-sensitive। main ≠ Main ≠ MAIN। Single quote ' ' শুধু একটি character-এর জন্য, string-এর জন্য double quote " " ব্যবহার করুন।

8. Practice Problems

প্রথমে নিজে চেষ্টা করুন। তারপর Show Answer বাটনে চাপ দিয়ে উত্তরটি দেখুন ও রান করুন।
  1. 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;
    }
  2. Print a right-angled triangle of * that is 5 rows tall using only printf and 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;
    }
  3. Write a program that uses %d, %f, %c and %s — all four — in a single printf, 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;
    }
  4. Write a program that returns 42 from main. After running, read the exit code on your terminal.
    একটি প্রোগ্রাম লিখুন যা main থেকে 42 return করবে। প্রোগ্রাম চালানোর পরে 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 দেখা যাবে।

  5. Trick question: what happens if you use %d but pass a float value? 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 এই ভুল ধরিয়ে দেয়।

  6. 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 (শব্দকোষ)

TermMeaningবাংলায়
main()The entry point of every C program.প্রতিটি C প্রোগ্রামের শুরুর বিন্দু।
Entry pointThe function the OS calls first when running a program.OS যে ফাংশনটিকে সবার আগে কল করে।
Header fileA .h file containing declarations to be #included.ঘোষণাসমূহ ধারণকারী .h ফাইল।
#includePreprocessor directive that pastes a header into your file.Header file যুক্ত করার preprocessor directive।
TokenThe smallest meaningful piece of source code.সোর্স কোডের সবচেয়ে ছোট অর্থবহ অংশ।
StatementA complete instruction ending with a semicolon.সেমিকোলনে শেষ হওয়া একটি পূর্ণ নির্দেশ।
ExpressionAny code fragment that produces a value.মান উৎপন্নকারী যেকোনো কোড।
returnSends a value back from a function and exits it.ফাংশন থেকে মান ফেরত পাঠিয়ে সেটি শেষ করার নির্দেশ।
Exit codeAn integer the program returns to the OS (0 means success).OS-কে ফেরত পাঠানো integer (০ মানে সফল)।
voidType meaning "no value" or "no parameters"."কোনো মান নয়" বা "কোনো parameter নয়" বোঝানো type।
Format specifierA 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.

প্রতিটি C প্রোগ্রাম main থেকে শুরু হয়। #include header যোগ করে, printf format specifier ব্যবহার করে output লেখে, আর return 0 OS-কে জানায় প্রোগ্রাম সফল হয়েছে। এখন আপনি hello.c-র প্রতিটি লাইন বুঝতে পারবেন এবং তাদের সম্পর্কিত error message-ও।

Next Module → Bits, two's complement ও IEEE 754 — কম্পিউটার আসলে সংখ্যা কীভাবে রাখে।