C Programming - Command Line Arguments - Discussion

Discussion Forum : Command Line Arguments - Yes / No Questions (Q.No. 2)
2.
If the different command line arguments are supplied at different times would the output of the following program change?
#include<stdio.h>

int main(int argc, char **argv)
{
    printf("%d\n", argv[argc]);
    return 0;
}
Yes
No
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
25 comments Page 2 of 3.

Subhendu Bera said:   1 decade ago
According to the C standard 2011 section 5.1.2.2.1 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:   1 decade ago
Thank you sundar;keep it up

Ramakri said:   1 decade ago
Is ** argv is same as *argv[]?

usually we use,

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

Anurag said:   1 decade ago
But what about argv? It is declared as char **argv[].

Gangadhar said:   1 decade ago
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.

FOR MORE INFO:

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:   10 years ago
Thanks @Sundar.

Now I understand what exactly has happened.

Nehal said:   9 years ago
@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:   9 years ago
Thank you @Sundar.

Adi said:   8 years ago
@Sundar.

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

Mounika said:   8 years ago
@Adi.

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.
(1)


Post your comments here:

Your comments will be displayed after verification.