🌐
GeeksforGeeks
geeksforgeeks.org › c language › format-specifiers-in-c
Format Specifiers in C - GeeksforGeeks
5 days ago - Format specifiers are used in C to specify the type of data that should be displayed or read during input and output operations.
🌐
Cplusplus
cplusplus.com › reference › cstdio › printf
Printf
C string that contains the text to be written to stdout. It can optionally contain embedded format specifiers that are replaced by the values specified in subsequent additional arguments and formatted as requested.
🌐
W3Schools
w3schools.com › c › c_variables_format.php
C Format Specifiers
You can think of a format specifier as a placeholder that tells C what kind of value will be printed.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › format-specification-syntax-printf-and-wprintf-functions
Format Specification Syntax: `printf` and `wprintf` Functions | Microsoft Learn
Each field of the conversion specification is a character or a number that signifies a particular format option or conversion specifier. The required type field specifies the kind of conversion to be applied to an argument. The optional flags, width, and precision fields control other format aspects such as leading spaces or zeroes, justification, and displayed precision.
🌐
Scaler
scaler.com › home › topics › format specifiers in c
Format specifiers in C - Scaler Topics
March 11, 2024 - Format specifiers in C , initiated with %, denote data types for input/output in functions like scanf and printf. They specify types like integer, string, or float. Different types have unique specifiers.
🌐
cppreference.com
en.cppreference.com › c › io › fprintf
printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s - cppreference.com
(optional) length modifier that specifies the size of the argument (in combination with the conversion format specifier, it specifies the type of the corresponding argument).
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › standard › base-types › standard-numeric-format-strings
Standard numeric format strings - .NET | Microsoft Learn
Format specifier is a single alphabetic character that specifies the type of number format, for example, currency or percent. Any numeric format string that contains more than one alphabetic character, including white space, is interpreted as ...
🌐
Apple Developer
developer.apple.com › library › archive › documentation › Cocoa › Conceptual › Strings › Articles › formatSpecifiers.html
String Format Specifiers
February 11, 2014 - The format specifiers supported by the NSString formatting methods and CFString formatting functions follow the IEEE printf specification; the specifiers are summarized in Table 1. Note that you can also use the “n$” positional specifiers such as %1$@ %2$s.
🌐
Astral
docs.astral.sh › uv › reference › cli
Commands | uv
December 8, 2025 - Limit candidate packages for specific packages to those that were uploaded prior to the given date. Accepts package-date pairs in the format PACKAGE=DATE, where DATE is an RFC 3339 timestamp (e.g., 2006-12-02T02:07:43Z), a local date in the same format (e.g., 2006-12-02) resolved based on your system's configured time zone, a "friendly" duration (e.g., 24 hours, 1 week, 30 days), or an ISO 8601 duration (e.g., PT24H, P7D, P30D).
Top answer
1 of 8
137

Use sprintf. (This is NOT safe, but OP asked for an ANSI C answer. See the comments for a safe version.)

int sprintf ( char * str, const char * format, ... );

Write formatted data to string Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str.

The size of the buffer should be large enough to contain the entire resulting string (see snprintf for a safer version).

A terminating null character is automatically appended after the content.

After the format parameter, the function expects at least as many additional arguments as needed for format.

Parameters:

str

Pointer to a buffer where the resulting C-string is stored. The buffer should be large enough to contain the resulting string.

format

C string that contains a format string that follows the same specifications as format in printf (see printf for details).

... (additional arguments)

Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n). There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.

Example:

// Allocates storage
char *hello_world = (char*)malloc(13 * sizeof(char));
// Prints "Hello world!" on hello_world
sprintf(hello_world, "%s %s!", "Hello", "world");
2 of 8
36

If you have a POSIX-2008 compliant system (any modern Linux), you can use the safe and convenient asprintf() function: It will malloc() enough memory for you, you don't need to worry about the maximum string size. Use it like this:

char* string;
if(0 > asprintf(&string, "Formatting a number: %d\n", 42)) return error;
log_out(string);
free(string);

This is the minimum effort you can get to construct the string in a secure fashion. The sprintf() code you gave in the question is deeply flawed:

  • There is no allocated memory behind the pointer. You are writing the string to a random location in memory!

  • Even if you had written

    char s[42];
    

    you would be in deep trouble, because you can't know what number to put into the brackets.

  • Even if you had used the "safe" variant snprintf(), you would still run the danger that your strings gets truncated. When writing to a log file, that is a relatively minor concern, but it has the potential to cut off precisely the information that would have been useful. Also, it'll cut off the trailing endline character, gluing the next log line to the end of your unsuccessfully written line.

  • If you try to use a combination of malloc() and snprintf() to produce correct behavior in all cases, you end up with roughly twice as much code than I have given for asprintf(), and basically reprogram the functionality of asprintf().


If you are looking at providing a wrapper of log_out() that can take a printf() style parameter list itself, you can use the variant vasprintf() which takes a va_list as an argument. Here is a perfectly safe implementation of such a wrapper:

//Tell gcc that we are defining a printf-style function so that it can do type checking.
//Obviously, this should go into a header.
void log_out_wrapper(const char *format, ...) __attribute__ ((format (printf, 1, 2)));

void log_out_wrapper(const char *format, ...) {
    char* string;
    va_list args;

    va_start(args, format);
    if(0 > vasprintf(&string, format, args)) string = NULL;    //this is for logging, so failed allocation is not fatal
    va_end(args);

    if(string) {
        log_out(string);
        free(string);
    } else {
        log_out("Error while logging a message: Memory allocation failed.\n");
    }
}
🌐
GitHub
github.com › davidinmichael › c-programming-tutorial › blob › main › 03-format-specifiers.c
c-programming-tutorial/03-format-specifiers.c at main · davidinmichael/c-programming-tutorial
A step by step tutorials on the basics of c, learn with practical and exercises to help you improve. - c-programming-tutorial/03-format-specifiers.c at main · davidinmichael/c-programming-tutorial
Author   davidinmichael
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › format specifiers in c
Format Specifiers in C | Types and Usage with Examples
June 3, 2025 - Format specifiers in C are symbols used in functions like printf() and scanf() to tell the compiler what type of data to expect. They ensure that values are correctly displayed or read—whether it's an integer, float, character, or string.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › format-specifiers
Format Specifiers in C Programming (Full List With Examples)
March 11, 2026 - Understand the full list of format specifiers in C programming with examples. Learn how to format and print data types like int, float, char, and more!
🌐
ScienceDirect
sciencedirect.com › topics › computer-science › format-specifier
Format Specifier - an overview | ScienceDirect Topics
A format string vulnerability is another common error in the way in which user-supplied data is processed. It is common, in the C language, to use the *printf() functions to create and manipulate character strings. These functions take an argument known as the format specifier, followed by ...
🌐
Wikipedia
en.wikipedia.org › wiki › Printf
printf - Wikipedia
2 weeks ago - The function accepts a format C-string argument and a variable number of value arguments that the function serializes per the format string. Mismatch between the format specifiers and count and type of values results in undefined behavior, and the program might crash or vulnerabilities may arise.
🌐
Medium
medium.com › @Dev_Frank › format-specifier-in-c-03f14aa8936b
FORMAT SPECIFIER IN C. WHAT DOES IT EVEN MEAN? | by Dev Frank | Medium
February 9, 2024 - Let’s assume you have a magic box that can talk. This box can only understand certain types of things you tell it. A format specifier is like a secret code you use to tell the box what type of thing you want to talk about.