Browse by Domains

Introduction to Command Line Arguments in C?

  1. Command Line Arguments in C
  2. Features of Command-Line Arguments
  3. Output in various programs
  4. The main() function
  5. Standard command-line arguments
  6. The envp command-line arguments
  7. Parsing C command-line arguments
  8. Example of Command-Line Arguments in C
  9. Working of Command-Line Arguments in C 
  10. Benefits of Command-Line Arguments in C

Command Line Arguments in C

Command-line arguments are arguments that are indicated after the name of the program in the framework’s order line, and these arguments are given to your program during program execution. The arguments passed from the command line are called order line arguments. These arguments are taken care of by the main() function. To help order line contention, you need to change the design of the main() function. The syntax is as follows:

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

Here, argc is mainly the number of arguments. It considers the record name the primary arguments. The argv[] contains the complete number of arguments. The primary argument is the document name consistently. The Parts of Command-Line Arguments needs the main two parameters that are passed into the main function: 

  • Number of Command line arguments 
  • The rundown of Command line arguments

Features of Command-Line Arguments

  • They are passed to fundamental() work. 
  • They are boundaries/contentions provided to the program when it is summoned. 
  • They are utilized to control programs from outside rather than hard-coding those qualities inside the code. 
  • argv[argc] is a NULL pointer. 
  • argv[0] contains the title of the code. 
  • argv[1] focuses on the primary order line contention and argv[n] focuses on the last contention.

Output in various programs

  • Without argument: When the code is accumulated and executed without passing any argument, it produces the following yield.

$ ./a.out

Program Name Is: ./a.out

  • Three Arguments: When the above code is incorporated and executed with three Arguments, it creates the accompanying yield.

$ ./a.out First Second Third 

Program Name Is: ./a.out 

Number Of Arguments Passed: 4 

argv[0]: ./a.out 

argv[1]: First 

argv[2]: Second 

argv[3]: Third

  • Single Argument: When the above code is arranged and executed with a solitary Argument isolated by space however inside twofold statements, it creates the accompanying yield.

$ ./a.out “First Second Third” 

Program Name Is: ./a.out 

Number Of Arguments Passed: 2 

argv[0]: ./a.out 

argv[1]: First Second Third

  • The single argument in cites isolated by space: When the above code is arranged and executed with a solitary contention isolated by space, however inside single statements, it creates the accompanying yield.

$ ./a.out ‘First Second Third’ 

Program Name Is: ./a.out 

Number Of Arguments Passed: 2 

argv[0]: ./a.out 

argv[1]: First Second Third

The main() function

The principal work doesn’t have a revelation since it’s incorporated into the language. If it did, the announcement punctuation for principle would resemble this: 

int main(); 

int main(int argc, singe *argv[]); 

If no return esteem is determined in principle, the compiler supplies a return worth of nothing. 

Standard command-line arguments

The arguments for principle permit helpful order line parsing of arguments. The sorts for argc and argv are characterized by the language. The names argc and argv are conventional, yet you can name them whatever you like. The arguments definitions are as per the following: 

argc 

A whole number that contains the include of arguments that continue in argv. The argc boundary is consistently more noteworthy than or equivalent to 1. 

argv 

A variety of invalid-ended strings addressing order line arguments entered by the client of the program. By show, argv[0] is the order with which the program is conjured. argv[1] is the principal command-line argument. The last argument from the order line is argv[argc – 1], and argv[argc] is consistently NULL. 

The envp command-line arguments 

The primary or wmain marks permit a discretionary Microsoft-explicit augmentation for admittance to climate factors. This expansion is likewise normal in different compilers for Windows and UNIX frameworks. The name envp is conventional. However, you can name the climate boundary whatever you like. Here are the viable presentations for the contention records that incorporate the environment parameter: 

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

int wmain(int argc, wchar_t* argv[], wchar_t* envp[]); 

envp 

The discretionary envp boundary is a variety of strings addressing the factors set in the client’s current circumstance. This cluster is ended by a NULL passage. It very well may be announced as a variety of pointers to burn (scorch *envp[]) or as a pointer to pointers to singe (roast **envp). On the off chance that your program utilizes wmain rather than primary, utilize the wchar_t information type rather than burn. 

The environment block passed to fundamental and wmain is a “frozen” duplicate of the current environment. On the off chance that you later change the environment by settling on a decision to putenv or _wputenv, the current environment (as returned by getenv or _wgetenv and the _environ or _wenviron variable) will change, yet the square highlighted by envp will not change. For more data on the best way to stifle environment handling. The envp argument is viable with the C standard, yet not with C guidelines. 

Parsing C command-line arguments 

The order line parsing rules utilized by Microsoft C/C++ code are Microsoft-explicit. The runtime startup code utilizes these principles when deciphering arguments given on the working framework order line: 

Arguments are delimited by a blank area, which is either a space or a tab. 

The principal argument (argv[0]) is dealt with extraordinarily. It addresses the program name. Since it should be a legitimate pathname, parts encompassed by twofold statement marks (“) are permitted. The twofold statement marks are excluded from the argv[0] yield. The parts encompassed by double statements mark forestall understanding of a space or tab character as the finish of the argument. The later principles in this rundown don’t have any significant bearing. 

A string encompassed by twofold statement marks is deciphered as a solitary argument, which might contain blank area characters. A cited string can be installed in arguments. The caret (^) isn’t perceived as a break character or delimiter. Inside a cited string, a couple of twofold statement marks are deciphered as a solitary got away from the double statement mark. On the off chance that the order line closes before an end twofold statement mark is discovered, then, at that point, every one of the characters read so far is yielded as the last argument. d

A double statement mark preceded by a backslash’s punctuation line (\”) is deciphered as a strict double statement mark (“). Backslashes punctuation lines are deciphered in a real sense, except if they promptly go before a double statement mark. If an even number of backslashes punctuation lines is trailed by a double statement mark, then, at that point, one backslash punctuation line (\) is set in the argv cluster for each pair of backslashes punctuation lines (\\), and the double statement mark (“) is deciphered as a string delimiter. 

If an odd number of backslashes punctuation lines is trailed by a double statement mark, then, at that point, one backslash punctuation line (\) is put in the argv cluster for each pair of backslashes punctuation lines (\\). The double statement mark is deciphered as a getaway succession by the leftover backslashes punctuation line, causing a strict double statement mark (“) to be put in argv.

Example of Command-Line Arguments in C

Example 1

#include <stdio.h>
int main( int argc, char *argv[] ) 
 {
   if( argc == 2 ) 
{
      printf("The argument supplied is %s\n", argv[1]);
   }
   else if( argc < 2 )
 {
      printf("One argument expected.\n");
   }
   else {
      printf("Too many arguments supplied.\n");
   }
}

Example 2

#include <stdio.h>
int main(int argc, char *argv[])
{
  int i;

  for (i = 0; i < argc; i++) 
printf("%s ", argv[i]);
  printf("\n");

  return 0;
}

Example 3

#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
//an integer variable is defined
int a;
//if the condition is applied to check if the count of arguments passed to the program is greater than or equal to two and if the condition is true, the command line arguments has been passed into the program is printed otherwise no argument is passed to the program is printed
if( argc >= 2 )
{
printf("The arguments passed to the code are:\n");
for(a = 1; a < argc; a++)
{
printf("The argument passed to the code is: %s\t", argv[a]);
}
}
else
{
printf("No argument is passed to the program\n");
}
return 0;
}

Working of Command-Line Arguments in C 

  • At whatever point there is a need to pass the qualities to the program from outside and don’t have any desire to utilize it inside the code, we utilize Command-Line Arguments in C. 
  • The qualities passed to the C program from the order line when the program is executed are called order line contentions in C. 
  • The main() work in the program handles the order line contentions passed to the program when the program is executed. 
  • The number of arguments passed to the program is addressed by argc and each contention passed to the program is pointed by a pointer which is kept up within a pointer exhibit addressed by argv[]. 

Benefits of Command-Line Arguments in C 

There are a few benefits referenced as mentioned below : 

  • At whatever point there is a need to pass the qualities to the program from outside and don’t have any desire to utilize it inside the code, Command Line Arguments can be utilized in C. 
  • The program to be executed can be controlled from an external perspective rather than hard-coding the qualities inside the program by utilizing Command-Line Arguments.

This brings us to the end of the blog on command-line arguments in C. We hope that you have a better understanding of the concepts.

Avatar photo
Great Learning Team
Great Learning's Blog covers the latest developments and innovations in technology that can be leveraged to build rewarding careers. You'll find career guides, tech tutorials and industry news to keep yourself updated with the fast-changing world of tech and business.

Leave a Comment

Your email address will not be published. Required fields are marked *

Great Learning Free Online Courses
Scroll to Top