Strange scope variable

I was looking for the getopt command, and I found that using the function seems to inexplicably optarg another variable called optarg . You can see an example of this in the following program that I selected 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); } 

Note that optarg now used, it would seem, without declaration or initialization. Maybe this is just a common trait in C that I don’t know about, but I’ve searched the Internet for several hours and I don’t know the name of what I am looking for. Any explanation would be nice.

+4
source share
5 answers

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

+2
source

The term "global variable". If you declare a variable outside the function, it is available inside the functions:

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

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

 extern char *optarg; 

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

+3
source

In the future, if you find names but don’t know where they are defined or declared, just start the C preprocessor, which is responsible for #include , and then search for the term with grep or just more .

for instance

 gcc -E foo.c >foo.i 

will put the result of the C preprocessor in foo.i

Then you can see the file using more (using / to search)

The file will contain links to the include file, which includes a definition or declaration.

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

+1
source

optarg declared in <unistd.h> .

0
source

- Variable: char * optarg

This variable is set by getopt to indicate the value of the option argument for those parameters that take arguments.

I find it on Using Getopt websit

0
source

Source: https://habr.com/ru/post/1401929/


All Articles