C Programming - Command Line Arguments - Discussion


If the different command line arguments are supplied at different times would the output of the following program change?


int main(int argc, char **argv)
    printf("%d\n", argv[argc]);
    return 0;

[A]. Yes
[B]. No

Answer: Option B


No answer description available for this question.

Deepika said: (Oct 22, 2010)  
Anyone please help me. How is it works?

Sundar said: (Oct 22, 2010)  

agrc - It is an integer value contains the number of arguments passed to main function through command line.

Lets assume argc = 3.

The 3 arguments will be stored in the two dimensional array argv as given below.

argv[0] = 1st argument value
argv[1] = 2nd argument value
argv[2] = 3rd argument value.

But in the given program, it is mentioned like

argv[argc] it means that argv[3].

This array holds the values upto argv[2] only (argv[0] - argv[2]).

Therefore, argv[3] may be contain null value or garbage value. While converting this value as integer (because in printf statement "%d" specified), it will give 0 as output.

Therefore, the program's output will NOT change due to the number of arguments passed.


If you use %s instead of %d in the printf statement, it may print (null) or any garbage value depends upon the platform. There is a chance for abnormal program termination too.

Hope this will help you. Have a nice day!

Karthi said: (Mar 1, 2011)  
Thank you somuch sundar. Its clear now.

Cool Buddy said: (Mar 18, 2011)  
Sundar rocks.

Parag said: (Jul 11, 2011)  

Nice ans. Thanks.

Arun said: (Aug 3, 2011)  
Good explanation sundar keep going.

Meenu said: (Dec 16, 2011)  
Thank you sundar.

Deepika said: (Feb 14, 2012)  
Excellent Sundar.. :)

Neha said: (May 23, 2012)  
Thank you sundar.

Adarsh said: (Jul 21, 2012)  
Nice explanation.


Subhendu Bera said: (Aug 22, 2012)  
According to the C standard 2011 section paragraph "argv[argc] shall be a null pointer." If it is printed using %d then it will print 0.
@sundar your statement "Therefore, argv[3] may(**) be contain null value or garbage value(**)....." is wrong it will only contain null value. There is no scope of garbage and it is irrespective of compiler. If it "may be" null or gurbage then output would vary with every execution.

Jessie said: (Sep 4, 2012)  
Thank you sundar;keep it up

Ramakri said: (Dec 4, 2012)  
Is ** argv is same as *argv[]?

usually we use,

int main(int argc, char *argv[]);

Anurag said: (Sep 3, 2013)  
But what about argv? It is declared as char **argv[].

Gangadhar said: (Sep 20, 2013)  
Always argv[argc] is NULL(shall be a null pointer).

argv and argc are how command line arguments are passed to main() in C and C++.

argc will be the number of strings pointed to by argv. This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.

The variables are named argc (argument count) and argv (argument vector) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings) is equally valid.


A pointer is said to be a null pointer when its right value is 0. Remember, a null pointer can never point to valid data.

To set a null pointer, simply assign 0 to the pointer variable. For example:

char *ptr_c;
int *ptr_int;

ptr_c = ptr_int = 0;

Here ptr_c and ptr_int become null pointers after the integer value of 0 is assigned to them.

NOTE: NULL pointer is different from null character '\0'.

Shantanu Joshi said: (Sep 30, 2015)  
Thanks @Sundar.

Now I understand what exactly has happened.

Nehal said: (Mar 24, 2016)  
@Sundar- It gives segmentation when we are trying to use %s instead of %d. And still I am not getting why it prints 0 for %d?

Mani said: (Nov 27, 2016)  
Thank you @Sundar.

Adi said: (Jun 12, 2017)  

But it is not mentioned about the no of arguments in the question. Anywhere. Then how? Please explain.

Mounika said: (Jun 16, 2017)  

If number of arguments are 4 , argv[4] wont exist since 4 arguments are stored in argv[0], argv[1], argv[2], argv[3].

If number of arguments are 5 ,argv[5] wont exist since 5 arguments are stored in argv[0] to argv[4].

So if there are n arguments , argv[n] always contain same value i.e, null or garbage value.

Hema said: (Mar 26, 2018)  
Nice explanation Thanks @Sundar.

Matan said: (Jul 14, 2018)  
At the end of argv you have NULL terminator.

Let's say you have three arguments in the command line so argc=3.
then argv will be 4 element size and argv[3] will be '\0'. just like a string.

Manai said: (Jul 15, 2019)  
Nice explanations, Thanks All.

Mohit said: (Aug 25, 2020)  
Is "&" a pointer variable in c?

Suraj said: (Feb 1, 2021)  
Thank you @Sundar.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.