[英]printf specifier does not work as expected
#include <stdio.h>
int main(void){
printf("%2s\n","Monday"); //output: Monday
getch();
}
上面的代碼在整個星期一打印。 我希望它打印“ Mo”。 為什么這在Visual Studio中不起作用,還是我編碼錯誤?
%2s
指定填充,它將永遠不會縮短字符串。 要輸出少於字符串中字符的字符,請將精度指定為%.2s
:
printf("%.2s", "Monday"); //output: Mo
另外,請注意, getch
不是標准的C函數。
只需將"%2s"
替換為"%.2s"
。
printf("%.2s\n","Monday");
您可以使用
printf("%.2s\n","Monday");
有關這些http://www.cprogramming.com/tutorial/printf-format-strings.html的更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.