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
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;
}
Discussion:
15 comments Page 1 of 2.
Sundar said:
2 decades ago
Hi Andrew,
As you said, argv[0] always contains the filename of the program being called.
But atoi() converts "myprog.exe" (string) into 0(integer zero).
So, 0 + 1 + 2 + 3 = 6.
Therefore it will print 6 as output.
Note:
=====
1. atoi("123Sundar.exe") will return 123.
2. atoi("sundar.exe") will return 0.
3. While calling the above program (exe) from command prompt like "C:\Turboc>myprog 1 2 3", the argv[0] will contain "C:\TURBOC\MYPROG.EXE".
While converting the value with atoi("C:\TURBOC\MYPROG.EXE"), it returns 0 (zero).
As you said, argv[0] always contains the filename of the program being called.
But atoi() converts "myprog.exe" (string) into 0(integer zero).
So, 0 + 1 + 2 + 3 = 6.
Therefore it will print 6 as output.
Note:
=====
1. atoi("123Sundar.exe") will return 123.
2. atoi("sundar.exe") will return 0.
3. While calling the above program (exe) from command prompt like "C:\Turboc>myprog 1 2 3", the argv[0] will contain "C:\TURBOC\MYPROG.EXE".
While converting the value with atoi("C:\TURBOC\MYPROG.EXE"), it returns 0 (zero).
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.
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.
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.
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.
Andrew said:
2 decades ago
The answer given is incorrect. argv[0] is normally the filename of the program being called. The list of argumnets would be myprog.exe 1 2 3 Therefore atoi(argv[0]) does not return what you expect 1.
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.
0136.
Ayush said:
1 decade ago
Hii sundar I cannot understand how the command line arguments are assigned to variable argc and argo please explain the whole process.
Rran said:
10 years ago
Can kindly any one please explain how to execute in Dev CPP to dos please explain the process please?
Vanaraju said:
1 decade ago
I didn't understand please give me another explanation.
Apurva Nigam said:
1 decade ago
Really helpful explanation @Sundar.
Thanks.
Thanks.
Anonymus said:
6 years ago
Thanks for the clear explanation @Sundar.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers