7

Using gcc 4.8.4 -Wall -Wextra -Wpedantic. I want to use argv for the executable name and have no need for argc.

If I do

int main(int argc, char *argv[])

I get this warning:

abc.c:5:14: warning: unused parameter ‘argc’ [-Wunused-parameter]

In the past I have just done

int main(int, char *argv[])

to get rid of the warning, but that may have been C++. Now I get an error:

abc.c:5:1: error: parameter name omitted

Is there a way to access argv and not get a warning for not accessing argc (with gcc warnings turned on)?

Scooter
  • 5,908
  • 8
  • 32
  • 59
  • With `(void)argc;` as a statement in `main` (not applied to the definition itself). – Weather Vane Nov 24 '16 at 00:16
  • @WeatherVane int main ( (void) argc, char *argv[]) is giving a compile error – Scooter Nov 24 '16 at 00:18
  • I updated the comment. – Weather Vane Nov 24 '16 at 00:18
  • To be precise, there's no "unused parameter *error*" in C. – AnT Nov 24 '16 at 00:20
  • @WeatherVane OK, that works. Can you put it in an answer so I can select it? – Scooter Nov 24 '16 at 00:20
  • @AnT I'm getting one. I am using -WExtra -WAll -Wpedantic . – Scooter Nov 24 '16 at 00:21
  • It isn't an error, it is a warning, and you deliberately turned the warning on, so presumably you wanted to be told about it, so presumably you already had an action in mind, such as, err, using the parameter, or deleting it. Hard to see what you're really asking here. – user207421 Nov 24 '16 at 00:29
  • @EJP If it is a warning, why does the word "error" occur in it and the compilation fail? I am referring to what you get with "int main(int, char *argv[]) – Scooter Nov 24 '16 at 00:33
  • @WeatherVane I had the title wrong - I said error which I should have said warning. I get the error on "int main(int,, const argv *[]) – Scooter Nov 24 '16 at 00:37
  • 2
    `abc.c:5:1: error: parameter name omitted`. Translation: "I understand your syntax perfectly well, but you can't have it! C++ is down the hall to your left." – Kaz Nov 24 '16 at 00:39
  • @AnT Oops, you are right. I had the quesiton title wrong. I was getting a warning and my C++-style fix gives the error. – Scooter Nov 24 '16 at 00:40

1 Answers1

12

My comment was unclear:

You can write (void)argc inside main() in order to get rid of the compiler message without doing any harm to your program

int main(int argc, char *argv[])
{
    char *prog = argv[0];
    (void)argc;
    return 0;
}
Santiago
  • 95
  • 6
Weather Vane
  • 31,226
  • 6
  • 28
  • 47
  • Interesting. Does it simply perform a cast and does not store it anywhere, or is there more to it ? Some clarification might be worth it. – saeleko Nov 24 '16 at 00:27
  • @LudaOtaku I always used to just write `argc;` without the `(void)` but never needed for a function argument, only a local variable, until someone advised that the correct way is as I answered. Sorry, no references. – Weather Vane Nov 24 '16 at 00:30
  • That's fine I think it's clear enough, it's simply an unused statement, not a special syntax to tell the compiler to ignore this variable to some degree. I was confused because I usually just comment out variables I don't need yet and purposely leave the warnings for function arguments so I know I haven't finished writing the function. However I still believe it should not be done for main as it might lead to a segfault in case there are not enough command line parameters being passed in in argv. It's fine for debug, not release. – saeleko Nov 24 '16 at 00:37
  • @LudaOtaku it is a refence to `argc` that does nothing. A statement such as `argc + 1;` might generate a warning because the result of that expression is not assigned. I suupose the `(void)` is intended to silence that "ineffective" warning too. – Weather Vane Nov 24 '16 at 00:40
  • Yes I thought about doing that and realized it would generate a warning yes. It makes sense to do it that way. – saeleko Nov 24 '16 at 00:42
  • You can also do things like `char *prog = argv[argc-argc];` or `assert(argc > 0);`, the latter of which is fatuous but sufficient when compiling with assertions enabled. If you compile without assertions enabled (`-DNDEBUG`), then the argument is still unused. – Jonathan Leffler Nov 24 '16 at 02:16
  • For what it's worth, I recently figured out a single line variant that works and shuts up GCC: `(void)argv[argc-1];`. It's smaller and means there's no unused variables. – Finn O'leary Jun 27 '19 at 16:45