Machine-translated by Google. Questions are authored in English.
Top

Command Line Arguments Questions and Answers

61.

What will be the output of this program (prg_1.c)?

#include <stdio.h>
int main(int argc,char* argv[])
{
    printf("%d",argc);
    return 0;
}

The program is run as:

C:\TC\BIN>prg_1.exe 1 2 3 4

Answer: B

Correct answer: 5

The runtime hands main two things: argc, the number of command line arguments, and argv, an array of pointers to those arguments as strings. The crucial point is that the program’s own name counts. argv[0] holds the name the program was invoked by, and the arguments the user typed follow from argv[1] onwards. Here the command line is prg_1.exe 1 2 3 4: one program name plus four arguments, so \[ \text{argc} = 1 + 4 = 5. \] It is also worth knowing that argv[argc] is guaranteed to be a null pointer, so the array always has argc + 1 elements - which is what lets a loop walk argv without consulting argc at all. The command line had been lost from this record and is restored above; without it the question has no determinate answer.

Please log in or register to report this question.

Answer: B

Correct answer: int main(int argc, char* argv[]){ }

This is the conventional form: an int count followed by an array of char *, each pointing at one argument string.

char **argv is exactly equivalent, since an array parameter decays to a pointer to its first element - the third option is therefore also correct in practice, and only one answer being expected here, the array notation is the canonical spelling. char argv[] would be an array of single characters, and int *argc a pointer where a count is wanted.

Please log in or register to report this question.

63.

What will be the output of this program (prg_2.c)?

#include <stdio.h>
int main(int argc,char* argv[])
{
    printf("%s",argv[argc]);
    return 0;
}

The program is run as:

C:\TC\BIN>prg_2.exe Help .Com

Answer: A

Correct answer: (null)

The program prints argv[argc]. With the command line prg_2.exe Help .Com there are three entries in use - argv[0] is the program name, argv[1] is Help, argv[2] is .Com - so argc is 3 and the program is asking for argv[3]. That is one past the last argument, and the standard requires it to be a null pointer: argv[argc] is always null, which is what allows a program to iterate over its arguments until it meets the null rather than counting them. Passing a null pointer to %s is strictly undefined behaviour, but every widely used C library detects it and prints the literal text (null) rather than crashing, and that is the output the question expects. The distractors are the values one index lower or higher in the reader’s mental model: .Com is argv[argc-1], Help is argv[1], and the command line itself is never a value of argv at all - the shell splits it before the program sees it. The program listing and its command line had both been lost from this record and are restored above.

Please log in or register to report this question.

64.

What will be the output of this program (prg_3.c)?

#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;
}

The program is run as:

C:\TC\BIN>prg_3.exe 10 20

Answer: C

Correct answer: 30

Command line arguments always arrive as strings, never as numbers. With the command line prg_3.exe 10 20, argv[1] is the two-character string "10" and argv[2] is "20". Adding those directly would add two pointers, which C does not even permit, so the program converts each with atoi - ASCII to integer, declared in <stdlib.h> - giving \[ 10 + 20 = 30. \] Two habits are worth taking from this. First, atoi reports no errors: given text that is not a number it simply returns 0, which is indistinguishable from a genuine "0", so production code uses strtol instead. Second, the program never checks argc before reading argv[1] and argv[2]; run with fewer arguments it would dereference a null pointer and crash. The command line had been lost from this record and is restored above.

Please log in or register to report this question.

Answer: A

Correct answer: Arguments passed to main() function

Command-line arguments are the strings typed after the program name, which the runtime hands to main as argc and argv.

They reach main and no other function: the parameters are local to it, so any other function needing them must be passed them explicitly or read them from globals the program sets up. C has neither classes nor member functions on structures, so the last two options describe constructs the language does not have.

Please log in or register to report this question.

66.

To use command line arguments in C++, how many parameters are passed to the main function?

Answer: B

2 arguments are needed to be passed to main() function while using command line arguments. The first one represents a number of strings in the argument list and the second list represents the list of string arguments.

Please log in or register to report this question.

Answer: D

Correct answer: all of the mentioned

All three declarations are acceptable. char *argv[] and char **argv are equivalent, since an array parameter decays to a pointer to its first element.

Adding const is also permitted and is arguably better practice, since the program has no business modifying the strings the environment supplied. What the standard actually requires is only that the second parameter be a char ** or something that reduces to one, which all three do.

Please log in or register to report this question.

Answer: A

Correct answer: Number of command line arguments

The first parameter is argc, an int giving how many arguments the program was invoked with - the program name included, so a bare invocation gives 1.

The list of the arguments themselves is the second parameter, argv. C has neither dictionaries nor stacks as language constructs, so those options describe nothing the runtime could supply. The count exists because the array itself carries no length, only a null pointer at its end.

Please log in or register to report this question.

Answer: B

Correct answer: List of command line arguments

The second parameter is argv, an array of pointers to the argument strings - one pointer per argument, in the order typed.

argv[0] holds the program name and argv[argc] is guaranteed to be a null pointer, so the list can be walked either by counting to argc or by stopping at the null. The count is the first parameter, and C has no dictionary or stack type to offer.

Please log in or register to report this question.

70.

Which of the following is correct about the first parameter of the main function?

Answer: D

Correct answer: All of the mentioned

All three statements hold. argc is declared int; it stores the count of command-line arguments; and it is non-negative, since a count cannot be less than zero.

In practice it is at least 1, because the program name occupies argv[0] and is counted. The standard does allow 0, in which case argv[0] is a null pointer, but that arises only from an exec call supplying no name at all - never from an ordinary shell.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Command Line Arguments Questions and Answers || Command Line Arguments MCQ Questions and Answers || Command Line Arguments GK Questions and Answers || Command Line Arguments GK MCQ Questions || Command Line Arguments Multiple Choice Questions and Answers || Command Line Arguments GK || GK on Command Line Arguments || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming