Exercise: Command Line Arguments

Questions for: Command Line Arguments

What will be the output of the program (sample.c) given below if it is executed from the command line (turbo c under DOS)?
cmd> sample Good Morning
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%d %s", argc, argv[1]);
    return 0;
}
A:
3 Good
B:
2 Good
C:
Good Morning
D:
3 Morning
Answer: A
No answer description is available. Let's discuss.
What will be the output of the program (sample.c) given below if it is executed from the command line (Turbo C in DOS)?
cmd> sample 1 2 3
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    int j;
    j = argv[1] + argv[2] + argv[3];
    printf("%d", j);
    return 0;
}
A:
6
B:
sample 6
C:
Error
D:
Garbage value
Answer: C

Here argv[1], argv[2] and argv[3] are string type. We have to convert the string to integer type before perform arithmetic operation.

Example: j = atoi(argv[1]) + atoi(argv[2]) + atoi(argv[3]);

What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three
/* myprog.c */
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char **argv)
{
    printf("%s\n", *++argv);
    return 0;
}
A:
myprog
B:
one
C:
two
D:
three
Answer: B
No answer description is available. Let's discuss.
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three
/* myprog.c */
#include<stdio.h>

int main(int argc, char **argv)
{
    printf("%c\n", **++argv);
    return 0;
}
A:
myprog one two three
B:
myprog one
C:
o
D:
two
Answer: C
No answer description is available. Let's discuss.
What do the 'c' and 'v' in argv stands for?
A:
'c' means argument control 'v' means argument vector
B:
'c' means argument count 'v' means argument vertex
C:
'c' means argument count 'v' means argument vector
D:
'c' means argument configuration 'v' means argument visibility
Answer: C
No answer description is available. Let's discuss.
Ad Slot (Above Pagination)
Quiz