#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.
#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.
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:
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?!
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;
}
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
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.
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.
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