It helps if you give examples of code that doesn't work otherwise we can only guess at what code you used.
From your explanation it sounds like you are passing a formatting string directly to the write()
function (bad choice of function name by whoever wrote that example code) but write()
will just display the string exactly as is. I don't know how you tried to give the variable to be substituted in that string. You need to use something like snprintf()
to fill in a buffer according to a given format string, just like printf()
except you have two extra parameters at the start, the first is a pointer to the buffer where the string will be written and the second is the size of the buffer snprintf()
will trunctate the string so that it doesn't overrun the buffer, the terminating null char will always be added.
Code: Select all
int number = 4; // variable we are going to print
char buffer; // buffer to hold string
snprintf(buffer, 40, "Value = %d", number);
write(1, 1, buffer);