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 2 of 3.
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.
Mani said:
9 years ago
Thank you @Sundar.
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?
Shantanu Joshi said:
10 years ago
Thanks @Sundar.
Now I understand what exactly has happened.
Now I understand what exactly has happened.
Cool buddy said:
1 decade ago
Sundar rocks.
Anurag said:
1 decade ago
But what about argv? It is declared as char **argv[].
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[]);
Jessie said:
1 decade ago
Thank you sundar;keep it up
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.
Adarsh said:
1 decade ago
Nice explanation.
@sundar.
@sundar.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers