0

int argcin C is an integer variable that holds the number of arguments that are passed to the program through the command line . Since we don't pass any integer to the command line while executing the program , then which component is responsible for passing the number of arguments to argc variable ?

AAA
  • 305
  • 1
  • 7
  • 1
    On Windows, it would be the code inside the executable that calls `main`. – ikegami Mar 07 '22 at 23:33
  • Note that `argc` is mainly a convenience. `argv` always ends with a `NULL` value, so you could count it yourself. And that's what the `exec*` functions do. – Barmar Mar 08 '22 at 00:03
  • 2
    `main()` is not the start of the program. The name of the actual starting function depends on the OS (for Linux it defaults to `_start()`. So when the program is run the actual starting function retrieves the command line, hooks up stdin and stdout and stderr, does some other stuff and then calls `main()`. That's why `main()` must take those types and number of parameters. Related: https://stackoverflow.com/questions/29694564/what-is-the-use-of-start-in-c – Jerry Jeremiah Mar 08 '22 at 00:17
  • 1
    @ikegami: *all* of the exec* functions accept a NULL terminated vector (array) of paramters. The kernel counts how many non-null elements there are and passes that as argc – Chris Dodd Mar 08 '22 at 03:27
  • @ikegami: because what you said implies the reverse. – Chris Dodd Mar 08 '22 at 05:09
  • @Chris Dodd, huh? No, not at all. I said the count is NOT provided to `exec*`. From that, one must conclude the count is calculated further down the path such as in the kernel. I think you misread what I wrote. – ikegami Mar 08 '22 at 06:53

1 Answers1

2

The "command line" is input to a shell program. It is that program that parses the line and forms argc and the NULL terminated array for argv and then calls the C program.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    This is true of unix, but not of [Windows](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa) – ikegami Mar 08 '22 at 04:27
  • On Unix-like systems, the shell doesn't directly call the C program, although it does parse the arguments. It calls [`execve`](https://man7.org/linux/man-pages/man2/execve.2.html), and the arguments it passes to `execve` do not include `argc` (although obviously it can be computed by counting the length of the `argv` array, which is an argument to `execve`). `argc` is computed during the startup of the newly created process. – rici Mar 08 '22 at 04:48
  • @rici Re "*argc is computed during the startup of the newly created process.*" By the kernel or by the process itself? This is not clear from your comment. /// Re "*the shell doesn't directly call the C program*", I'm not sure what you mean by this exactly. – ikegami Mar 08 '22 at 07:00
  • @ikegami: for your first question, it's an implementation detail. My ambiguity was deliberate. But it's definitely not the shell. I don't see how "the shell doesn't directly call the C program" could be misinterpreted, so you'll have to offer your parse. The shell calls `execve` (or something which calls `execve`). Any other calls happen in the new process. – rici Mar 08 '22 at 07:20
  • @rici, It's not my question, it's the OP's – ikegami Mar 08 '22 at 13:59
  • @ikegami: so you're trying to tell me that my comment to this answer is not an attempt to answer the original question? That's true. It was an attempt to suggest that this answer is badly phrased, which is why it appears here in a comment. – rici Mar 08 '22 at 14:14
  • Re "*so you're trying to tell me that my comment to this answer is not an attempt to answer the original question?*", No, I'm saying it's a failed attempt – ikegami Mar 08 '22 at 14:16