-2

This is my main function code

os2.c

#include <stdio.h>
int main( int argc, char *argv[]){
 int item;
 item = argc;
 printf("%d", item);

 return 0;
}

I run the following command line in Ubuntu's terminal

gcc -o test os2.c
./test 5

The result printed is equal to 2 instead of 5, What's wrong with my code?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 9
    I suggest you read some [reference documentation](http://en.cppreference.com/w/c/language/main_function). – user694733 Aug 29 '17 at 06:21

5 Answers5

3

First, your question is not just for Ubuntu/Linux.
It works in Windows and Macintosh too including every OS using the terminal.

argc = argument count: the number of argument that are passed to a program
argv = argument vector: passed arguments

argc is an int that count the arguments of your command line.

I run the following command line in Ubuntu's terminal: gcc -o test os2.c
./test 5

Here your arguments are

  1. argv[0] == ./test
  2. argv[1] == 5

So we conclude that:

argc is 2 based on the number of arguments above.
argv contains the arguments that we've passed to the program.

This is an example of how you use arguments in the right way:

source.c

#include <stdio.h>

int main(int argc, char **argv){
    for(int i = 0;i < argc;i++)
        printf("arg %d : %s\n",i,argv[i]);

    return 0;
}

Command Line

gcc -o test source.c
./test this is a test

Output

arg 0 : ./test
arg 1 : this
arg 2 : is
arg 3 : a
arg 4 : test

But anyways, I recommend to never use the first argument; It'd be a gap in your software cause it can be hacked easily. There're many ways to do whatever you want in C.

Now; You should know what you've to do if you want to get the value 5 instead of counting the number of arguments that you've passed to your application which in the OP case were 2.

Beyondo
  • 2,952
  • 1
  • 17
  • 42
  • 1
    `argv` is often regarded as short for 'argument vector' — it is an array of strings. 'Argument values' more or less works as a synonym. – Jonathan Leffler Aug 29 '17 at 13:46
2

Nothing wrong with code. You are passing two arguments ./test and 5. So it shows 2. It is the number of arguments passed to the program.

argc : argument count.

To get the argument passed you can do this.

printf("%s",argv[1]);

In general you check it like this:

if(argc!=2)
{
  printf("Usage: ./program-name argument\n");
  return EXIT_FAILURE; // failure-considering you are writing this in main.
}
// do your work here.

But in argv[1] you will get the char string. To get an integer out of it, you have to convert it using something like atoi or your custom conversion function. Also you can use strtol() which is useful in conversion from string to long.

1. In case of main return EXIT_FAILURE works but not in other functions but exit(EXIT_FAILURE) works in all the functions

user2736738
  • 30,591
  • 5
  • 42
  • 56
2

argc means the number of argument that are passed to the program. not printed which value you have passed.

Use argv[1] to print value of 5.

#include <stdio.h>

int main(int argc, char **argv)
{
    if (argc >= 2)
    printf("%d\n", atoi(argv[1]));
    return(0);
}
msc
  • 33,420
  • 29
  • 119
  • 214
2

argc (argument count) and argv (argument vector) are how command line arguments are passed to main() in C and C++.

argc is number of arguments passed to program (number of strings pointed to by argv), which is correct in your case. First string is program name, second string is "5" (so you have to convert "5" to int).

This might be a useful reading for you.


If you want to convert second argument to int try this

if (argc < 2)
{
    return -1;
}

int item = atoi(argv[1]);

atoi may lead to undefined behavior so you should use strtol instead.

Or you may loop through all arguments

for(int i = 0; i < argc; i++)
{
    // argv[i] is the argument at index i
    // You may print them
    printf("%s", argv[i]);
    // Convert them, ...
}
kocica
  • 6,412
  • 2
  • 14
  • 35
0

argc is the count of arguments, which is 2 here. If you intend to print 5, you need to print argv[1] (or if you always want the last argument, argv[argc-1]) instead.

Roney Michael
  • 3,964
  • 5
  • 30
  • 45