关于sprintf整合%d出错的问题

有些编译器用到sprintf函数整合%d类型的数据时候可能会出错例如
错误的示例

char *p[80]=0;
memset(p,0,sizeof(p));
sprintf((char *)p,"AT+CIPSTART=\"TCP\",\"%d.%d.%d.%d\",%d\r\n",210,209,82,119,8080);
PrintString1(p);



输出的值为 : AT+CIPSTART=”TCP”,”-11567.21111.8080.0”,53

这是因为某些编译器不知道需要整合的数是什么类型导致的 所以需要加上强制转换类型才可以输出我们需要的答案 
正确的方法

char *p[80]=0;
memset(p,0,sizeof(p));
sprintf((char *)p,"AT+CIPSTART=\"TCP\",\"%d.%d.%d.%d\",%d\r\n",(int)210,(int)209,(int)82,(int)119,(int)8080);
PrintString1(p);



此时输出的是 :AT+CIPSTART=”TCP”,”210.209.82.119”,8080


原文链接:https://blog.csdn.net/SimpleSwing/article/details/64127453

发布了95 篇原创文章 · 获赞 28 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/happygrilclh/article/details/104557334