C Programming

Sprintf in C

In this guide, we will discuss how to use the sprintf function in C programs. The sprintf function is used to write a formatted string to a character string buffer.

Let us discuss how to use this function and illustrate with various examples.

Basic Usage

The sprintf function is defined in the stdio.h header; it accepts a format string and writers to a string buffer. Think of it as the printf function but instead of writing to the console, the sprint sends output to a formatted string.

The basic syntax for the sprintf function is as:

int sprintf(char* buff, const char* fmt,);

Sprintf Parameters

The following are the parameters accepted by the sprintf function.

  1. buff – This points to the string buffer to which the result is written.
  2. fmt – This is a pointer to a string written to the file stream.
  3. … – This represents other parameters such as width, precision, length, and a specifier. To learn more about the family of printf functions, use the command man printf.

The sprint() function writes the data to the string pointed by the buff.

Return Value

The sprintf function has an integer return type. Upon success, the function returns the number of characters written to the buffer, excluding the null terminating character.

On failure, the function returns a negative integer value.

Example 1

The following is an example of how to use the sprint() function in C.

#include
#include

int main() {
    int age;
    char name[30], country[30], details[100];

    printf("Enter your name: ");
    fgets(name, 30, stdin);

    printf("Enter your country: ");
    fgets(country, 30, stdin);

    printf("Enter your age: ");
    scanf("%d", &age);

    sprintf(details, "Name: %s Age: %d  Country: %s ", name, age, country);
    printf("details: \n\n %s", details);
}

In the above example, we create a formatted string containing values such as name, country and the age. Using various specifiers, we can inject variables into the string.

These specifiers include:

  1. %c – used for character variables.
  2. %d or %i – signed integer
  3. %f – Floating point
  4. %s – string of characters.
  5. %p – pointer address

Example 2

We can also use the sprint() function to convert an integer or float to a string type. Take a look at the example program below:

#include
#include

int main() {
    char after[10];
    float var = 3.14159;
    // var is a float before sprintf
    printf("Before sprintf %f \n", var);

    // now its a string
    sprintf(after, "%f", var);
    printf("After sprintf %s \n", after);
}

In the example above, we use the sprint() function to convert a floating pointing value to a string.

Closing

In this tutorial we discussed how to use the sprint() function to redirect output to a formatted string. The sprint() function is useful in specific conditions. Check out the manual to learn more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list