Learn C Language 

Learn C Programming Language Step By Step. It’s easy to learn c programming language with real-life examples and hands-on. We are trying our best to deliver quality content.

Command Line Argument

 

« Previous Page                                                                                                                             Next Page »

It is very important to understand command line arguments in c language in order to crack an interview round of many companies.

The process of passing a parameter or parameters to a program during execution of a program or runtime of a program is called as command-line arguments in c. In other words, the Command line argument is a parameter that is passed to a program during execution. It is an important concept in C programming. It is possible to pass some values from the command line to your C program whenever that program executes. These values are called command-line arguments in c and the other name of an argument is a parameter.

A parameter is a singular of parameters (grammatically). However, the original definition goes here: A parameter is just a value that is passed to a program in the parenthesis of a function during the execution of a program through command line. A parameter is also called as an argument.

When To Use?

Many times it becomes necessary to control a program from outside instead of hardcoding those values inside the program. In that situation, you need to use the command line argument method to implement your program.

Components Of Command-Line

There are two important components in command line arguments in c programming. It is eminent to note that command line arguments in c language are handled using the main() function.

  • agrc 
  • argv[]

Look At Above Components One By One

  • argc: The argc stands for argument count. It refers to the number of arguments or parameters passed.
  • argv[]: The argv[] stand for argument vector. It is a pointer array or an array of a pointer which points to each argument passed to the program.

 

As you can clearly see in the above syntax that argc (argument counter ) and argv[] (argument vector) are passed as parameters inside the main function. The asterik (*) is prefixed before argv[] to instruct the compiler this is a special type of the variable and that is called pointer variable. The data type of argc is an integer because arguments passed inside the function is always an integer. For example, it maybe 1,2,3 (all are an integer) but never be 1.3 (this is just meaningless).

Case Analysis

First Case: No argument is Passed

When the above code is compiled and executed without passing any parameter, it gives the following result.

OUTPUT: One Argument is needed

Second Case: One argument is Passed 

When the above code is compiled and executed by passing the only one parameter, it gives the following result.

OUTPUT: The argument passed is Test_Program(name of the program)

First Case: Two arguments are Passed

When the above code is compiled and executed by passing two parameters, it gives the following result.

OUTPUT: Too many arguments are passed

Important Points

  • The argv[0] holds the name of the program itself and argv[1] is a pointer to the first command-line argument supplied, and *argv[n] is the last argument.
  • The default value of argc is zero. In other words, if no arguments are passed, argc will be one, and if you will pass one argument then the value of argc will be 2.

What if you want to pass all arguments in one go?

Well, it’s possible to pass all the arguments in one go. You can pass all the arguments through the command line by separating each argument by space. For example, argument1 argument2 (not like argument1argument2).

What happens when the argument itself has space?

When the argument itself has space then you can pass them in two ways. One way is to put all the arguments inside double quotes (” “) and the other is inside a single quote (‘ ‘).

For example, “argument1 argument2” or ‘argument1 argument2’.

Top Book Suggestion

The two books on C Programming Language that I Personally Recommend. I really loved reading these books. These books are listed for quality content, easy steps, and affordable price. You can get it from Amazon and Flipkart. 

 

Check The Price On Amazon:

1. C Programming Beginner’s Guide

2. Head First C: A Brain-Friendly Guide

Check The Price On Flipkart:

1. Introduction to C Programming 

2. Head First C: A Brain-Friendly Guide

« Previous Page                                                                                                                             Next Page »

Buy Premium Courses At Lowest Price. Become A Certified Developer Today!

Best Books Selected by Our Experts

Buy Premium Projects At Lowest Price

Ebooks Written by Our Experts

CoursePot.Com

Buy Premium Courses At Lowest Price. Grab Golden Opportunity To Become A Certified Developer Today!

Useful Links

Pin It on Pinterest

Share This