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.
Suraj said:
5 years ago
Thank you @Sundar.
(1)
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.
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.
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)
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers