Float to Mixed Numbers
why strcmp not comparing strings ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <stdio.h>
#include <string.h>
main()
{
float n = 0.5;
float x = 0;
char buff[2], b[2];
sprintf(buff, "%f", n);
int i = 1;
for(i = 1; i <= 100; i++){
x = (1.0/i);
sprintf(b, "%f", x);
if(!strcmp(buff, b)){
printf("%d / %d\n", 1, i);
break;
}
}
}
|
buff and b aren't large enough and are almost certainly being overwritten.
You should never use sprintf, use snprintf instead.
Topic archived. No new replies allowed.