Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to best obtain a string from the standard input stream in C language and convert it to an int array?
How to best obtain a string from the standard input stream in C language and convert it to an int array?

#include? #include??

int?main?()?{? int?i;? char?buffer[256 ];? printf?("Enter?a?number:?");? fgets?(buffer,?256,?stdin);? i?=?atoi?(buffer);

printf? ("The?value?entered?is?%d.",?i);? system("pause");? return?0;?

}

Actually you The requirement is very simple. You can easily convert string to array by using atoi function. I can explain your doubts with the above short piece of code. You don’t understand what you’re asking me!

Execution result:

Enter a number: 233cyuyan

The value entered is 233.

The function description is as follows:

Header file: #include?

atoi()? function is used to convert a string into an integer (int), and its prototype is:

int ?atoi?(const?char?*?str);

Function description atoi()? function will scan the parameter ?str? string and skip the preceding whitespace characters (such as spaces, tab indentation, etc. , can be detected by the ? function), the conversion will not start until a number or a positive or negative sign is encountered, and the conversion will not end until a non-number or the end of the string ('\0') is encountered, and the result will be returned.

The return value returns the converted integer; if ?str? cannot be converted to ?int? or ?str? is an empty string, then ?0 will be returned