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? By IncludeHelp Last updated : March 10, 2024

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

Printing all arguments given through 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).

C program to print all arguments given through command line

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

Explanation

./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.

C Command Line Arguments Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.