Sunday, July 12, 2009

My "C" programme is not working correctly ?

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





main()


{


int a,b,c;


printf("Enter values :");


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


if(a%26gt;b %26amp;%26amp; a%26gt;c)


{


printf("\nThe largest number is :",a);


}


else if(b%26gt;c %26amp;%26amp; b%26gt;a)


{


printf("\nThe largest number is :",b);


}


else


{


printf(c);


}


}





Above given is my C programme to find the largest number. My compiler does not show any error. But it takes input of only variable. I have tried many times but it only takes one input. Why is this so ???

My "C" programme is not working correctly ?
Sir the main problem is you have commas in the scanf statement





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





while inputing the values you must enter as below if you would like to enter 1, 2 %26amp; 3





1,2,3





If you miss to add comma and instead if you press enter it will stop getting values





instead you can use





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








Also see the last printf statement printf(c); please change that to





printf("%d", c);





Hope this helps
Reply:First


------------


In the printf function, you use something called a format specifier to display numbers. The format specifier for an integer is %d, so use that where you want your integer displayed. Example:





int x = 1;


printf("The number 1 is equal to %d", x);





This will output: "The number 1 is equal to 1"





So change





printf("\nThe largest number is :",a);





to printf("\nThe largest number is: %d",a);





Make a similar change to the next printf call.





Second


----------------------





printf(c);





is a no go. The function prototype for printf is:





int printf(const char * ...), which means essentially that it accepts a variable number of character pointers. The argument you pass it, c, is interpreted at a pointer, and the function attempts to print the string starting at whatever address c points to, and ending at the first occurrence of a NULL character. Even if the program is allowed to do this, and chances are it's not, the output would be a bunch of gibberish formed by values pulled out of some spot in process memory.





This should not technically cause a compiler error, but any compiler worth its salt should give you a warning that you are using an int as a const char pointer, or something to that effect.





The fix: printf("%d", c)
Reply:use this line instead


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





also as linked and loaded said


replace


printf("\nThe largest number is :",a);


with


printf("\nThe largest number is : %d",a);


and


printf("\nThe largest number is :",b);


with


printf("\nThe largest number is : %d",b);


and finally


printf(c);


with


printf("%d",c);
Reply:Dear, You should determin format of variables in scanf and printf : for example %d for an integer variable





change your program to :


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





main()


{


int a,b,c;


printf("Enter values :");


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


if(a%26gt;b %26amp;%26amp; a%26gt;c)


{


printf("\nThe largest number is %d:",a);


}


else if(b%26gt;c %26amp;%26amp; b%26gt;a)


{


printf("\nThe largest number is %d:",b);


}


else


{


printf("%d",c);


}


}


No comments:

Post a Comment