When I was making a video of tclock.c of ncurses-examples, I noticed this part:

int
main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
{
    /* ... */
}

I could already guess that GCC_UNUSED is a GCC variable __attribute__, although I have never used that extension before:

unused
This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC does not produce a warning for this variable.

If you have unused variables with related warning flags on, -Wunused-parameter and -Wunused-variable, or simply -Wall -Wextra, GCC would show warnings like:

GCC_UNUSED.c: In function 'main':
GCC_UNUSED.c:4:7: warning: unused variable 'shout_about_me' [-Wunused-variable]
GCC_UNUSED.c:2:10: warning: unused parameter 'argc' [-Wunused-parameter]
GCC_UNUSED.c:2:22: warning: unused parameter 'argv' [-Wunused-parameter]

For a code as shown below:

int
main(int argc, char *argv[])
{
  int shout_about_me;

  return 0;
}

To disable those warning when using GCC, GCC_UNUSED seems commonly defined for being used like the following:

#define GCC_UNUSED __attribute__((unused))

int
main(int argc GCC_UNUSED, char *argv[] GCC_UNUSED)
{
  int shout_about_me GCC_UNUSED;

  return 0;
}

Just like the one I’ve seen in tclock.c. GCC_UNUSED would specifying that the variable and parameters are unused, GCC wouldn’t tell you anything if those are not used.

The identifier is used very common, ncurses, lynx, or xterm, all have it defined in the same name. As far a I could tell, it’s not defined by any standard headers, you would need to define it in your codes.

Not all compilers used are GCC, so I have even seen more often another approach is taken, which would check __GNUC__, if undefined, it then defines as a comment to avoid errors in other compilers, more or less like the following code:

#ifdef __GNUC__
// curses.h or other sources may already define
#undef  GCC_UNUSED
#define GCC_UNUSED __attribute__((unused))
#else
#define GCC_UNUSED /* nothing */
#endif

I used the code above because I don’t want the warnings, for instances ncurses would define it be /* nothing */ if not already defined, so a redefinition is needed to ensure it goes my way; or you can define before including curses.h.

GCC used variable attribute seems good to utilize, but what if I want warnings for using unused variables?

int using_unused GCC_UNUSED;

using_unused = 1;
printf("%d\n", using_unused);

I’d like GCC to spit a warning, when I use an unused variable, so I could take GCC_UNUSED off the variable.