C Programming - Command Line Arguments - Discussion

Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 17)
17.
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog 1 2 3
/* myprog.c */
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char **argv)
{
    int i, j=0;
    for(i=0; i<argc; i++)
        j = j+atoi(argv[i]);
    printf("%d\n", j);
    return 0;
}
123
6
Error
"123"
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
15 comments Page 1 of 2.

Anonymus said:   6 years ago
Thanks for the clear explanation @Sundar.

Kajol jain said:   8 years ago
@Rran.

Just save your file in dev cpp, then compile and run it. After that open command prompt, write your path where your file (program) has been saved. Then just write your program name give space and give inputs (value for the *argv[] argument) according to your code and then press enter. You'll see your output.

Devendar said:   8 years ago
I think the answer is wrong because there is no {} to for loop. The program will execute differently and the output will come as.

0136.

Parth said:   9 years ago
Thank you for this explanation.

Rran said:   10 years ago
Can kindly any one please explain how to execute in Dev CPP to dos please explain the process please?

Sunitha said:   1 decade ago
argc will take the no of command line arguments,

i.e. If "myprog one two three" then argc=4 and argv is a array of that command line arugemnt ie argv[0] means myprog,argv[1] means one like that.

Rajesh said:   1 decade ago
Nice explanation sundar.

Apurva Nigam said:   1 decade ago
Really helpful explanation @Sundar.

Thanks.

Vanaraju said:   1 decade ago
I didn't understand please give me another explanation.

Sreenivas said:   1 decade ago
Sunder your explanation was awesome.


Post your comments here:

Your comments will be displayed after verification.