First acquaintance with programming | On how to elegantly learn the command line parameters of the C language

 

When executing the program, you can pass values ​​to the C program from the command line. These values ​​are called command-line parameters , and they are important to the program, especially when you want to control the program from the outside instead of hard-coding these values ​​in the code.

Command line parameters are processed using main() function parameters, where argc  refers to the number of parameters passed in, and argv[]  is an array of pointers that point to each parameter passed to the program.

The following is a simple example, check whether the command line provides parameters, and perform corresponding actions according to the parameters:

#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("Too many arguments supplied./n");    }    else    {      printf("One argument expected./n");    } }

Compile and execute the above code with one parameter, it will produce the following results:

$./a.out testing The argument supplied is testing

Compile and execute the above code with two parameters, it will produce the following results:

$./a.out testing1 testing2 Too many arguments supplied.

Compile and execute the above code without passing any parameters, it will produce the following results:

$./a.out One argument expected

It should be noted that argv[0]  stores the name of the program, argv[1]  is a pointer to the first command line parameter, and *argv[n] is the last parameter.

If no parameter is provided, argc will be 1, otherwise, if an argument is passed, argc  will be set to 2.

Multiple command line parameters are separated by spaces, but if the parameters themselves contain spaces, the parameters should be placed in double quotation marks "" or single quotation marks" when passing parameters.

Let us rewrite the above example, there is a space, then you can pass this point of view, put them in double quotes or single quotes """".

Let us rewrite the above example and pass a command line parameter placed inside double quotes to the program:

#include <stdio.h>  int main( int argc, char *argv[] )  {    printf("Program name %s/n", argv[0]);      if( argc == 2 )   

 {      printf("The argument supplied is %s/n", argv[1]);    }    else if( argc > 2 )    {      printf("Too many arguments supplied./n");    }   

 else    {      printf("One argument expected./n");    } }

Using a simple parameter separated by spaces, enclosed in double quotes, compiling and executing the above code, it will produce the following results:

$./a.out "testing1 testing2"  Progranm name ./a.out The argument supplied is testing1 testing2


In addition, if you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster! I may be able to help you here~

UP has uploaded some video tutorials on learning C/C++ programming on the homepage. Those who are interested or are learning must go and take a look! It will be helpful to you~

Sharing (source code, actual project video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning:

Programming learning:

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/115265022
Recommended