C Program to Print the Program Name and All its Arguments

«
»

This is a C Program to print the program name and all its arguments.

Problem Description

This C Program Prints the Program Name and All its Arguments.

Problem Solution

It prints the program name and its Arguments using command line argument as shown in the program below.

advertisement
Program/Source Code

Here is source code of the C Program to Print the Program Name and All its Arguments. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Print the Program Name and All its Arguments
 */
#include <stdio.h>
 
void main(int argc, char *argv[])    /* command line Arguments */
{
    int i;
    for (i = 0;i < argc;i++)
    {
        printf("%s ", argv[i]);        /* Printing the string */
    }
    printf("\n");
}
Program Explanation

In this C program, we are using command line arguments to print the program name by using argc and argv[] parameters. Using for loop the entered program name and its argument are printed by initializing the value of ‘i’ variable to zero. The loop will execute till the condition that value ‘i’ variable becomes less than the value of argc parameter. Print the program name entered in the command line argument.

advertisement
Runtime Test Cases
 
$ cc arg9.c
$ a.out this is  c class by sanfoundry
a.out this is c class by sanfoundry

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn