The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program.
Eg.
#include
int main( int argc, char *argv[] ) {
if( argc == 2 ) {
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
}
else {
printf("One argument expected.\n");
}
}
--------------------------------------------------------------------------------------------------------------
Next Page Home Last Page
--------------------------------------------------------------------------------------------------------------
1.
Which of the following syntax is correct for command-line arguments?
A. int main(int var, char *varg[])
B. int main(char *argv[], int argc)
C. int main()
{
int argv, char *argc[];
}
D. none of the mentioned
Answer: A
-------------------------------------------------------------------------------------------------------------
2.
What type of array is generally generated in Command-line argument?
A. Single dimension array
B. 2-Dimensional Square Array
C. Jagged Array
D. 2-Dimensional Rectangular Array
Answer: C
--------------------------------------------------------------------------------------------------------------
3.
The first argument in command line arguments is
A. The number of command-line arguments the program was invoked with;
B. A pointer to an array of character strings that contain the arguments
C. Nothing
D. None of the mentioned
Answer: A
---------------------------------------------------------------------------------------------------------------
4.
The second (argument vector) in command line arguments is
A. The number of command-line arguments the program was invoked with;
B. A pointer to an array of character strings that contain the arguments,one per string.
C. Nothing
D. None of the mentioned
Answer: B
-------------------------------------------------------------------------------------------------------------
5.
What is the output of this C code (run without any commandline arguments)?
#include
int main(int argc, char *argv[])
{
while (argc--)
printf("%s\n", argv[argc]);
return 0;
}
A. Compile time error
B. Executablefilename
C. Segmentation fault
D. Undefined
Answer: B
-----------------------------------------------------------------------------------------------------------
Next Page Home Last Page
More Imp Topics:
Pointers
Array
Preprocessor
Structures
Control Structures
Switch Case
Printf
Variables
File Handling
Strings
Eg.
#include
int main( int argc, char *argv[] ) {
if( argc == 2 ) {
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
}
else {
printf("One argument expected.\n");
}
}
--------------------------------------------------------------------------------------------------------------
Next Page Home Last Page
--------------------------------------------------------------------------------------------------------------
1.
Which of the following syntax is correct for command-line arguments?
A. int main(int var, char *varg[])
B. int main(char *argv[], int argc)
C. int main()
{
int argv, char *argc[];
}
D. none of the mentioned
Answer: A
-------------------------------------------------------------------------------------------------------------
2.
What type of array is generally generated in Command-line argument?
A. Single dimension array
B. 2-Dimensional Square Array
C. Jagged Array
D. 2-Dimensional Rectangular Array
Answer: C
--------------------------------------------------------------------------------------------------------------
3.
The first argument in command line arguments is
A. The number of command-line arguments the program was invoked with;
B. A pointer to an array of character strings that contain the arguments
C. Nothing
D. None of the mentioned
Answer: A
---------------------------------------------------------------------------------------------------------------
4.
The second (argument vector) in command line arguments is
A. The number of command-line arguments the program was invoked with;
B. A pointer to an array of character strings that contain the arguments,one per string.
C. Nothing
D. None of the mentioned
Answer: B
-------------------------------------------------------------------------------------------------------------
5.
What is the output of this C code (run without any commandline arguments)?
#include
int main(int argc, char *argv[])
{
while (argc--)
printf("%s\n", argv[argc]);
return 0;
}
A. Compile time error
B. Executablefilename
C. Segmentation fault
D. Undefined
Answer: B
-----------------------------------------------------------------------------------------------------------
Next Page Home Last Page
----------------------------------------------------------------------------------------------
More Imp Topics:
Pointers
Array
Preprocessor
Structures
Control Structures
Switch Case
Printf
Variables
File Handling
Strings