【发布时间】:2014-05-13 14:36:51
【问题描述】:
我正在做一个 Arduino 草图,并试图获得一些空间。我在一些网站上看到使用sprintf 比使用print 更好。我试过了,但它占用了更多空间。
例如:
char toWrite[18];
sprintf(toWrite,"%d/%d/%d %d:%d:%d",RTC.now().day(),RTC.now().month(),RTC.now().year()-2000,RTC.now().hour(),RTC.now().minute(),RTC.now().second());
tft.println(toWrite);
占用的空间多于:
tft.print(RTC.now().day(), DEC);
tft.print('/');
tft.print(RTC.now().month(), DEC);
tft.print('/');
tft.print(RTC.now().year(), DEC);
tft.print(' ');
tft.print(RTC.now().hour(), DEC);
tft.print(':');
tft.print(RTC.now().minute(), DEC);
tft.print(':');
tft.println(RTC.now().second(), DEC);
tft.println();
有人能解释一下为什么吗?
非常感谢!
PS : 对不起,我的英语不是我的母语 =)
【问题讨论】:
-
空间是什么意思?屏幕上?记忆? exe大小?
-
使用 C++ 流,确切地说是
std::stringstream。sprintf容易出现缓冲区溢出,printf不够灵活。 -
如果您指的是例行程序的空间,那么与打印相比,它会更大,因为它更复杂,并且会做一些聪明的事情来让您的生活更轻松。
-
@CaptainObvlious 我不相信 C++ STL 默认在 Arduino 上可用,你需要一个 separate library 。
-
对不起,我的意思是内存大小