使用 C 在单个 printf 语句中打印多个不同的东西
printing multiple different things in single printf statement using C
我正在尝试将三个 printf 语句转换为一个更长的 C 语句。
我要转:
printf("The current system time is ");
printf("%f", get_current_time());
printf("\n");
成单个 printf
语句,其中函数 get_current_time
returns 一个 double
,例如 1459231814.421397
或类似的。
我设法得到的最接近的是
printf("The current system time is " , ("%f" (get_current_time())), "\n");
其中returns只有一处错误,即:
error: called object is not a function or function pointer
:
printf("The current system time is " , ("%f" (get_current_time())), "\n");"
它指出%f
之前的"
。
三个分隔的printf
语句打印:
"The current system time is 1459231876.947026"
或类似的固定次数,每次换行,这就是我想要的。
我不明白为什么它在分开时有效,但将它们加在一起成为一个长语句却失败了。我确定我必须错误地格式化较长的语句,只是不确定我到底做错了什么。
非常感谢任何帮助,如果这个问题的格式不是很好,我很抱歉,这是我问的第一个问题。
这是 printf
的基本用例
printf("%s%f\n", "The current system time is ", get_current_time());
你想要的是:
printf("%s%f\n", "The current system time is ", get_current_time());
或
printf("The current system time is %f\n", get_current_time());
不确定为什么还没有人提到这个,但是:
printf("The current system time is %f\n", get_current_time());
printf("The current system time is %f \n",get_current_time());
使用以下内容:
printf ( "The current system time is %f\n", get_current_time() );
我正在尝试将三个 printf 语句转换为一个更长的 C 语句。 我要转:
printf("The current system time is ");
printf("%f", get_current_time());
printf("\n");
成单个 printf
语句,其中函数 get_current_time
returns 一个 double
,例如 1459231814.421397
或类似的。
我设法得到的最接近的是
printf("The current system time is " , ("%f" (get_current_time())), "\n");
其中returns只有一处错误,即:
error: called object is not a function or function pointer
:
printf("The current system time is " , ("%f" (get_current_time())), "\n");"
它指出%f
之前的"
。
三个分隔的printf
语句打印:
"The current system time is 1459231876.947026"
或类似的固定次数,每次换行,这就是我想要的。
我不明白为什么它在分开时有效,但将它们加在一起成为一个长语句却失败了。我确定我必须错误地格式化较长的语句,只是不确定我到底做错了什么。 非常感谢任何帮助,如果这个问题的格式不是很好,我很抱歉,这是我问的第一个问题。
这是 printf
printf("%s%f\n", "The current system time is ", get_current_time());
你想要的是:
printf("%s%f\n", "The current system time is ", get_current_time());
或
printf("The current system time is %f\n", get_current_time());
不确定为什么还没有人提到这个,但是:
printf("The current system time is %f\n", get_current_time());
printf("The current system time is %f \n",get_current_time());
使用以下内容:
printf ( "The current system time is %f\n", get_current_time() );