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

Command Line Arguments Questions and Answers

11.

If the following program (myproc.c) is present in the directory "C:\TC" then what will be output of the program if run it from DOS shell?

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

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

 

Answer: B

Correct answer: C:\TC\MYPROC.EXE

Under MS-DOS from version 3.0 onwards, the full pathname of the executable is available to the program, and Turbo C places it in argv[0] in upper case with the .EXE extension.

That is a platform convention rather than a rule of the language. The C standard says only that argv[0] represents the program name, and leaves the form open - on Unix-like systems it is generally the string the user typed, so the same program invoked as ./myproc would report exactly that.

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.

Answer: C

Correct answer: In place of ac and av, argc and argv should be used.

That is the false statement. The names are conventional, not required: the compiler cares only that the first parameter is an int and the second a char **, so ac and av work exactly as well.

The other three are true. ac holds the count of arguments including the program name, av[] holds their addresses, and both are ordinary parameters of main and therefore local to it - which is why passing them on to other functions, or copying them into globals, is necessary.

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.

15.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        int i = 97, *p = &i;
        foo(&p);
        printf("%d ", *p);
        return 0;
    }
    void foo(int **p)
    {
        int j = 2;
        *p = &j;
        printf("%d ", **p);
    }

 

Answer: A

Correct answer: Undefined behaviour

foo is given the address of the caller's pointer, so *p = &j does change main's p - that part works, and foo prints 2.

But j is an automatic variable of foo, and its lifetime ends when foo returns. main is then left with a dangling pointer, and *p is undefined behaviour. In practice the stack often still holds 2, which is why the value usually appears - but the compiler is entitled to assume the case never arises.

Please log in or register to report this question.

16.

What will be the output of the program (sample.c) given below if it is executed from the command line (Turbo C in DOS)?
cmd> sample 1 2 3

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

int main(int argc, char *argv[])
{
    int j;
    j = argv[1] + argv[2] + argv[3];
    printf("%d", j);
    return 0;
}

 

Answer: C

Correct answer: Error

argv[1], argv[2] and argv[3] are char * values - addresses, not numbers - and C does not define addition between two pointers. The compiler rejects the expression.

The mistake is a common one, and it rests on forgetting that everything arriving on the command line is text. To get 6 the program would need to convert each string with atoi or strtol first, and only then add the resulting integers.

Please log in or register to report this question.

Answer: C

Correct answer: 'c' means argument count 'v' means argument vector

The two parameters are named for what they hold: argc is the argument count and argv the argument vector, "vector" being the older term for a one-dimensional array.

The names are conventional rather than required - the compiler cares only about the types - but they are universal enough that departing from them is unhelpful. argv is an array of char *, one pointer per argument, terminated by a null pointer at argv[argc].

Please log in or register to report this question.

18.

The maximum combined length of the command-line arguments including the spaces between adjacent arguments is

Answer: D

Correct answer: It may vary from one operating system to another

The C standard says nothing about how long a command line may be; the limit belongs to the operating system and its shell.

MS-DOS allowed 127 characters, which is where the figures in the distractors come from. Modern systems are far more generous - Linux permits a few hundred kilobytes for the whole argument and environment block, and Windows around 32,000 characters - and the limit differs again between shells. Portable code therefore reads long inputs from a file or from standard input rather than from the command line.

Please log in or register to report this question.

19.

What will be the output of the following C code (run without any command line arguments)?

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

 

Answer: B

Correct answer: Executablefilename

With no arguments argc is 1. The condition while (argc--) tests the current value and then decrements, so the first test sees 1, which is true, and the body runs with argc now 0.

The call is therefore printf("%s\n", argv[0]), printing the program name. The next test sees 0, which is false, and the loop ends. The post-decrement is what makes the indexing come out right: the value tested and the index used differ by one.

Please log in or register to report this question.

20.

What will be the output of the program (sample.c) given below if it is executed from the command line (turbo c under DOS)?
cmd> sample Good Morning

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

int main(int argc, char *argv[])
{
    printf("%d %s", argc, argv[1]);
    return 0;
}

 

Answer: A

Correct answer: 3 Good

Two things are being asked at once. argc counts the program name along with the arguments, so sample Good Morning gives 3, not 2.

And argv[1] is the first argument after the program name, which is "Good" - argv[0] holds the program name itself. The off-by-one in each is the same idea seen twice: the count and the indexing both include argv[0].

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