How to Convert an Int to String in C
Updated onbyAlan Morel
Table of Contents
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"
:
CLIKE#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:
BASH12345
The sprintf()
function has this syntax:
CLIKEint 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.
Leave us a message!
×
- Managing PHP Dependencies with Composer
- Getting Started with Svelte
- Git Tutorial: Learn how to use Version Control
- How to deploy a .NET app using Docker
- How to build a Discord bot using TypeScript
- How to deploy a Node app using Docker
- Using Puppeteer and Jest for End-to-End Testing
- How to Scrape the Web using Node.js and Puppeteer
- Getting Started with Handlebars.js
- Getting User Location using JavaScript's Geolocation API
- Creating a Twitter bot with Node.js
- Building a Real-Time Note-Taking App with Vue and Firebase