Example of command line parameters in C language

WBOY
Release: 2023-08-30 15:09:07
forward
1000 people have browsed it

Example of command line parameters in C language

When executing C programs, some values can be passed to them from the command line. These values are calledCommand Line Parameters, and many times they are important to your program, especially when you want to control the program from the outside rather than hardcoding these values within the code.

Command line parameters are processed using main() function parameters, whereargcrefers to the number of parameters passed,argv[]is an array of pointers to each parameter passed to program. The following is a simple example that checks if there are any arguments provided from the command line and takes appropriate action -

Sample Code

#include  int main( int argc, char *argv[] ) { if( argc == 2 ) { printf("The argument supplied is %s

", argv[1]); } else if( argc > 2 ) { printf("Too many arguments supplied.

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

"); } }

Copy after login

Output

$./a.out testing The argument supplied is testing
Copy after login

Output

$./a.out testing1 testing2 Too many arguments supplied.
Copy after login

Output

$./a.out One argument expected
Copy after login

The above is the detailed content of Example of command line parameters in C language. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!