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 ++?
You should read it
- The Nox App Player command line parameters support
- How to schedule Linux commands with 'at'
- Manage the Event Log with the command line
- Forget the GUI, the Command Line is returning
- Diskcomp command in Windows
- 5 reasons why people love the Linux command line
- 5 best command line emulation software for Windows 10
- How to use the Linux command line on Android with Termux
- How to create a command line program in Python with Click
- PowerShell command in Windows
- Cmd command in Windows
- 10 tips for using Command Line Windows 10 users should know
Maybe you are interested
Difference between arguments and parameters in C/C++
Mistral announces Large 2: flagship LLM with 123 billion parameters
How to install Cheat Engine to change game parameters
TypeScript optional parameters in Callbacks
What is Intel Core i5 1135g7? Detailed parameters and performance
Setting the parameters of the digital camera