Sunday, July 12, 2009

C code problem?

/* Sendind and receving values between functions */


#include%26lt;stdio.h%26gt;


void calsum();


void main()


{


int a,b,c, sum;


printf("\n Enter any three numbers");


scanf("%d %d %d",%26amp;a, %26amp;b, %26amp;c);


sum= calsum(a,b,c);


printf("\nSum=%d,sum");


}


void calsum(x,y,z)


int x,y,z;


{


int d;


d=x+y+z;


return (d)


}


This code is giving errors


So where I am wrong

C code problem?
Nithinpb is correct. However, this is one more thing. When you declare a function and you are writing out the actual code for the function, you need to give the passed values a data type. So it needs to be:





int calsum(int x, int y, int z)





Also, you need to declare "int d" outside the function, otherwise it will move out of scope once you leave the function and the program won't have a variable d (because you only declare the "int d" in the function, not in the main).
Reply:It should not be void calsum(x,y,z). You are not returning any value. In the main you are expecting a value. So, its giving error.





Make it, int calsum(x,y,z).





--


Nithin.


http://www.bluminut.com
Reply:You'll also get an error on the line with printf("\nSum=%d,sum"); It should be printf("\nSum=%d",sum); Your 2nd quotation is in the wrong place.





In the future, let us know what the error says and we can give better feedback.
Reply:void calsum(x,y,z)


int x,y,z;


{


int d;


d=x+y+z;


return (d)


}





i think this line is the one giving errors because x, y, z are parameters and used without being declared, try this:





void calsum(int x,int y, int z)


{


int d;


d=x+y+z;


return (d)


}

thank you cards

No comments:

Post a Comment