“From today onwards, study hard and make progress every day”
Repetition is the best method for memory; spend one minute each day to remember the basics of C language.
“Essential Knowledge Points for C Language Beginners: 100 Articles Series”“
6. Detailed Explanation of the main() Function: The Entry Point of C Programs
1. Basic Form of the main Function
In C language, the main function is the starting point of every program. The standard form is as follows:
int main(void) {
// Program code
return 0;
}
Or in the form with command line arguments:
int main(int argc, char *argv[]) {
// Program code
return 0;
}
2. Components of the main Function
- 1. Return Type: Must be of type int
- 2. Function Name: Fixed as main
- 3. Parameter List: Can be without parameters (void) or with parameters
- 4. Function Body: Contains the block of code that executes statements
- 5. Return Value: Typically 0 indicates success, non-zero indicates an error
3. Detailed Explanation of the Parameters of the main Function
The prototype of the main function with parameters:
int main(int argc, char *argv[])
- • argc: Argument count, indicates the number of command line arguments
- • argv: Argument vector, an array of strings that stores the specific arguments
Example program:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Program Name: %s\n", argv[0]);
printf("Number of Arguments: %d\n", argc - 1);
for(int i = 1; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
4. Return Value of the main Function
- 1. Returning 0 indicates the program ended normally
- 2. Returning a non-zero value indicates the program ended abnormally
- 3. The operating system can capture this return value
Example of checking the return value (Linux/bash):
./myprogram
echo $? # Displays the return value of the last command
5. Common Error Forms
- 1. Error: Omitting the return type
main() { // Does not conform to standard
// ...
}
- 2. Error: Using void as the return type
void main() { // Does not conform to standard
// ...
}
- 3. Error: Non-standard parameter list
int main(int argc, string argv[]) { // string is not a standard C type
// ...
}
6. Special Forms Explanation
- 1. The third form of parameters (environment variables):
// Legal but not recommended
int main(int argc, char *argv[], char *envp[])
- 2. Writing without parameter names:
int main(int, char **) // Legal but not recommended
7. Best Practice Recommendations
- 1. Always explicitly specify the return type as int
- 2. Even if no parameters are needed, it is recommended to use void to indicate this clearly
- 3. It is recommended to use standard macros for return values:
#include <stdlib.h>
return EXIT_SUCCESS; // Equivalent to return 0
return EXIT_FAILURE; // Non-zero error code
- 4. Pay attention to boundary checks when handling parameters:
if(argc < 2) {
printf("Usage: %s <parameter>\n", argv[0]);
return EXIT_FAILURE;
}
———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author, some content and images are sourced from the internet and AI, please feel free to consume, opinions are for learning reference only~~]

“If you like C, please like it”
Click the bottom right corner to view
“