Irresponsible Newb
Irresponsible Newb

Reputation: 613

Strange out of scope variable

I was looking up the getopt command and I discovered that using the function seems to inexplicably produce another variable called optarg. You can see an example of this in the following program I swiped from Wikipedia:

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <unistd.h>    /* for getopt */
int main (int argc, char **argv) {
    int c;
    int digit_optind = 0;
    int aopt = 0, bopt = 0;
    char *copt = 0, *dopt = 0;
    while ( (c = getopt(argc, argv, "abc:d:012")) != -1) {
        int this_option_optind = optind ? optind : 1;
        switch (c) {
        case '0':
        case '1':
        case '2':
            if (digit_optind != 0 && digit_optind != this_option_optind)
              printf ("digits occur in two different argv-elements.\n");
            digit_optind = this_option_optind;
            printf ("option %c\n", c);
            break;
        case 'a':
            printf ("option a\n");
            aopt = 1;
            break;
        case 'b':
            printf ("option b\n");
            bopt = 1;
            break;
        case 'c':
            printf ("option c with value '%s'\n", optarg);
            copt = optarg;
            break;
        case 'd':
            printf ("option d with value '%s'\n", optarg);
            dopt = optarg;
            break;
        case '?':
            break;
        default:
            printf ("?? getopt returned character code 0%o ??\n", c);
        }
    }
    if (optind < argc) {
        printf ("non-option ARGV-elements: ");
        while (optind < argc)
            printf ("%s ", argv[optind++]);
        printf ("\n");
    }
    exit (0);
}

Notice that optarg is now being used seemingly without be declared or initialized. Maybe this is just a common feature in C that I am unaware of, but I have been googling for a few hours and I don't know the name of what I am looking for. Any explanations would be nice.

Upvotes: 5

Views: 695

Answers (5)

Eric
Eric

Reputation: 1

— Variable: char * optarg

This variable is set by getopt to point at the value of the option argument, for those options that accept arguments.

I find this at the Using Getopt websit

Upvotes: 0

K-ballo
K-ballo

Reputation: 81349

optarg is declared in <unistd.h>.

Upvotes: 0

From the man page

GETOPT(3)                BSD Library Functions Manual                GETOPT(3)

NAME
     getopt -- get option character from command line argument list

LIBRARY  
     Standard C Library (libc, -lc)  

SYNOPSIS  
     #include <unistd.h>  

     extern char *optarg;  
     extern int optind;  
     extern int optopt;  
     extern int opterr;  
     extern int optreset;  

     int  
     getopt(int argc, char * const argv[], const char *optstring);

These variables are declared in the unistd.h header file.

Upvotes: 2

gbulmer
gbulmer

Reputation: 4290

For future reference, if you find names but don't know where they are defined or declared, only run the C pre-processor, which is responsible for #include, and then search for the term using grep or just more.

For example

gcc -E foo.c >foo.i

would put the result of the C pre-processor into foo.i

You could then have a look at the file using more (using / to search)

The file will have references to the include file which includes the definition or declartion.

For example,
more foo.i
then
/optarg
shows the line
extern char *optarg;
by scrolling upwards (or reverse search ?# ) I could find
# 414 "/usr/include/unistd.h" 3 4

Upvotes: 1

ruakh
ruakh

Reputation: 183240

The term is "global variable". If you declare a variable outside of a function, it's accessible inside functions:

int i = 7;

int main()
{
    printf("%d\n", i); // prints 7
    return 0;
}

In the case of optarg, the unistd.h header declares it as a global char * variable with external linkage:

extern char *optarg;

(see http://pubs.opengroup.org/onlinepubs/000095399/functions/getopt.html).

Upvotes: 4

Related Questions