Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to Convert Integer into String in C Language
How to Convert Integer into String in C Language
1.int/float to string/array:

C language provides several standard library functions, which can convert any type of numbers (integer, long integer, floating point, etc. ) in a string. The method and description of each function are listed below.

● itoa () (): converts integer values into strings.

● ltoa (): converts a long integer value into a string.

● ultoa (): converts an unsigned long integer value into a string.

● gcvt (): Convert floating-point numbers into strings and round them off.

● ecvt (): converts a double-precision floating-point value into a string, and the conversion result does not contain decimal points.

● fcvt (): the specified number of digits is the conversion precision, and the rest are the same as ecvt ().

In addition, sprintf series functions can be used to convert numbers into strings, which is slower than itoa () series functions.

2. String/array to integer/floating point type

C/C++ language provides several standard library functions, which can convert a string into any type (integer, long integer, floating point, etc. ).

● atof (): Converts a string into a double-precision floating-point value.

● atoi (): Convert a string into an integer value.

● atol (): Convert a string into a long integer value.

● strtod (): Converts a string into a double-precision floating-point value and reports all remaining digits that cannot be converted.

● strtol (): Converts a string into a long integer value and reports all remaining digits that cannot be converted.

●strtool (): Converts a string into an unsigned long integer and reports all remaining digits that cannot be converted.

C language implementation:

# Contains?

# Contains?

Invalid? itoa (int n,char s[]);

//atoi function: convert s into an integer.

int main(void)

{

int n;

char s[ 100];

Printf ("Enter n: \ n");

scanf("%d ",& ampn);

printf(" the string:\ n ");

itoa (n,s);

Returns 0;

}

void itoa (int n,char s[])

{

int i,j,sign

if((sign = n)& lt; 0)// record symbol

n =-n; //Make n a positive number

I = 0;

Do {

s[i++]= n % 10+' 0 '; //Next number

}

while ((n/= 10)>0); //Delete this number

if(sign & lt; 0)

s[i++]= '-';

s[I]= ' \ 0 ';

for(j = I; j & gt=0; The number generated by j-)// is in reverse order, so it should be output in reverse order.

printf("%c ",s[j]);

}

C language is a general computer programming language, which is widely used. The design goal of C language is to provide a programming language that can compile and process low-level memory in a simple way, generate a small amount of machine code and run without any support of running environment.

Although C language provides many low-level processing functions, it still maintains good cross-platform characteristics. C language programs written in standard specifications can be compiled on many computer platforms, even including some embedded processors (single chip microcomputer or MCU) and supercomputers.

In 1980s, in order to avoid the differences of C language grammar used by different developers, the American National Bureau of Standards formulated a complete set of American national standard grammar of C language, called ANSI C, as the original standard of C language. At present, the standard C 1 1 released by the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) on February 8, 2000 is the third official standard of C language, and it is also the latest standard of C language, which better supports Chinese character function names and.

C# is object-oriented and runs on. Net? Framework-based high-level programming language. It plans to make its debut at the Microsoft Professional Developers Forum (PDC). C# is the latest achievement of Microsoft researcher anders hejlsberg. C# looks surprisingly similar to Java; Including single inheritance, interface, almost the same syntax as Java, compiling into intermediate code and then running. However, there are obvious differences between C# and Java. It draws lessons from a feature of Delphi and integrates directly with COM (Component Object Model). It is the protagonist of Microsoft. NET windows network framework.