title: How to display result of an expression based on its data type in C
body: <p>The following program displays a double expression result. </p>
  
  <pre><code>int main()
  {
  double a=5,b=10, sum; //I have to declare the types as double so I get the result in double, What I want is some way to get a floating sum when floats are added and int when ints are added (like expect 5+10 to be 15 not 15.00000)
  
  sum = a+b;
  
  printf("the sum is %lf \n", sum);
  
  return 0; 
  }
  // the sum is 15.000000
  </code></pre>
  
  <p>How can I modify it to achieve something like below, without explicitly defining integer, float or double type? </p>
  
  <p>Edit
  Actually, I am saving the values of expression in a stack. So, currently I am using double type so the result of the expression is in double type, for example, 2+2 will be 4.0 but I want it to be as pure int 2, whereas if my expression result is a float then in this case I only want to see floating result like 4.2+1= 5.2</p>
  
  <p>I am expecting some logic with unions and some typedef type where I can define int as 1 and float as 2 and then check if the expression result is floating at that time I set the result type as float on the other hand if the expression result is int I set the type as int</p>
  
  <pre><code>// 5+5 = 10
  // 5.0+3.8 = 8.8 
  </code></pre>
  
  <p>Here is my code where I am taking input from the user and checking if the character is number if yes then checking if its a float. Right now I am using double type for the number (yylval) to make my expression work with floating inputs from the user but what I want is to have some union or something that can check if the input expression has float numbers then answer should be float otherwise for integers only it should have integer result. I haven't added my expression evaluator code here.     </p>
  
  <p>Updated code syntax</p>
  
  <pre><code>while (ch==' '||ch=='\t'||ch=='\n') ch=getchar();
  
  if (isdigit(ch)) {
      do {
          yytext[i]=ch;
          yylval=atoi(yytext);
          num = num * 10;
          num = num + yylval - '\0';
  
          ch=getchar();
      } while (isdigit(ch));
      yytext[i]=0;
  
  if (ch == '.')
  {
      ch=getchar();
      double weight;
      weight =1;
      while (isdigit(ch))
      {
          weight = weight / 10;
          double scaled;
          yytext[i]=ch;
          yylval=atoi(yytext);
  
          scaled = yylval  * weight;
          printf("scaled value in loop is %lf\n", scaled);
          num = num + scaled;
          ch=getchar();
      }
      yytext[i]=0;
  }
  yylval = num;
  
  return(NUMBER); 
  }
  </code></pre>