C - Console Input Float Input


When reading floating-point values formatted using scanf_s(), you can enter the values in a variety of forms.


#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>

int main(void)
  float fp1 = 0.0f;
  float fp2 = 0.0f;
  float fp3 = 0.0f;
  int value_count = 0;
  printf("Enter: 3.14.314E1.0314e+02\n");

  value_count = scanf_s("%f %f %f", &fp1, &fp2, &fp3);

  printf("Number of values read = %d\n", value_count);
  printf("fp1 = %f  fp2 = %f  fp3 = %f\n", fp1, fp2, fp3);
  return 0;/*w w  w  .  j  a  v  a2s. c  om*/


This example demonstrates three different ways of entering the same value.

  • the first is a straightforward decimal value,
  • the second has an exponent value defined by the E1 that indicates the value is to be multiplied by 10,
  • the third has an exponent value of e+02 and therefore the mantissa is to be multiplied by 100.