-3
#include <stdio.h>

int main(int n){

int n;

printf("%d/n",n);

return 0;

}

我了解 main 中的论点是如何工作的,并就我在这段代码中做错了什么征求建议。

4

7 回答 7

3

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:

  1. The value of argc shall be nonnegative.
  2. argv[argc] shall be a null pointer.
  3. If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.
  4. If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.
  5. The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
于 2013-09-13T18:24:24.173 回答
2

根据语言标准,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 的代码格式?!


1. 这对于所谓的托管实现是正确的;基本上,任何在操作系统下运行的东西。还有独立的实现,通常是嵌入式系统,它们可以自由地以任何他们想要的方式定义程序入口点。

于 2013-09-13T18:41:50.553 回答
1

你应该替换你的主要功能:

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
于 2013-09-13T18:27:11.903 回答
1

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;
}
于 2013-09-13T18:24:23.577 回答
1

参数的main工作方式是这样的:

int main(int argc, char ** argv) {
  // your code
}

您将此文件编译为二进制文件,调用它yourbin,然后在 shell 中执行它时:

./yourbin arg1 arg2

的值argc是传入的参数数量,在本例中为 2,并且argv是指向 C 字符串数组的指针,在本例中"./yourbin", "arg1", "arg2"

因此,您的整数n将是从命令行传入的参数数量。

于 2013-09-13T18:28:37.460 回答
0

在这种情况下,您不需要参数,因为您什么都不做。但是你可能需要它来做类似的事情。您的代码不会编译,因为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 以在命令行参数中使用。

于 2013-09-13T18:25:50.553 回答
0

我建议阅读一些关于 C 语言范围的内容

以下是有关IBM 不得不说的内容和本 C 教程的一些链接

在这种情况下,如果您想在程序中使用 n 变量,您可以查看第一个注释

于 2013-09-13T18:40:30.197 回答