Command Line Arguments in C with Examples

This C Tutorial explains Command Line Arguments in C Programming with examples.

Linux system gives us a command environment where we execute commands to perform certain tasks. For example,

    ls command lists files and directories in a directory
    ll gives long listing of files and directories in a directory

Actually, such commands are programs. And we may or may not pass arguments to programs for specific needs.

How can we pass arguments to ours’ C programs?

Well! We can pass arguments to a C program through command environment. These arguments go into program’s main() parameters. A C program’s main takes generally two arguments, these are, first an integer type and the other an array of pointers-to-char.

advertisement
advertisement
    int main(int argc, char *argv[])
    {
        /* statements */
    }

argc stands for argument’s count while argv contains argument values. These names haven’t any magic associated with them and you can use any of yours’ choice for them. argv[] array is terminated by a NULL string. Therefore, you can use either of two arguments to count on no. of arguments passed to a C program.

Consider the below given command, say, to compile a program named “cmd.c”,

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
    gcc -o cmd cmd.c
    ./cmd -abc -def hello, command line" "you pass arguments to program"
            "great"

Firstly, we compiled cmd.c to a binary file with name cmd then we run the binary file cmd together with several arguments. All the arguments including program name, any options beginning with dash ‘-‘ followed by strings are passed into main’s parameters of cmd.c. Arguments passed from the command into a program are called command-line arguments. Let’s see a simple C program processing command-line arguments,

int main(int argc, char *argv[])
{
    while (*argv != NULL) {
        printf("%s", *argv);
        argv++;
    }
 
    return 0;
}

Notice that above program doesn’t do useful except it prints on the terminal arguments passed to it with the program name.

advertisement

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
If you find any mistake above, kindly email to [email protected]

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.