I pulled out the penultimate digit of the floating-point number, and to get such a digit, I wrote down the float number into the array via sprintf , and pulled out the penultimate digit, and translated it through atoi . But I recorded a two-digit number, so I had to do div 10 . Why atoi took the extra figure on the right, and not translated only one number, but two.

 for ( int i = 0; i < N; i++) { x = x + 0.99; memset(myString, 0, 30); t = tan(2.0 * x); sprintf(myString, "%f", t); f_x[i] = atoi(&myString[strlen(myString) - 2])/10; // точность округления 8 знаков и был взят предпоследний //f_x[i] = myString[strlen(myString) - 2]-'0'; еще способ } 
  • So why did you think that the result should be unambiguous? You fed atoi a two-digit entry. And got it. What is an "extra digit to the right"? Why suddenly atoi should have considered it "superfluous"? - AnT 4:39 pm

1 answer 1

atoi works with a string, not a character.

 int atoi(const char *str) 

The line starts at the specified address and ends with a null character.
So everything as spelled out.

You can take exactly the symbol myString[strlen(myString) - 2] , and subtract from it the code zero 0x30 - here will be the desired value.

  • , is it possible to specify a range for atoi? - Elvin
  • You can copy part of a string to a new one ( strncpy ) and apply atoi. For one character, doing this is ridiculous. - MBo pm