Structure of a C Program

 

💻 Structure of a C Program (Easy Explanation)

A C program follows a specific structure (format). Understanding this structure helps you write programs correctly 👍


🔹 Basic Structure of C Program

#include <stdio.h> // 1. Header files

int main() { // 2. Main function

printf("Hello, World!"); // 3. Statements (instructions)

return 0; // 4. Return statement
}

🔹 Explanation of Each Part

1. 📂 Header Files

#include <stdio.h>
  • Used to include standard libraries
  • stdio.h is used for input/output functions like printf()

2. 🔑 main() Function

int main()
  • This is the starting point of every C program
  • Execution begins from here

3. 🧾 Body of Program (Statements)

{
printf("Hello, World!");
}
  • Contains instructions to perform tasks
  • Each statement ends with a semicolon ;

4. 🔚 Return Statement

return 0;
  • Ends the program
  • 0 means program executed successfully

🔹 Optional Parts (Advanced)

📝 Comments

// This is a comment
/* Multi-line comment */
  • Used to explain code
  • Ignored by compiler

🔧 User-defined Functions

void myFunction() {
printf("Custom Function");
}
  • Helps in code reusability

🔹 Flow of Execution ▶️

  1. Header files included
  2. main() function starts
  3. Statements execute one by one
  4. Program ends with return 0

🔹 Simple Analogy 🤔

Think of a C program like a recipe 🍲:

  • Header files = Ingredients
  • main() = Cooking start
  • Statements = Steps
  • return 0 = Dish ready

🔹 Summary 📝

A C program mainly has:

  • Header files 📂
  • main() function 🔑
  • Statements 🧾
  • Return statement 🔚

Comments

Popular posts from this blog

Introduction to Computer

History of Computer

Computer Generation