Search code examples
pythoncarraysstringstring-formatting

Formatting string in C


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++.

EDIT:

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


Solution

  • 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);