-1

I'm having some trouble reading a line of the code, and understanding what constitutes an argument in the context of this line of code. This is saved in a file called argv0.c

#include <cs50.h>
#include <stdio.h>

int main(int argc, string argv[])
{
    if (argc == 2)
    {
        printf("hello, %s\n", argv[1]);
    }
    else
    {
        printf("hello, world\n");
    }
}

I compile the code as follows:

make argv0
./argv0

following which I am prompted for an input. Herein lies the issue:

  1. if I type in "Dion Lim" in the terminal, is Dion Lim considered an argument? If so, is it two arguments?

  2. Why is it that if I type in "Dion Lim" in the terminal, I get "Hello, World", but if I type in "Dion" i get "Hello,Dion"

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Lim Dion
  • 5
  • 1

4 Answers4

1

Q1) Yes, they are two arguments.

Q2) Because argc consider the name of executable it's the first parameter. So:

./argv0 Dion Lim // argc == 3
./argv0 Diom     // argc == 2
./argv0          // argc == 1

You can get more detail here.

dceccon
  • 423
  • 1
  • 7
  • 25
0

if I type in "Dion Lim" in the terminal, is Dion Lim considered an argument? If so, is it two arguments?

It depends on how your shell handles it of course, but usually "Dion Lim" would be one argument while Dion Lim (without the quotation marks) would be two arguments. The space delimits the arguments, and with the quotes you can work around that if you want space in your input (sometimes you can also escape the space, like Dion\ Lim).

Why is it that if I type in "Dion Lim" in the terminal, I get "Hello, World", but if I type in "Dion" i get "Hello,Dion"

The argc parameter tells you how many arguments you have (I think of it as standing for "argument count"). The program's name also counts as an argument, so if you only pass Dion, then argc will be 2 already. If you pass Dion Lim, it will be 3.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Hey Blaze, if I could aska follow up question. What is the purpose of prefacing an int before arg c ( i.e " int arg c")? – Lim Dion Jul 19 '19 at 08:40
  • @LimDion you should really run through a basic tutorial for that kind of question; `int` is the *type* of the argument named `argc` to the function `main`. – jonrsharpe Jul 19 '19 at 08:45
0

To see the number of arguments check the value argc (arguments count). There is always at least one input argument, which is the program name.

So with./argv0 Dion Lim there are three input arguments.

If you are wondering make compiles the program using Makefile so if look in the directory you from which you are running make you will find a file named Makefile containing the compilation instructions.

roschach
  • 8,390
  • 14
  • 74
  • 124
0

According to the C Standard (5.1.2.2.1 Program startup)

— 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.

So if you "manually" supply the argument Dion then argc will be exactly equal to 2. The first program parameter will be the program name (as it is followed from the quote) and the second program parameter will be the word Dion.

If you will type Dion Lim then the host system considers them as two program parameters and together with the program name argc will be equal to 3.

However if you enclose the input Dion Lim in parentheses like "Dion Lim" then the system will consider the input as one parameter and your program will output

hello Dion Lim
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335