1

I was reading one of the application of "Unions" of creating mixed data types. Example

typedef union {
int x;
float y;
}mix;
mix arr[100];

Each element of array arr[100] can store either a int or a float type value. But suppose I want to take a input from user and store it in arr[ ] and I won't know that either user is going to enter a floator an int value. So I don't know which statement from the following I should choose to store that input from user.

1.scanf ("%d",&arr[0].x);

OR

2.scanf ("%f",&arr[0].y);

And similar problem arises when I want to print that value.

I know I can solve this problem using a "tag field". So I can do it as

#include <stdio.h>
typedef union {
int x;
float y;
}mix;
mix arr[100];

int main ()
{
int k; // tag field
puts("Mention 1 if you want to enter integer and 0 if you want to enter float value");
scanf ("%d",&k);
if (k)
{
scanf ("%d",&arr[0].x);
printf ("%d is the entered integer value\n",arr[0].x);
}
else
{
scanf ("%f",&arr[0].y);
printf ("%f is the entered float value\n",arr[0].y);
}

}

But here user is telling the type of data he is gonna enter(with the help of 0 or 1). I want to know: Is there any way in C language that compiler can automatically detect the type of data user is entering and according run either 1st or 2nd scanf statement without help of user? OR Is there any predefined function present in library for doing that?

Also tell if you have any another intresting way of doing this program.

timrau
  • 22,578
  • 4
  • 51
  • 64
Avi
  • 325
  • 1
  • 11
  • 1
    Read text into a _string_ and then parse it as `int`, and then as `float`: 4 outcomes possible. both, float, int, neither. If both, I'd prefer `int`. – chux - Reinstate Monica Feb 14 '19 at 05:46
  • Would you classify input text `"4294967296\n"` as an out-of-range-`int` or a `float`? What about `"2147483647.0\n"`. Is that an exact `int` or imprecise `float`? – chux - Reinstate Monica Feb 14 '19 at 05:55
  • @chux Thanks for your line "Read text into a _string_ and then parse it as `int`, and then as `float`". This has clicked an idea in me. – Avi Feb 14 '19 at 06:50
  • @chux "Would you classify input text `"4294967296\n"` as an out-of-range-`int` or a `float`?" **:** I was just trying this program for small values (4 to 5 digits), So we can consider it as out of range. – Avi Feb 14 '19 at 06:54
  • @chux " What about `"2147483647.0\n"`. Is that an exact `int` or imprecise `float`? " : It should be considered as float. – Avi Feb 14 '19 at 06:56
  • And how would your program later know which type to use? – the busybee Aug 15 '22 at 09:25

2 Answers2

1

This might help you I guess...

float num;
printf("Enter number\n");
scanf("%f",&num);
if((num - (int)num)== 0) //(int)num : Type casting
    printf("Entered number is of int type\n");
else
    printf("Entered number is of float type\n");
  • It works...thanks. But only problem with your solution is that it treats a number like `5.00` as integer but it should be a float type. But anyway your answer helped me. One upvote for that. – Avi Feb 14 '19 at 07:25
1

Determining Data Type Of User Entered Value

Read as a line fgets()and the parse with strtol(), strtof().

Untested code, read comments.

puts("Mention 1 if you want to enter integer and 0 if you want to enter float value");
char buffer[100];
if (fgets(buffer, sizeof buffer, stdin)) {
  // OK we have an input line of text now as a string
  buffer[strcspn(buffer, "\n")] = '\0'; // lop off potential \n

  char *endptr;
  errno = 0;
  long lval = strtol(buffer, &endptr);
  // If conversion occurred and no text at the end ...
  if (endptr > buffer && *endptr == '\0') {
    // An integer type!
    if (errno == ERANGE) puts("Integer value out of range");
    printf("Integer value: %ld\n", lval);
  } else {
    errno = 0;
    float = strtof(buffer, &endptr);
    if (endptr > buffer && *endptr == '\0') {
      // A float
      if (errno == ERANGE) puts("float value out of range");
      printf("float value: %g\n", f);
    } else
      puts("Non-numeric input");
    }
  }

For a int mystrtoi(const char *str), see ref code.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256