How to store 2 digit integer value and float values in char array in C -
following c code print values of char getting unexpected results. code
#include<stdio.h> main() { char sendbuffer[1000]; float count; int i; for(count=1.5;count<=2.5;) { for(i=0;i<=15;) { sendbuffer[0]=count+48; sendbuffer[1]='a'; sendbuffer[2]='b'; sendbuffer[3]='c'; sendbuffer[4]=i+48; sendbuffer[5]='\0'; printf("%s\n",sendbuffer); i=i+5; } count=count+0.5; } }
the results getting are:
1abc0 1abc5 1abc: 1abc? 2abc0 2abc5 2abc: 2abc? 2abc0 2abc5 2abc: 2abc?
whereas, expecting
1.5abc0 1.5abc5 1.5abc10 1.5abc15
and on. can tell me how store integer , float values in char array in c?
this can work:
snprintf(sendbuf, sizeof(sendbuf), "%.1fabc%d", count, i);
Comments
Post a Comment