811 views
1 votes
1 votes
What is the output of this C code (run without any command line arguments)?
#include <stdio.h>
  1.     int main(int argc, char *argv[])
  2.     {
  3.         while (*argv  !=  NULL)
  4.         printf("%s\n", *(argv++));
  5.         return 0;
  6.     }

In this program argv gives the base address of array of char pointer so while(*argv !=NULL) condition is true and this should give the compile time error because of *(argv++) but when I execute this prog using cmd.... c:\ <prog_name>...output comes <prog_name> so how prog name is dispalying

1 Answer

Best answer
1 votes
1 votes

See here no error is present.

#include <stdio.h>
int main(int argc, char *argv[])
 {
    while (*argv  !=  NULL)// that means argv array is not reached end of string
    printf("%s\n", *(argv));// here pointer is pointing to one address
    argv++;// here it is pointing to next address
     return 0;
}

It contain program name because argv contains program name at element 0 and command line argument when argc is 1

selected by

Related questions

0 votes
0 votes
1 answer
1
Aakash1125 asked May 11, 2017
690 views
All four are correct i think.Then why option c is false?In place of ac and av ,argc and argv should be used.and this is true.
1 votes
1 votes
1 answer
2
Sanjay Sharma asked Jun 8, 2016
1,949 views
What would be the output of the following program, if run from the command line as “myprog 1 2 3” ?main (int argc, char * argv[ ]) { int i ; i = argv + argv + argv[...
0 votes
0 votes
1 answer
3
Sanjay Sharma asked May 28, 2017
5,443 views
what is the difference between command and instruction
0 votes
0 votes
0 answers
4