I have found information only on conversion from int to string. However, is it possible to pass an integer to a formatted string in C?
The code I am trying to execute:
char a[] = {
"%d"
};
a[0]= 0;
printf("%s", a);
However, the output is nothing. Have I screwed up in something or should I simply convert the value as the following seems to work:
char a[] = {
"%s"
}
a[0] = "word";
printf("%s", a);
Yes, it is possible. The approach is a bit different.
The most safe method is creating a fixed-size buffer like
char buffer[100]
, then call the function fromstdio.h
calledsnprintf
. This allows you to write into buffer any formatted string with any arguments you want.This is a snippet how to do this in practice: