How to Convert an Int to String in C

Updated onbyAlan Morel
How to Convert an Int to String in C

Table of Contents

  1. sprintf()

In this post, we'll learn how to convert an integer to a string in C.

sprintf()

To convert an integer to a string, you can use the sprintf() function in C.

Here's how to convert the int 12345 to the string "12345":

#include <stdio.h>

int main() {
    int number = 12345;
    char string[5];

    sprintf(string, "%d", number);

    printf("%s\n", string);
}

The output will be the number converted to a string:

12345

The sprintf() function has this syntax:

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

It takes any value and prints it to a string. It is similar to the printf() function except it doesn't print to the console but instead returns the string.

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.