Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Command Line Parameter in C

Explore Command Line Parameter in C with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers command line parameter in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

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 ++?

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.