-1

My friend attend an interview , in that he was failed to ans a question. The question was like this Program to find the datatype of a given Input. He asked me and I was able to crack up to this,

include<stdio.h> 
main() 
{ 
  char n; 
  printf("\nEnter a character: "); 
  scanf("%c",&n); 
  if(isdigit(n)) 
     printf("\nInteger"); 
  else 
     printf("\nCharacter"); 
}

But it will tell only integer or charector. But the actual question in interview is user will enter only numeric input and program has to tell weather it is int, float or double.How to do this please help me in this.

Leeor
  • 19,260
  • 5
  • 56
  • 87
Raghavendra
  • 259
  • 4
  • 12
  • 23
  • 6
    How is this Java? Anyway, maybe http://stackoverflow.com/questions/78474/determine-if-a-string-is-an-integer-or-a-float-in-ansi-c is a way to your answer. – srm Apr 29 '14 at 12:55
  • there are some ways e.g. you can use regex for example, to check float & char [use this](http://codepad.org/2DjjZKZI). – Grijesh Chauhan Apr 29 '14 at 13:03
  • 1
    In that context, what's the difference between a float and a double? If the user enters '5.3', the value cannot be represented accurately as either a float, or a double, or an integer, so what do you do with that? Handy interview hint: the interviewer cares more that you ask questions like these than that you can write the code (well, actually, they assume you can write the code, but you'd better also ask this type of question!) – William Pursell Apr 29 '14 at 13:07

1 Answers1

0

well it could be like this: When it comes to the float and int well you can just use an if(scanf("%d", &number)) and the same u can do for double or floats. u will get only true in one of them. If u get it in the first time then it is int otherweise it is float or double. Then just make a different sizeof() comparison and here u have it.

jumetaj
  • 11
  • 3
  • No, that won't work. Example: `sscanf("12.4", "%d", &number)` will return `1` and `number` will be assigned the value `12`. – Klas Lindbäck Apr 29 '14 at 13:45