ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » C programs » C Command Line Arguments programs

C program to print all arguments given through command line

Learn: How to get and print the values through the command line using the concept of Command Line Arguments in C programming language?

As we have discussed in command line argument tutorial, that we can also give the input to the program through the command line.

In this program, we will print all given arguments given through command line, there are two variables argc which stores the total number of arguments and argv which stores the array of strings that mean all arguments including command name (program’s executable file name).

#include <stdio.h>

int main(int argc, char *argv[])
{
	int counter;
	for(counter=0; counter<argc; counter++)
		printf("argv[%2d]: %s\n",counter,argv[counter]);
	
	return 0;
}

Output

sh-4.2$ ./main Hello world "how are you?" 
argv[ 0]: ./main
argv[ 1]: Hello 
argv[ 2]: world
argv[ 3]: how are you?

Consider the output

./main, Hello, and world are the single word argument while "how are you?" is multiple words arguments, to pass multiple words in command line, we can enclose them in double quotes.

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.