#include <stdio.h>
int main(int n){
int n;
printf("%d/n",n);
return 0;
}
我了解 main 中的论点是如何工作的,并就我在这段代码中做错了什么征求建议。
main
function prototype can be only one of these:
int main();
int main(int argc, char *argv[]); (*) Check my comment for this one.
int main(int argc, char **argv);
If you look into last two prototypes, there are two arguments. First is the number of the arguments passed to the program, and the second one is the list of the arguments. The first argument (argv[0]
) is always reserved for the program name.
So you could do something like this:
int main(int argc, char** argv)
{
int n = 0;
if(argc > 1)
{
// Paramenters are sent as strings, so you need to cast it to the int
char *end;
n = strtol(argv[1], &end, 10);
if (*end)
{
printf("Please pass the number for the argument!");
return 0;
}
printf("%d\n", n);
}
return 0;
}
Now, you can pass that argument to the program (./program_name 15
) and it should print it out.
Note: atoi
is here only for the demonstration purposes.
Quote from standard:
The function called at program startup is named
main
. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;) or in some other implementation-defined manner.
If they are declared, the parameters to the main function shall obey the following constraints:
根据语言标准,main
采用以下形式之一:
int main( void )
或者
int main( int argc, char **argv ) // or char *argv[]
在第二种形式中,argc
包含传递给 的命令行参数的数量main
,而argv
是包含参数本身的字符串向量。第一个参数 ( argv[0]
) 是用于调用程序的命令,因此argc
始终至少为 1 1。
一个实现可以自由定义额外的签名main
- 检查你的编译器文档。
快速而肮脏的示例:从命令行获取两个整数,将它们相加,然后打印结果(假设 C99):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int convert( char *arg, int *val )
{
int result = 1;
char *chk;
int x = strtol( arg, &chk, 10 );
if ( !isspace( *chk ) && *chk != 0 )
{
result = 0;
fprintf( stderr, "%s is not a valid integer!\n", arg );
return 0;
}
*val = x;
return 1;
}
int main( int argc, char **argv )
{
if ( argc < 3 )
{
fprintf( stderr, "USAGE: %s x y\n", argv[0] );
exit( 0 );
}
int x, y;
if ( convert( argv[1], &x) && convert( argv[2], &y ))
{
printf( "%d\n", x + y );
}
return 0;
}
啊,谁破坏了 IE 的代码格式?!
你应该替换你的主要功能:
int main(int argc, char* argv[]){
for(int i = 0; i < argc; i++){
printf("%s\n",argv[i]);
}
return 0;
}
然后,当您从控制台调用程序时
./foo 1 2 3
您将获得以下输出:
foo
1
2
3
To work with arguments, you need two parameters:
#include <stdio.h>
int main (int argc, char *argv[])
{
//Do someting with argv
return 0;
}
argv is an array of strings, which are null terminated and contain the arguments (the first argument is the name of your executable file).
argc is the count of arguments (the length of the array argv).
Your code could work this way:
#include <stdio.h>
int main(int argc, char *argv[]){
if(argc > 1)
printf("%s\n",argv[1]);
else
printf("argument missing.\n");
return 0;
}
参数的main
工作方式是这样的:
int main(int argc, char ** argv) {
// your code
}
您将此文件编译为二进制文件,调用它yourbin
,然后在 shell 中执行它时:
./yourbin arg1 arg2
的值argc
是传入的参数数量,在本例中为 2,并且argv
是指向 C 字符串数组的指针,在本例中"./yourbin", "arg1", "arg2"
因此,您的整数n
将是从命令行传入的参数数量。
在这种情况下,您不需要参数,因为您什么都不做。但是你可能需要它来做类似的事情。您的代码不会编译,因为int main(int)
它不是 C 提供的重载主要方法之一。
#include <stdio.h>
int anotherMethod(int a);
int main(){
int n = anotherMethod(5);
printf("%d/n",n);
return 0;
}
int anotherMethod(int a){
a = a + 5;
return a;
}
不过,参数可以传递给 main 以在命令行参数中使用。