Command line parameter in C

This chapter only really makes sense for you if you are using a command promt to compile the program. It is possible to pass values ​​from the command line - command line to program C when it is executed. These values ​​are called Command line arguments and are sometimes important for your program when you control your program outside instead of raw coding values ​​inside the code.

The command line parameters are handled using the main () function parameters, with argc pointing to the number of parameters you pass and argv [] as the pointer array to any parameter provided for that program . Here is an example to check if there are any parameters provided from the command line and perform the corresponding actions:

 #include int main ( int argc , char * argv [] ) { if ( argc == 2 ) { printf ( "Tham so duoc cung cap la: %sn" , argv [ 1 ]); } else if ( argc > 2 ) { printf ( "Qua nhieu tham so duoc cung cap.n" ); } else { printf ( "Ban nen cung cap mot tham so.n" ); } } 

When the above code is compiled and executed with a parameter, it will print the following result:

 $ ./ a . out thamso1 Tham so duoc cung cap la : thamso1 

When you pass two parameters to the code above, it will print the following result:

 $ ./ a . out thamso1 thamso2 Qua nhieu tham so duoc cung cap . 

When the above code is executed and executed with no parameters passed, it will print the result below:

 $ ./ a . out Ban nen cung cap mot tham so . 

Note that argv [0] holds the name of the program itself and argv [1] is a pointer to the first command line parameter provided, argv [n] is the last parameter. If no parameters are provided, argc will be 1, if you pass a parameter then argc will have a value of 2.

You pass all command line parameters separately by a space, but if the parameters itself have a space, you can pass these parameters by placing them in double quotation marks ("") or quoting single (''). Now we rewrite the above program when you print out the program name and pass the command line parameters inside the double quotation mark ("").

 #include int main ( int argc , char * argv [] ) { printf ( "Ten chuong trinh la: %sn" , argv [ 0 ]); if ( argc == 2 ) { printf ( "Tham so duoc cung cap la: %sn" , argv [ 1 ]); } else if ( argc > 2 ) { printf ( "Qua nhieu tham so duoc cung cap.n" ); } else { printf ( "Ban nen cung cap mot tham so.n" ); } } 

When the above code is compiled and executed with a single parameter by a space inside the double quotation mark, the following result is printed:

 $ ./ a . out "thamso1 thamso2" Ten chuong trinh la : ./ a . out Tham so duoc cung cap la : thamso1 thamso2 

According to Tutorialspoint

Previous lesson: Memory management in C

Next article: What is C ++?

3.5 ★ | 2 Vote

May be interested

  • 5 best command line emulation software for Windows 105 best command line emulation software for Windows 10
    currently, users can get a shell within windows 10, but many still prefer the more configurable command line emulator applications. so this article will introduce you to five of the best command line emulation software for windows 10.
  • Bitsadmin getdisplayname and bitsadmin geterror command in WindowsBitsadmin getdisplayname and bitsadmin geterror command in Windows
    the bitadmin getdisplayname command takes the display name of the specified task. the bitsadmin geterror command retrieves detailed error information for the specified job.
  • Bcdboot command in WindowsBcdboot command in Windows
    the bcdboot command allows you to quickly set up the system partition, or to repair the boot environment on the system partition.
  • How to use the Linux command line on Android with TermuxHow to use the Linux command line on Android with Termux
    android is a very operating system 'capacity with more and more desktop accessibility applications. however, sometimes you want to make some things on android that can be as easy as desktop. fortunately, you can use the termux tool, which builds on the existing infrastructure and provides a command line environment that allows you to install real linux applications on your android device.
  • How to create a command line program in Python with ClickHow to create a command line program in Python with Click
    click is a python package to write command line interfaces with as little code as possible. this article will show you how to use click to create the command line program.
  • Cmd command in WindowsCmd command in Windows
    the cmd command starts a new version of the command interpreter, cmd.exe. if used without parameters, cmd will display copyright information and the version of the operating system.
  • Set command in WindowsSet command in Windows
    the set command helps show, set or remove cmd.exe environment variables.
  • 10 tips for using Command Line Windows 10 users should know10 tips for using Command Line Windows 10 users should know
    you can use the cmd command to perform some tasks that normally only use mouse, drag and click. the cmd command is also quite useful when you need to create scripts and automated tasks.
  • Fc command in WindowsFc command in Windows
    the fc command compares two files or a collection of files and displays the differences between them.
  • 12 best command line emulators for Windows12 best command line emulators for Windows
    nowadays, users can get a shell inside windows 10, but many still prefer configurable command line emulators. therefore, this article will introduce to you five of the best command line emulator software for windows 10.