C – Command Line Arguments

Accepting Command Line arguments:

  • Every C application runs by Computer Operating System.
  • OS runs the program from its environment.
  • Every OS consists CUI and GUI mode.
  • To read command line arguments, we need to run the C program from CUI OS mode.

Run application from Command prompt:

  • Cmd Prompt is Ms-DOS
  • It is Command User Interface.
  • OS takes command as input and open specified application which is present in that machine.
  • We compile and run the code from command line using tcc compiler as follows

Code program:

#include<stdio.h>
int main()
{
            printf(“Hello…\n”);
            return 0;
}

Note: save into “turboc” folder with name Program.c

Open command prompt:

Compilation:
            C:/turboc> tcc Program.c
                        -> Generate Program.exe file
 
Execution:
            C:/turboc>Program.exe
                        -> Generate output.

main() prototype: Regular main() function prototype is slightly different from the main() function taking command line arguments. We must define main() function as follows

int  main(int argc ,  char*  argv[ ])

where:

  • ‘argc’ = argument count ( Number of arguments passed including file name)
  • ‘argv’ = argument vector(list)  (List of arguments  – default type is String)

Why argument type is String by default?

  • In any programming language, only String type can hold any data.
    • main(int[]) –> can take only integers
    • main(float[]) –> can take only float values.
    • main(char* []) –> can hold any type of data.
      • char* a = “10”;
      • char* b = “23.45”;
      • char* c = “g”;
      • char* d = “CBook”;

Scroll to Top