like

Tuesday, December 20, 2011

Command Line Argument

1] What do the 'c' and 'v' in 'argc' and 'argv' stand for?


2] According to ANSI specifications which is the correct way of decalrating main() when it receives command line arguments?

A] main(int argc, char *argv[])
B] main(argc,argv)
int argc; char *argv[];
C] main()
{
int argc; char *argv[];
}
D] None of the above.


3] What would be the output of the following program?

/* sample.c*/
main(int argc,char **argv)
{
argc = argc - (argc - 1);
printf("%s",argv[argc - 1]);
}


4] If different command line arguments are supplied at different times would the output of the following program change?
< Yes / No>

main(int argc, char *argv[])
{
printf("%d",argv[argc]);
}


5] If the following program (myprog) is run form the command line as myprog 1 2 3

What would be the output/

main(int argc, char *argv[])
{
int i;
for(i = 0; i < argc; i++) printf("%s",argv[i]); } 6] If the following program (myprog) is run form the command line as myprog 1 2 3 What would be the output? main(int argc,char *argv[]) { int i; i = argv[1] + argv[2] + argv[3]; printf("%d",i); } A] 123 B] 6 C] Error. D] "123" 7] If the following program (myprog) is run form the command line as myprog 1 2 3 What would be the output? main(int argc,char *argv[]) { int i,j = 0; for(i = o; i < argc; i++) j = j + atoi(argv[i]); printf("%d",j); } A] 123 B] 6 C] Error. D] "123" 8] Would the following program give the same output at all times? < Yes / No>

main(int argc, char*argv[])
{
strcpy(argv[0],"hello");
strcpy(argv[1],"good morning");
printf("%s %s",argv[0],argc[1]);
}


9] If the following program (myprog) is run from the command line as

myprog one two three

What would be the output?

main(int argc, char *argv[])
{
printf("%s",*++argv);
}


10] If the following program (myprog) is run from the command line as

myprog one two three

What would be the output?

main(int argc, char *argv[])
{
printf("%c",*++argv);
}


11] The variables 'argc' and 'argv' are always local to main.



12] The maximum combined length of the command line arguments including the spaces between adjacent arguments is

A] 128 characters.
B] 256 characters.
C] 67 hcaracters.
D] It may vary from one operating system to another.


13] What will the following program output?

main(int argc, char *argv[],char *env[])
{
int i;
for(i = 1; i < argc ; i++) printf("%s",env[i]); } A] List of all environment variables. B] List of all command line arguments. C] Error. D] NULL. 14] If the following program (myprog) is run from the command line as myprog "*.c" What would be the output? main(int argc, char *argv[]) { int i; for(i = 1; i < argc; i++) printf("%s",argv[i]); } A] *.c B] List of all .c files in the current directory C] "*.c" D] None of the above 15] If the following program (myprog) is run from the command line as myprog *.c What would be the output? main(int argc, char *argv[]) { int i; for(i = 1; i < argc; i++) printf("%s",argv[i]); } A] *.c B] List of all .c files in the current directory C] "*.c" D] None of the above 16] If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which? 17] Does there exist any way to make the command line arguments available to other functions without passing them as arguments to the function? < Yes / No>


18] If the following program (myprog) is run from the command line as

myprog Jan Feb Mar

What would be the output?

#include "dos.h"
main()
{
fun();
}

fun()
{
int i;
for(i = 0; i <_argc; i++) printf("%s",_argv[i]); } 19] If the following program (myprog) is present in the directory c:\bc\tucs then what would be its output? main(int argc, char *argv[]) { printf("%s",argv[0]); } A] MYPROG B] C:\BC\TUCS\MYPROG C] Error D] C:\BC\TUCS 20] Which is an easy way to extract myprog form the output of program 13.19 above? 21] Which of the following is true about argv? A] It is an array of character pointers. B] It is a pointer to an array of character pointers. C] It is an array of strings. D] None of the above. 22] If the following program (myprog) is run from the command line as myprog monday tuesday wednesday thursday What would be the output? main(int argc, char *argv[]) { while(--argc > 0)
printf("%s",*++argv);
}

A] myprog monday tuesday wednesday thursday
B] monday tuesday wednesday thursday
C] myprog tuesday thursday
D] None of the above.


23] If the following program (myprog) is run form the command line as

myprog friday tuesday sunday

What will be the output?

main(int argc, char *argv[])
{
printf("%c",(*++argv)[0]);
}

A] m
B] f
C] myprog
D] friday


24] If the following program (myprog) is run form the command line as

myprog friday tuesday sunday

What will be the output?

main(int argc, char *argv[])
{
printf("%c",**++argv);
}

A] m
B] f
C] myprog
D] friday


25] If the following program (myprog) is run form the command line as

myprog friday tuesday sunday

What will be the output?

main(int argc, char *argv[])
{
printf("%c",*++argv[1]);
}

A] r
B] f
C] m
D] y


26] If the following program (myprog) is run form the command line as

myprog friday tuesday sunday

What will be the output?

main(int sizeofargv, char *argv[])
{
while(sizeofargv)
printf("%s",argv[--sizeofargv]);
}

A] myprog friday tuesday sunday
B] myprog friday tuesday
C] sunday tuesday friday myprog
D] sunday tuesday friday













***********************************************************************

************************* ANSWERS *******************************

***********************************************************************


1] Count of arguments and vector(array) of arguments.


2] A


3] C:\SAMPLE.EXE


4] No


5] C:\MYPROG.EXE 1 2 3


6] C


7] B. When atoi() tries to convert argv[0] to a number it cannot do so (argv[0] being a file name) and hence returns a zero.


8] No


9] one


10] P


11] True


12] D


13] B


14] A


15] A


16] Yes. Compile the program as

tcc myprog wildargs.obj

This compiles the file myprog.c and links it withe the wildcard expansion module WILDCARGs.OBJ, then run the resulting executable file MYPROG.EXE.
If you want the wildcard expansion to be default so that you won't have to link your program explicitly with WILDCARGS.OBJ, you can midigy our standard C?.LIB library files to have WILDCARGS.OBJ linked automatically. To acheive htis we have to remove SET ARGV from the library and add WILDCRAGS. The commands will invoke the Turbo Librarian to modify all the standard library files (assuming the current directory contains the standard C libraries, and WILDCRAGS.OBJ):

tlib cs -setargv +wildargs
tlib cc -setragv +wildargs
tlib cm -strargv +wildargs
tlib cl -setargv +wildargs
tlib ch -setargv +wildargs


17] Yes. Using the predefined variables_arcg,_argv.


18] C:\MYPROG.EXE Jan Feb Mar


19] B


20] #include "dir.h"
main(int arc, char *argv[])
{
char drive[3],dir[50],name[8],ext[3];
printf("\n %s",argv[0]);
fnsplit(argv[0],drive,dir,name,ext);
printf("\n %s \n %s \n %s \n %s",drive,dir,name,ext);
}


21] A


22] B


23] B


24] B


25] A


26] C

No comments:

Post a Comment