Why We Include stdio.h, int main, and return 0 in a C Program

Why We Include stdio.h, int main, and return 0 in a C Program

In this post, we’ll break down the essential parts of a simple C program. We’ll cover why we include certain files, the purpose of int main, and the meaning of return 0.


Example: A Simple C Program to Print “Hello, World!”

C
#include<stdio.h>

int main() {
    printf("hello world");
    return 0;
}

Step 1: #include <stdio.h>

The first line in the program is #include <stdio.h>. This line tells the preprocessor to include the stdio.h file, which contains the standard input-output functions like printf and scanf.

Explanation:

  • #include: This is a preprocessor directive that includes files before actual compilation begins.
  • <stdio.h>: The file stdio.h is included to access I/O functions needed in the program.

Pro Tip: Always include stdio.h if your program uses input-output functions like printf or scanf.


Step 2: The main() Function

In C, int main() is the starting point of program execution. When you run a C program, the operating system initiates execution by calling the main() function.

Explanation:

  • Return Type: The return type int means the function returns an integer value to indicate the program’s status after execution.
  • Return Value:
  • Returning 0 indicates successful execution.
  • Returning a non-zero value (error code) indicates an error in the program.
C
int main() {
    // code goes here
    return 0;
}

Tip: return 0 indicates the program ran successfully. If any system error occurred, a non-zero value would be returned to signal failure.


Step 3: Curly Braces { }

Since C is a block-structured language, curly braces { } are used to group statements together to form a block of code. Everything within { and } in main() is treated as part of the function.

  • Opening Brace {: Marks the beginning of the code block.
  • Closing Brace }: Marks the end of the code block.

Step 4: printf("hello world");

The printf function, defined in stdio.h, is used to print text to the screen. Here, it prints “hello world”.

  • Function: printf is a standard function that outputs text.
  • Usage:
  • Every statement in C, including printf, must end with a semicolon ;.
C
printf("hello world"); // Prints "hello world" to the screen.

Note: If stdio.h is not included, the compiler will issue a warning about an implicit declaration of printf.


Step 5: return 0

The return statement in main() is crucial as it provides the program’s exit status.

  • Returning 0: Indicates successful completion.
  • Returning Non-Zero: Indicates an error or unsuccessful execution.

Rule: Always explicitly return 0 in main() to signify successful execution, especially if the program completes without errors.


Full Example Recap

C
#include<stdio.h>

int main() {
    printf("hello world"); // Outputs "hello world" to the screen.
    return 0; // Indicates successful execution
}

By understanding these fundamental parts of a C program, you’re ready to write and execute simple programs successfully.

1 Comment

  1. H.B

    creative 👏💯
    Thank you sir , good job 👍

Leave a Reply

Your email address will not be published. Required fields are marked *