Exercise: Command Line Arguments

Questions for: Command Line Arguments

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

int main(int argc, char *argv[])
{
    printf("%c", *++argv[1]);
    return 0;
}
A:
r
B:
f
C:
m
D:
y
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?
cmd> sample friday tuesday sunday
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%c", **++argv);
    return 0;
}
A:
s
B:
f
C:
sample
D:
friday
Answer: B
No answer description is available. Let's discuss.
What will be the output of the program if it is executed like below?
cmd> sample
/* sample.c */
#include<stdio.h>

int main(int argc, char **argv)
{
    printf("%s\n", argv[argc-1]);
    return 0;
}
A:
0
B:
sample
C:
samp
D:
No output
Answer: B
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?
cmd> sample "*.c"
/* sample.c */
#include<stdio.h>

int main(int argc, int *argv)
{
    int i;
    for(i=1; i<argc; i++)
        printf("%s\n", argv[i]);
    return 0;
}
A:
*.c
B:
"*.c"
C:
sample *.c
D:
List of all files and folders in the current directory
Answer: A
No answer description is available. Let's discuss.
What will be the output of the program
#include<stdio.h>
void fun(int);

int main(int argc)
{
    printf("%d ", argc);
    fun(argc);
    return 0;
}
void fun(int i)
{
    if(i!=4)
        main(++i);
}
A:
1 2 3
B:
1 2 3 4
C:
2 3 4
D:
1
Answer: B
No answer description is available. Let's discuss.
Ad Slot (Above Pagination)
Quiz