
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Implement your own itoa() in C
In this section we will see how to convert an integer number to a string.
The logic is very simple. Here we will use the sprintf() function. This function is used to print some value or line into a string, but not in the console. This is the only difference between printf() and sprintf(). Here the first argument is the string buffer. where we want to save our data.
Input: User will put some numeric value say 42
Output: This program will return the string equivalent result of that number like “42”
Algorithm:
Step 1: Take a number as argument Step 2: Create an empty string buffer to store result Step 3: Use sprintf() to convert number to string Step 4: End
Example Code
#include<stdio.h> char* my_itoa(int number) { char str[20]; //create an empty string to store number sprintf(str, "%d", number); //make the number into string using sprintf function return str; } main() { int number; printf("Enter a number: "); scanf("%d", &number); printf("You have entered: %s", my_itoa(number)); }
Output
Enter a number: 56 You have entered: 56
- Related Articles
- How will implement Your Own sizeof in C
- Implement your own sizeof operator using C++
- Write your own memcpy() in C
- Write your own atoi() in C++
- Write your own memcpy() and memmove() in C++
- Print with your own font using C#
- Write your own strcmp that ignores cases in C++
- How to write your own header file in C?\n
- Build Your Own Botnet
- Can you build your own sundial?
- Making your own custom filter tags in Django
- Describe the ‘Greenhouse Effect’ in your own words.
- Can you own Crypto in your Roth IRA
- How to create your own helper class in Java?
- Describe the ‘Green House Effect’ in your own words.

Advertisements