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

Command Line Arguments Questions and Answers

41.

What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample friday tuesday sunday

/* sample.c */
#include <stdio.h>

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

 

Answer: C

Correct answer: sunday tuesday friday sample

The parameter is merely named sizeofargv; it is still the argument count, which is 4 here. The loop prints argv[--sizeofargv], so the indices run 3, 2, 1, 0.

That gives "sunday", "tuesday", "friday" and finally "sample" - the arguments in reverse order with the program name last, since argv[0] is included. There is no separator in the format string, so the words in fact run together in the output.

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.

43.

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)
{
    int i;
    for(i=1; i<=3; i++)
        printf("%u\n", &argv[i]);
    return 0;
}

If the first value printed by the above program is 65517, what will be the rest of output?

Answer: B

Correct answer: 65519 65521

The loop prints the addresses of successive elements of argv, and consecutive elements of an array are one element apart. Under Turbo C's 16-bit model a char * occupies 2 bytes.

\[ 65517,\ 65517+2 = 65519,\ 65519+2 = 65521 \]

The step is the size of a pointer, not of the strings pointed at, which is what the distractors misjudge. On a 64-bit system the same program would step by 8, so the arithmetic depends entirely on the platform's pointer width.

Please log in or register to report this question.

44.

Even if integer/float arguments are supplied at command prompt they are treated as strings.

Answer: A

Correct answer: True

The shell passes arguments as text, and argv is an array of char *, so a program invoked as prog 42 receives the two-character string "42" and not the number 42.

Converting is the program's own job, with atoi, strtol or sscanf. Forgetting it produces the error the companion question shows, where adding two char * values is rejected outright - and, more insidiously, arithmetic on the character codes where the compiler happens to allow it.

Please log in or register to report this question.

45.

What is the output of this C code?

#include <stdio.h>
    void foo(int*);
    int main()
    {
        int i = 10;
        foo((&i)++);
    }
    void foo(int *p)
    {
        printf("%d\n", *p);
    }

 

Answer: C

Correct answer: Compile time error

&i produces a value, not an lvalue, and ++ requires an operand it can modify. There is nowhere to store the incremented result, so the compiler rejects (&i)++.

The fix is to store the address in a pointer variable first, as the companion question does: int *p = &i; and then p++, which increments an object that exists. The rule is the same one that makes 5++ and (a+b)++ illegal.

Please log in or register to report this question.

46.

Which of the following gives the name of the program if the second parameter to the main fucntion is char **argv?

Answer: C

Correct answer: argv[0]

argv[0] holds the name by which the program was invoked, whether argv is declared as an array of pointers or as char ** - the two are equivalent as parameters.

The arguments proper begin at argv[1], which is why a program testing for arguments writes if (argc > 1). The exact contents of argv[0] are a platform convention: Unix-like systems supply the string as typed, while some others give a full path.

Please log in or register to report this question.

Answer: D

All of the statements about the second parameter is correct. It is the collection of character pointers which of which the first represents the name of the program file.

Please log in or register to report this question.

48.

What is the index of the last argument in command line arguments?

Answer: D

Correct answer: argc - 1

The vector is indexed from 0 and holds argc strings, so the valid indices run 0 to argc-1 and the last argument is at argc-1.

argv[argc] exists but is the guaranteed null pointer terminating the array, not an argument. Reading it as a string is the mistake the companion questions turn on, and beyond it - argv[argc+1] - nothing is defined at all.

Please log in or register to report this question.

49.

What will be the output of the program

#include <stdio.h>
void fun(int);

int main(int argc)
{
    printf("%d ", argc);
    fun(argc);
    return 0;
}
void fun(int i)
{
    if(i!=4)
        main(++i);
}

 

Answer: B

Correct answer: 1 2 3 4

main is declared with only argc, which is 1 when the program is run without arguments, so 1 is printed first.

fun(1) then finds i != 4 and calls main(2), which prints 2 and calls fun(2), and so on. When fun(4) is reached the test fails and the recursion stops, having printed 1 2 3 4. Calling main recursively is legal in C, though not in C++, and is a curiosity rather than a technique.

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.

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