Apply for Zend Framework Certification Training

c



< Operators in C Last >



Purpose of int main(),char main(),void main() with examples
In C, main() is the starting point of program execution. The return type of main() tells the operating system/compiler what the function returns after the program finishes.
1. int main() — Recommended and Standard
int main() means the main() function returns an integer value to the operating system.
Usually, we return 0 to indicate that the program completed successfully.
#include <stdio.h>
int main(){
    printf("Hello, World!");
    return 0;
}
Purpose:
int → return type is integer.
return 0; → tells the operating system that the program executed successfully.
This is the standard form of main() in modern C.
You can also write:
int main(void){
    printf("Hello, World!");
    return 0;
}
Here, void inside the parentheses means the function takes no arguments.
2. char main() — Not Appropriate for main()
char main() means the main() function is expected to return a character value.
For example, an ordinary function can return char:
#include <stdio.h>
char getGrade(){
    return 'A';
}
int main(){
    char grade = getGrade();
    printf("Grade = %c", grade);
    return 0;
}
Here, getGrade() returns a character 'A'.
However, you should not use char main() for the C program's entry point. The standard forms are based on int, such as:
int main()
or
int main(void)
3. void main() — Non-standard
void main() means that main() does not return a value.
Example:
#include <stdio.h>
void main(){
    printf("Hello, World!");
}
You may see this in some old books, compilers, or tutorials, and some compilers may accept it.
However, void main() is not the standard form of main() in C.
Prefer:
int main(){
    printf("Hello, World!");
    return 0;
}
Quick Comparison
Form                                 Meaning                                                             Recommended?
int main()                          main() returns an integer                                          Yes
int main(void)                    main() returns an integer and takes no arguments      Yes
char main()                        main() returns a character                                         No
void main()                         main() returns nothing                                              No
Easy way to remember
int main()
    ↓
Program starts
    ↓
Statements execute
    ↓
return 0
    ↓
Program ends successfully
For students: The best practice is to always use int main() or int main(void) in standard C programs.

< Operators in C Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top