C - Console Input Float Input

Introduction

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

Demo

#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");

  printf("Input:\n");
  value_count = scanf_s("%f %f %f", &fp1, &fp2, &fp3);

  printf("\nOutput:\n");
  printf("Number of values read = %d\n", value_count);
  printf("fp1 = %f  fp2 = %f  fp3 = %f\n", fp1, fp2, fp3);
  return 0;//from   w w  w.ja  va  2 s  .c  o m
}

Result

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.