Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers
C - Command line arguments Aptitude Questions and Answers
C programming Command Line Arguments Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on Command Line Arguments – Passing values with running programs, separate argument values, number of arguments etc.
1) What will be the output of this program (prg_1.c)? If input command is
C:\TC\BIN>prg_1 includehelp.com C C++ Java
#include <stdio.h>
int main(int argc,char* argv[])
{
printf("%d",argc);
return 0;
}
- 4
- 5
- 6
- 3
Correct Answer - 2
5
The first argument of main argc contains the total number of arguments given by command prompt
including command name, in this command total arguments are 5.
2) What will be the output of this program (prg_2.c)? If input command is
C:\TC\BIN>prg_2 1,2 "Ok" "Hii" Hello , AAA
#include <stdio.h>
int main(int counter,char** arg)
{
unsigned char tally;
for(tally=0;tally< counter; tally+=1)
printf("%s|",arg[tally]);
return 0;
}
- C:\TC\BIN\PRG_2.EXE|1,2|Ok|Hii|Hello|,|AAA|
- ERROR: Invalid argument list.
- C:\TC\BIN\PRG_2.EXE|1|,|2|Ok|Hii|Hello|,|AAA|
- 1,2|Ok|Hii|Hello|,|AAA|
Correct Answer - 1
C:\TC\BIN\PRG_2.EXE|1,2|Ok|Hii|Hello|,|AAA|
3) Which is the correct form to declare main with command line arguments ?
You can choose more than answers.
- int main(int argc, char argv[]){ }
- int main(int argc, char* argv[]){ }
- int main(int argc, char** argv){ }
- int main(int* argc, char* argv[]){ }
Correct Answer - 2 and 3
int main(int argc, char* argv[]){ } and int main(int argc, char** argv){ }
4) What will be the output of this program (prg_2.c)? If input command is
C:\TC\BIN>prg_2 Include Help .Com
#include <stdio.h>
int main(int argc,char** arg)
{
printf("%s",arg[argc]);
return 0;
}
- (null)
- .Com
- Help
- C:\TC\BIN>prg_2.exe
Correct Answer - 1
(null)
argc contains the total number of arguments passed through command prompt, in this program value of
argc will be 3, and the given arguments will store into arg[] from 0 to 2 index.
So arg[argc] => arg[3] will return null.
5) What will be the output of this program (prg_2.c)? If input command is
C:\TC\BIN>prg_3 10 20 30
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[])
{
int answer;
answer=atoi(argv[1])+atoi(argv[2]);
printf("%d",answer);
return 0;
}
- 50
- 60
- 0
- 30
Correct Answer - 4
30
1st, 2nd argument of argv are 10 and 20.