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

Command Line Arguments Questions and Answers

Answer: A

Correct answer: The number of command-line arguments the program was invoked with;

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

The second parameter is argv, the array of pointers to the argument strings. Note the wording of the question: it asks about the first parameter of main, not about the first item the user typed, which is the program name and lives in argv[0].

Please log in or register to report this question.

Answer: A

Correct answer: ptr is a pointer to integer, p is not

The * binds to the declarator, not to the type, so it applies to ptr alone. p is declared as a plain int.

This is why the style int *ptr; - asterisk against the name rather than the type - is often preferred: it makes the grouping visible. Declaring two pointers requires the asterisk on each, as int *ptr, *p;, and the mistake here is a classic source of confusion in code that declares several variables at once.

Please log in or register to report this question.

Answer: B

Correct answer: argument count, argument vector

argc is the argument count - the number of arguments including the program name - and argv is the argument vector, "vector" being the older word for a one-dimensional array.

The array holds one char * per argument, with argv[0] naming the program and argv[argc] guaranteed to be a null pointer. The names are conventional rather than mandated; what the compiler requires is the types, an int followed by a char **.

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.

35.

What will be the output of the following C code (if run with no options or arguments)?

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

 

Answer: A

Correct answer: 1

Run with no options or arguments, argc is 1: the program name in argv[0] is itself counted.

The value is not platform-dependent in any practical sense - the standard requires the program name to be represented and counted when it is available - so the second and third options fail. The single theoretical exception is a program started by an exec call passing no name at all, which gives argc of 0, and no shell does that.

Please log in or register to report this question.

36.

Does there exist any way to make the command-line arguments available to other functions without passing them as arguments to the function?

Answer: A

Correct answer: Yes

The parameters of main are local to it, but their values need not stay there. The straightforward method is to copy them into file-scope variables at the top of main, after which any function in the translation unit can read them.

Some environments also expose them directly: Turbo C provides the globals _argc and _argv, and several Unix systems have equivalents. Either way the answer is yes, though the portable route is the explicit copy into globals of one's own.

Please log in or register to report this question.

37.

Which character is used to separate different arguments?

Answer: C

Command line arguments are separated by space. So if you write
./output This is a single parameter
then they will interpreted as 5 command line arguments as shown : [“./output”, “This”, “is”, “single”, “parameter”].

Please log in or register to report this question.

38.

What is the output of this C code?

#include <stdio.h>
    int *f();
    int main()
    {
        int *p = f();
        printf("%d\n", *p);
    }
    int *f()
    {
        int j = 10;
        return &j;
    }

 

Answer: A

Correct answer: Undefined behaviour

j is an automatic variable, so its lifetime ends when f returns. The pointer handed back addresses storage that has been reclaimed, and dereferencing it is undefined behaviour.

In practice the value often survives long enough to print as 10, because nothing has yet overwritten that part of the stack - but the call to printf itself uses the stack and may destroy it, and the compiler is entitled to assume the situation never arises. Allocating with malloc, as the companion question does, is the correct fix.

Please log in or register to report this question.

39.

What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three

/* myprog.c */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    printf("%s\n", *++argv);
    return 0;
}

 

Answer: B

Correct answer: one

++argv advances the pointer to argv[1] and yields the new value, so *++argv is argv[1] - the first argument after the program name.

With the command line myprog one two three that is the string "one", and %s prints it in full. The pre-increment is what skips the program name: argv[0] holds that, and shifting the pointer past it is a common idiom when a program wants only its arguments.

Please log in or register to report this question.

40.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        char *p = NULL;
        char *q = 0;
        if (p)
            printf(" p ");
        else
            printf("nullp");
        if (q)
            printf("q\n");
        else
            printf(" nullq\n");
    }

 

Answer: A

Correct answer: nullp nullq

Both pointers are null. NULL is a null pointer constant, and so is a literal 0 in a pointer context - the compiler converts it, which is why char *q = 0; is legal and gives the same value as = NULL.

A null pointer tests false in a condition, so both if statements take their else branch. Note that the internal representation is not required to be all-zero bits; what the standard guarantees is the conversion from the constant 0 and the comparison, which is all the program relies on.

Please log in or register to report this question.

Loading…
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