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 3 of 3.
Parag said:
1 decade ago
@Sundar
Nice ans. Thanks.
Nice ans. Thanks.
Cool buddy said:
1 decade ago
Sundar rocks.
Karthi said:
1 decade ago
Thank you somuch sundar. Its clear now.
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)
Deepika said:
1 decade ago
Anyone please help me. How is it works?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers