问题描述:

I want to do something similar to this in C language:

# This example is in Python language

var1 = 10

var2 = 45

var3 = 76

text = "Numbers are: %d, %d, %d." % (var1, var2, var3)

Is this possible in C? I want to say a solution for pure C, not C++.

Thanks.

EDIT:

I don't want to print the string directly, I only want to storage formatted string.

网友答案:

Use snprintf()

int var1 = 10;
int var2 = 45;
int var3 = 76;
char text[100];

snprintf(text, sizeof text, "Numbers are: %d, %d, %d.", var1, var2, var3);
网友答案:

Closest implementation would be:

#include <stdio.h>

int main(void){
    char text[100];
    int var1 = 10;
    int var2 = 45;
    int var3 = 76;
    sprintf(text, "Numbers are : %d, %d, %d", var1, var2, var3);
}
网友答案:

Yes it is possible in C

int main()
{
int var1,var2,var3;
var1 = 10;
var2 = 45;
var3 = 76;
printf("Numbers are: %d %d %d", var1, var2, var3);
return 0;
}

Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. For more details about printf() you can visit the link below: refer : http://www.cplusplus.com/reference/cstdio/printf/ it's same for C also

相关阅读:
Top