Q:
If the following program (myprog) is run from the command line as
myprog 1 2 3
what would be the output?
main(int argc, char *argv[])
{
int i, j = 0;
for (i = 0; i < argc ; i++)
j = j + atoi ( argv[i]);
printf ("%d", j);
}
Answer & Explanation
Answer: B) 6
Explanation: When atoi() tries to convert argv[0] to a number it cannot do so (argv[0] being a file name) and hence returns a zero.
View Answer
Report Error
Discuss