Chuyển int sang string c++

     
“Strings data types are everywhere in modern programming, from low-level programming languages to lớn high-level languages that make a great effort lớn abstract the fundamental concepts. Therefore, you will find yourself working with strings in any programming language more times than not.

Bạn đang xem: Chuyển int sang string c++

For that purpose, we will discuss quick & very beginner-friendly methods of converting an integer into a string in the C programming language.”

Method 1 – Convert Int to String Using the Sprintf Function

The Sprintf function is one of the functions you can use khổng lồ convert an integer value into a string. As the name suggests, the function will take any value & print it into a string.

It is very similar khổng lồ the printf function. But instead of printing the value into the standard out, the function returns a formatted string which we can store in a variable and use later.

The function syntax is as shown below:


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

The function accepts three main parameters:

str – this specifies a pointer khổng lồ a char data type.format – the format parameter allows you khổng lồ specify the type of output đầu ra with a placeholder.args – this parameter specifies the integer values in which lớn convert into a string.

You will notice that the function returns an int type. This is because it returns a formatted string which is discarded, & a value of -1 if an error occurs.

Let us see this function in action.


#include intmain() int var =100;charint_str<20>;sprintf(int_str, "%d", var);printf("Var: %s", int_str);return0;

In the code above, we start by importing the necessary header files. For the sprint() function, we require the standard input and output header file only.

We then xuất hiện the main function & declare two variables. The first is the integer value that we wish to lớn convert khổng lồ a string.

The next variable is the character array that we will use lớn store the string value once we convert the int into a string.

We then use the sprint function & pass the char type, the format, and the int as the parameters.

Xem thêm: Cách Làm Chuồng Chó Bằng Thùng Giấy Đơn Giản Và Tiết Kiệm, Tự Làm Chuồng Cho Chó Từ Thùng Giấy Dễ Hay Khó

Finally, we can print the resulting string using the printf function. The resulting output is a shown:


$ gcc to_string.c$ ./a.outVar: 100

Method 2 – Convert Int khổng lồ String With itoa() Function (non-standard)

There is another non-standard function in C that you can use lớn convert an int to a string. It is a simple type-casting function. It first appeared in the C Programming Language book.

However, as the book states, this function is non-standard và does not handle negative integers very well.

Since it is non-standard, attempting khổng lồ compile the function will heavily depend on your OS and if the function is available.

However, we are developers, và experimenting is in our nature.

The following syntax shows how the itoa() function works.


char* itoa(int num, char * buffer, int base)

The function takes three main parameters:

num – this parameter refers to the int value that you wish khổng lồ convert to a string.buffer – the buffer parameter is a pointer lớn a char data type lớn hold the converted string.base – refers to lớn the conversion base.

The function then returns a string of the specified integer.

The code below illustrates how to lớn use the itoa() function lớn convert an int to lớn a string.


#include #include #include intmain() int var =100;charint_string<20>;itoa(var, int_string, 10);printf("Var: %s ", int_string);return0;

Here, we specify the conversion of the int to lớn base 10.

Xem thêm: Hướng Dẫn Cách Treo 3 Chữ Phúc Lộc Thọ Chuẩn Phong Thủy Cát Tường

Closing

In this article, we covered two main methods of converting an Int lớn a string in C. It is good lớn stick lớn the Sprintf function as it’s a standard function & can be exported across systems.

Thanks for reading, and Happy coding!!


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 khổng lồ Programming. My dream is to chia sẻ my knowledge with the world and help out fellow geeks. Follow my nội dung by subscribing to giaoducphanthiet.edu.vn mailing list


View all posts
RELATED LINUX HINT POSTS

Linux Hint LLC, editor
giaoducphanthiet.edu.vn1309 S Mary Ave Suite 210, Sunnyvale, CA 94087
Privacy Policy và Terms of Use