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 1 of 3.

Sundar said:   1 decade ago
@Deepika:

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.

Note:

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!
(2)

Suraj said:   5 years ago
Thank you @Sundar.
(1)

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)

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'.

Deepika said:   1 decade ago
Anyone please help me. How is it works?

Mohit said:   5 years ago
Is "&" a pointer variable in c?

Manai said:   6 years ago
Nice explanations, Thanks All.

Matan said:   7 years ago
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.

Hema said:   7 years ago
Nice explanation Thanks @Sundar.

Karthi said:   1 decade ago
Thank you somuch sundar. Its clear now.


Post your comments here:

Your comments will be displayed after verification.