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;
}
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!
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)
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'.
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'.
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.
@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.
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.
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)
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.
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.
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?
Adi said:
8 years ago
@Sundar.
But it is not mentioned about the no of arguments in the question. Anywhere. Then how? Please explain.
But it is not mentioned about the no of arguments in the question. Anywhere. Then how? Please explain.
Ramakri said:
1 decade ago
Is ** argv is same as *argv[]?
usually we use,
int main(int argc, char *argv[]);
usually we use,
int main(int argc, char *argv[]);
Shantanu Joshi said:
10 years ago
Thanks @Sundar.
Now I understand what exactly has happened.
Now I understand what exactly has happened.
Anurag said:
1 decade ago
But what about argv? It is declared as char **argv[].
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers