× Home
Next Lec → ← Previous Lec

Command Line Arguments

What are command line arguments?

Command Line Example

  • FFmpeg is a free and open-source project consisting of a vast software suite of libraries and programs for handling video, audio, and other multimedia files and streams.
  • Ffmpeg.exe is a command line utility written in C language.
  • Other examples include git, brew, apt-get etc.

Sample program

                        

                        #include <stdio.h>
                        int main(int argc, char* argv[]) //☆☆☆
                        {
                            printf("argc is %d\n", argc);
                            if(argc >= 2) // if nothing is passed then the value of argc becomes 1.
                            {
                                printf("The arguments supplied are : ");
                                for(int i=0; i<argc; i++)
                                {
                                    printf("%s\t", argv[]);
                                }
                            }
                            return 0;
                        }
                    
                    

when prog.exe file will be created then we pass the arguments like
prog.exe harry code in teminal
Now the value in argc is 3, how? → we have wrote 3 words 'filename.exe'(with index 0) , 'harry'(with index 1) and 'code'(with index 2) that's why the value in argc becomes 3.

More Example

                            
                            #include <stdio.h>
                            
                             int main(int argc, char const *argv[])
                             {
                                printf("The value of argc is %d\n",argc);
                                return 0;
                             }
                            
                        

inside terminal I wrote
.\code.exe hi hellow how do you do
output was → The value of argc is 7

                            
                            #include <stdio.h>
                            
                             int main(int argc, char const *argv[])
                             {
                                printf("The value of argc is %d\n",argc);
                                for (int i = 0; i < argc; i++)
                                {
                                    printf("This argument at index number %d has value of %s \n", i, 
argv[i]); } return 0; }
                        
                                
                            Terminal command → .\code.exe hi hellow how do you do
                            output:
                             This argument at index number 0 has value of C:\Users\A--e3\Main\BCA\C_programming\l68\program\code.exe
                             This argument at index number 1 has value of hi
                             This argument at index number 2 has value of hellow
                             This argument at index number 3 has value of how
                             This argument at index number 4 has value of do
                             This argument at index number 5 has value of you
                             This argument at index number 6 has value of do
                        
                    

Usage → this could be used with File I/O functions and we can take and store value of arguments that are given in terminal and store that inside a file.