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

  • assert.h in CPhoto of assert.h in C
    the file header named assert.h from library c provides a macro called assert that can be used to test an assumption made by the program and print a diagnostic message to find the error if this assumption is false.
  • ctype.h in CPhoto of ctype.h in C
    the header file with the name ctype.h of the c library declares some pretty useful functions for checking and mapping characters.
  • errno.h in CPhoto of errno.h in C
    the file header named errno.h in standard c library defines the integer variable errno, which is set by system call and some library functions for errors to indicate that an error is occurring. this macro is an extension of the type of lvalue type int which can be modified, so it can be read and modified by a program.
  • float.h in CPhoto of float.h in C
    the header file named float.h of library c contains a set of diverse constants (platform dependent) related to floating point real number values.
  • limits.h in CPhoto of limits.h in C
    the file header named limits.h in library c defines the various attributes of different variable types. macros, defined in this header, limit the value of various variable types such as char, int, and long.
  • locale.h in CPhoto of locale.h in C
    header files named locale.h in library c define their own location settings, such as date format and currency symbols.