我在C
中有这两个变量:
double predict_label = 6.0;
double prob_estimates = 8.0;
如何将C
中的这两个变量转换为char
,并打印出一个字符串,该字符串类似于“预测标签的值为6而概率估计的值为8”。 / p>
答案 0 :(得分:6)
我不希望您转换为字符那么多,而不必打印的整数值。假设这样就足够了:
printf("predict label is %d and probability estimates is %d\n",
(int)predict_label, (int)prob_estimates);
答案 1 :(得分:2)
如果您确实想将变量值添加到字符串中,则可以使用snprintf():
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
implementation 'com.google.firebase:firebase-auth:11.6.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-core:16.0.0'
答案 2 :(得分:2)
您可以安排将没有小数位(因此也没有小数点)的浮点值打印到字符串变量中,然后可以将其打印到所需的文件中,例如使用snprintf()
。该代码还使用字符串连接来避免行太长。
#include <stdio.h>
int main(void)
{
double predict_label = 6.0;
double prob_estimates = 8.0;
char buffer[256];
snprintf(buffer, sizeof(buffer),
"The value for predict label is %.0f"
" and the value for probability estimates is %.0f.",
predict_label, prob_estimates);
printf("%s\n", buffer);
return 0;
}