linux - Pass arguments into C program from command line -


So I'm in Linux and when you execute it from the command line, I want to accept a program.

For example,

./ myprogram 42 -b -s

Then program 42 number as int Will archive and based on the execution of certain parts of the code, what is the logic, such as -b or -s

You can use

  #include & lt; Ctype.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Unistd.h & gt; Int main (int argc, char ** argv) {int bflag = 0; Int sflag = 0; Int index Int c; Opter = 0; While ((c = getopt (argc, argv, "bs")) -1) switch (c) {case 'b': bflag = 1; break; The case is: sflag = 1; break; Case '?': If (ISOPRT) fprintf (stderr, "unknown option` -% c '. \ N", optopt); Else fprintf (stderr, "unknown option character '\\ x% x'. \ N", optopt); Return 1; Default: aborted (); } Printf ("bflag =% d, sflag =% d \ n", bflag, sflag); (Index = optin; index and lieutenant; organi; index ++) for printf ("non-option argument% s \ n", argv [index]); Return 0; }  

Comments