In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the ways to write main functions in C language?" the content in the article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "what are the writing methods of main functions in C language"?
In C language, the main function is the entrance to the program. It can be written in four ways, namely:
/ / write method one
Main ()
{
Printf ("Hello notepad++\ n")
}
/ / Writing method two
Int main (void)
{
Printf ("Hello notepad++\ n")
Return 0
}
/ / the third way of writing
Void main ()
{
Printf ("Hello notepad++\ n")
}
/ / Writing method 4
Int main (int argc, char * argv [])
{
Printf ("Hello notepad++\ n")
}
The first three without parameters are more common ways of writing. What is the meaning of the two parameters in the fourth way? Let's give a brief introduction.
First of all, the main function cannot be called by other functions, so the actual values of the formal parameters argc and argv cannot be passed in from other functions, so where should the parameters be passed in?
Is obtained from the operating system command line. So these two parameters are also called command-line arguments, where argc is the number of command-line arguments and argv is the first address of the string command-line arguments.
Let's look at a code example:
# include
/ / Writing method 4
Int main (int argc, char * argv [])
{
Int i
Printf (">\ n")
Printf ("argc =% d\ n", argc)
For (I = 0; I)
< argc; i++ ) { printf("argv[%d] = %s\n",i, argv[i]); } printf("Hello notepad++ \n"); return 0; } 键入组合键【win+r】打开运行窗口,输入cmd并回车进入命令行环境:Enter the gcc hello.c-o hello.exe command to compile, and then enter hello.exe cmd1 cmd2 cmd3 cmd4 cmd5 (the general form of the command line at the DOS prompt is: C:\ > executable file name parameter parameter. ) pass parameters to the main function, and the output is as follows:
It can be seen that argv is an array of string pointers, its element values are the first address of each string on the command line (parameters are treated as strings), and argc is the length of the pointer array, that is, the number of parameters. Thank you for your reading, the above is the content of "what is the writing of main function in C language?" after the study of this article, I believe you have a deeper understanding of the writing of main function in C language, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.