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.

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
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 Books in C Programming, Data-Structures and Algorithms

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.