Command Line Argument

There are three types of declaration we can write it:

  1. User-Friendly:
int i;
scanf("%d",i); //here compiler will waits for user input

2. OS-Friendly:

int i = 7;
//it directlly takes i = 7 as a will not wait for user input

3. Local Time:

  • Here command line arguments will come in picture. What is it? will try to understand in this blog post.

While we are taking input from CMD:

main(int argc, char **argv)
//we have to change the main function prototype
main(int argc, char **argv, char **env)
//use in system programming

Here argc holds a number of argument, we are providing through the command line, including the executable file name.

Here argv holds the base address for array pointer, where command line arguments there.

For example :

main(int argc, char **argv)
    printf("argc = %d\n",argc);

//here while compiling a program we need to give arguments like below examples

answer : argc = 1

username@systemname$./a.out 10 20
answer : argc = 3

username@systemname$./a.out "abc_def"
answer : argc = 2
  • Whatever the type of data we are passing through command line those are treated as a String.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by

Up ↑

%d bloggers like this: