-2

The code is like (real noob question) :

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

I know, it means I have to give some arguments while executing in the terminal, but the code does not require any arguments or information from the user. I don't know what to give as the argument?

Ronit
  • 127
  • 1
  • 8
  • 2
    Just compile and run without argument. – thibsc Mar 29 '18 at 07:59
  • IDEs typically have an option where you can provide command line parameters as input when debugging. But command line parameters aren't mandatory, unless you programmatically enforce that a certain number of parameters are necessary to run your program. – Lundin Mar 29 '18 at 08:18

3 Answers3

3

I have to give some arguments while executing in the terminal,

No, you don't have to. You may give some arguments. There are conventions regarding program arguments (but these are just conventions, not requirements).

It is perfectly possible to write some C code with a main without argument, or with ignored arguments. Then you'll compile your program into some executable myprog and you just type ./myprog (or even just myprog if your PATH variable mentions at the right place the directory containing your myprog) in your terminal.

The C11 standard n1570 specifies in §5.1.2.2.1 [Program startup] that

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.

The POSIX standard specifies further the relation between the command line, the execve function, and the main of your program. See also this.

In practice I strongly recommend, in any serious program running on a POSIX system, to give two argc & argv arguments to main and to parse them following established conventions (In particular, I hate serious programs not understanding --help and --version).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
3

For example:

#include <stdio.h>

int main(int argc, char **argv)
{
    printf("Hello World\n");  
} 

Compile with GCC,

$gcc prog.c -o prog

$./prog

Hello World

So, If you do not use agave in your code, then, there is no need to provide an argument.

msc
  • 33,420
  • 29
  • 119
  • 214
0

You can always pass some number of arguments or pass nothing unless you are checking for the number of arguments and arguments passed (or forcing compiler to do so). Your command interpreter has no idea what your program is going to do with the passed argument or whether the program need any argument. It's your program which takes care of all these things.
For example,

int main(void){
    return 0;
} 

you can pass any number of arguments to the above program

$ gcc hello.c -o hello  
$ ./hello blah blah blah 

In case of

int main(int argc, char **argv){
    return 0;
}

you can pass no arguments.

$ gcc hello.c -o hello 
$ ./hello

For

int main(int argc, char **argv){
    if(argc < 3){
        printf("You need to pass two arguments to print those on the terminal\n");
        exit(0);
    }
    else{
         printf("%s %s\n", argv[1], arv[2]);
    }
    return 0;
}

You have to pass two arguments because the program checking the number of arguments passed and using them

$ gcc hello.c -o hello  
$ ./hello Hello world
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    Naming `test` your executable is poor taste (conflict with standard or builtin [test(1)](http://man7.org/linux/man-pages/man1/test.1.html)...) And in practice, you should always ask GCC to give warnings and debug info, so compile with `gcc -Wall -Wextra -g test.c o mytest` then run `./mytest Hello World` – Basile Starynkevitch Mar 29 '18 at 08:40
  • BTW, "any number of arguments" is optimistic. There is a limitation of that number (see [execve(2)](http://man7.org/linux/man-pages/man2/execve.2.html) and its `E2BIG` error), and sadly that limit is easily reachable. – Basile Starynkevitch Mar 29 '18 at 08:45
  • @BasileStarynkevitch; Agreed with all your points. Made corrections. – haccks Mar 29 '18 at 09:16