GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 173 of 453    Print  

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);
}

A) 1 2 3
B) 6
C) error
D) "123"



  
Total Answers and Comments: 3 Last Update: March 07, 2008   
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
January 02, 2006 02:15:20   #1  
bhaskarsinghal Member Since: December 2005   Contribution: 5    

RE: If the following program (myprog) is run from t...
ans B)6
 
Is this answer useful? Yes | No
March 06, 2008 16:27:34   #2  
pvsola Member Since: March 2008   Contribution: 6    

RE: 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]);  
ans: 6

Explanation: converting string "c:myprog.exe 123" to integer will return 0

 
Is this answer useful? Yes | No
March 07, 2008 05:15:14   #3  
vanavill Member Since: February 2008   Contribution: 7    

RE: 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]);  

initially J 0 then it incremented by the arg value one. now it is 1.then it incremented
by the arg value 2. now the value of J is 3. then it's value incremented by the arg value 3. now it's value is 6.now the loop condition over. The J 6.

so the answer is 6.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape