-3
#include <stdio.h>

int main(int n){

int n;

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

return 0;

}

I wounder how argument in main works and ask for suggestions on what I do wrong with this code.

assylias
  • 321,522
  • 82
  • 660
  • 783
user2682811
  • 175
  • 1
  • 2
  • 5

7 Answers7

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.
Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
  • 1
    The C standard doesn't mention the `int main()` form. It's accepted by most or all compilers, but `int main(void)` is more certain to be correct and is more explicit. (`int main()` happens to be the preferred form in C++, but the empty parentheses don't mean the same thing.) And I'm not sure why you're using `atol()` rather than `atoi()` (neither of which handles errors). – Keith Thompson Sep 13 '13 at 18:29
  • Yes, the only variants, mentioned the footnote: `Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.` are defined for the second case (but however, there is still nothing about `main(void)`, I guess Wikipedia needs an edit). – Nemanja Boric Sep 13 '13 at 18:34
2

Per the language standard, main takes one of the following forms:

int main( void )

or

int main( int argc, char **argv ) // or char *argv[]

In the second form, argc contains the number of command-line arguments passed to main, while argv is a vector of strings containing the arguments themselves. The first argument (argv[0]) is the command used to invoke the program, so argc is always at least 11.

An implementation is free to define additional signatures for main - check your compiler documentation.

Quick and dirty example: take two integers from the command line, add them, and print the result (assumes 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;
}

Ahd who broke code formatting for IE?!


1. This is true for what are called hosted implementations; basically, anything that's running under an operating system. There are also freestanding implementations, which are typically embedded systems, and they are free to define the program entry point any way they want to.
John Bode
  • 119,563
  • 19
  • 122
  • 198
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;
}
Emiswelt
  • 3,909
  • 1
  • 38
  • 56
1

You should replace your main function :

int main(int argc, char* argv[]){

    for(int i = 0; i < argc; i++){
        printf("%s\n",argv[i]);
    }

    return 0;

}

Then, when you will call your program from a console

./foo 1 2 3

You'll get the follwing output :

foo
1
2
3
superzamp
  • 501
  • 6
  • 17
1

The way arguments to main work is like so:

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

You compile this file into a binary, call it yourbin, then when you execute it at a shell:

./yourbin arg1 arg2

The value of argc is the number of arguments passed in, in this case it is 2, and argv is a pointer to an array of C strings, in this case "./yourbin", "arg1", "arg2"

So, your integer n would be the number of arguments passed in from the command line.

tlehman
  • 5,125
  • 2
  • 33
  • 51
0

You don't need the argument in this case as you are doing nothing with it. But you may need it to do something similar to this. Your code won't compile as int main(int) is not one of the overloaded main methods provided by 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;
}

Argument can be passed to main to be used in command line arguments though.

Andromeda
  • 1,370
  • 2
  • 10
  • 15
0

I would recommend to read a little about scope in C

Here are some links on What IBM has to say about and this C tutorial

In this case if you want to use the n variable in your program you can check the first comment

SantiCarta
  • 76
  • 1
  • 6