C sprintf() Function | Format and Store Strings

Introduction

The sprintf() function allows you to write formatted data to a string. It is similar to printf(), but instead of sending the output to the console, sprintf() stores the output in a character array (string). This function is useful when you need to format strings for further processing or storage.

sprintf() Function Syntax

The syntax for the sprintf() function is as follows:

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

Parameters:

  • str: A pointer to a character array where the formatted output will be stored.
  • format: A C string that contains text to be written to str. It can optionally contain embedded format specifiers that are replaced by the values specified in subsequent additional arguments.
  • ...: The additional arguments correspond to the format specifiers in the format string and represent the values to be formatted.

Returns:

  • The function returns the total number of characters written, excluding the null terminator.

Understanding sprintf()

The sprintf() function formats data and stores it in the provided character array (str). The function uses format specifiers to determine the type and format of the data to be written. Each format specifier starts with a percent sign (%) and is followed by a character that specifies the type of data (e.g., d for integers, f for floating-point numbers, s for strings).

Examples

Writing an Integer to a String

To demonstrate how to use sprintf() to write an integer to a string, we will write a simple program.

Example

#include <stdio.h>

int main() {
    char buffer[50];
    int number = 42;

    // Write the integer to the string
    sprintf(buffer, "The number is: %d", number);

    // Print the formatted string
    printf("%s\n", buffer);

    return 0;
}

Output:

The number is: 42

Writing a Floating-point Number to a String

This example shows how to use sprintf() to write a floating-point number to a string.

Example

#include <stdio.h>

int main() {
    char buffer[50];
    float number = 3.14;

    // Write the floating-point number to the string
    sprintf(buffer, "The number is: %f", number);

    // Print the formatted string
    printf("%s\n", buffer);

    return 0;
}

Output:

The number is: 3.140000

Writing a String to Another String

This example demonstrates how to use sprintf() to write a string to another string.

Example

#include <stdio.h>

int main() {
    char buffer[50];
    char name[] = "Ramesh Fadatare";

    // Write the string to another string
    sprintf(buffer, "Hello, %s!", name);

    // Print the formatted string
    printf("%s\n", buffer);

    return 0;
}

Output:

Hello, Ramesh Fadatare!

Real-World Use Case

Formatting and Storing Multiple Values

In real-world applications, the sprintf() function can be used to format and store multiple values in a single string for further processing or storage.

Example

#include <stdio.h>

int main() {
    char buffer[100];
    char name[] = "Ramesh Fadatare";
    int age = 25;
    float salary = 50000.50;

    // Write multiple values to the string
    sprintf(buffer, "Name: %s, Age: %d, Salary: %.2f", name, age, salary);

    // Print the formatted string
    printf("%s\n", buffer);

    return 0;
}

Output:

Name: Ramesh Fadatare, Age: 25, Salary: 50000.50

Conclusion

The sprintf() function in C is a standard library function that writes formatted output to a string. It is part of the C standard library (stdio.h) and is used to format strings without printing them to the console.

Comments