Advertisement
dmilicev

command line arguments v2.c

Oct 19th, 2019
521
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     command line arguments v2.c
  3.  
  4.  
  5.     Janaidah Dora's tricky question:
  6.  
  7.     https://www.facebook.com/JanaidahAndor
  8.  
  9.     How do you make a program by using do-while or if-else
  10.     by inputing 10 integers and by just declaring one variable.
  11.  
  12.     Loops must have their own counter and this is the only one
  13.     variable that must be declared.
  14.  
  15.     Jark Clim,
  16.     https://www.facebook.com/metallicsoul92
  17.  
  18.     gave me an idea for solving this tricky task.
  19.     The idea is to use the command line arguments,
  20.     which we write when invoking a program,
  21.     to accommodate our 10 variables.
  22.  
  23.   Command line arguments: main(int argc, char *argv[])
  24.  
  25.   Do not forget first to compile this program
  26.   and then run exe file from command line,
  27.   exactly with quotes, like this:
  28.  
  29.   "command line arguments v2.exe" 1 2 3 4 5 6 7 8 9 10
  30.  
  31.   Exe file name is in quotes because
  32.   name has several words separated by space
  33.  
  34.   We have to write arguments  1 2 3 4 5 6 7 8 9 10
  35.   to reserve memory space.
  36.  
  37.   Maybe someone else will have a better solution...
  38.  
  39. */
  40.  
  41.  
  42. #include <stdio.h>
  43.  
  44. int main(int argc, char *argv[])
  45. {
  46.     int i;          // this is the only one declared variable
  47.  
  48.  
  49.     if(argc==1){    // if there is no command line arguments
  50.  
  51.         printf( "\n Do not forget first to compile this program \n"
  52.                 "\n and then run exe file from command line \n"
  53.                 "\n exactly with quotes, like this: \n\n"
  54.  
  55.                 "\n \"command line arguments v2.exe\" 1 2 3 4 5 6 7 8 9 10 \n\n"
  56.  
  57.                 "\n Exe file name is in quotes because \n"
  58.                 "\n name has several words separated by space. \n\n"
  59.                 "\n We have to write arguments  1 2 3 4 5 6 7 8 9 10 \n"
  60.                 "\n to reserve memory space. \n\n"
  61.               );
  62.  
  63.         printf("\n\n Press any key to exit. \n");
  64.         getch();    // to pause screen
  65.         return 1;   // exit because there is no command line arguments
  66.     }
  67.  
  68.                     // if there are command line arguments:
  69.     printf("\n Number of command line arguments passed, argc = %2d \n", argc);
  70.  
  71.     for ( i = 0 ; i < argc ; i++)
  72.       printf("\n %2d. command line argument passed is %2s \n", i, argv[i]);
  73.  
  74.     for ( i = 1 ; i < argc ; i++)
  75.     {
  76.       printf("\n Enter %2d. integer ", i);
  77.       scanf("%s",argv[i]);
  78.     }
  79.  
  80.     printf("\n\n\t Entered %2d integers are: \n", argc-1);
  81.  
  82.     // argv[i] are strings ( char *argv[] ) and we have to convert them to integers
  83.     for ( i = 1 ; i < argc; i++)
  84.     {
  85.         printf("\n\n %2d. integer = %2d", i, atoi(argv[i]) );
  86.  
  87.     }
  88.  
  89.  
  90.     printf("\n\n Press any key to exit. \n");
  91.     getch();    // to pause screen
  92.  
  93.     return 0;
  94. }
Advertisement
RAW Paste Data Copied
Advertisement