Raghavendra
Raghavendra

Reputation: 279

Finding datatype of given Input

Today 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, how about for an inputs like float double. Can any one help me inthis. Here we can use any programming language.

Upvotes: 0

Views: 275

Answers (1)

barak manos
barak manos

Reputation: 30136

I believe that you (or your friend) may have possibly misinterpreted the term given input as keyboard-input, where in fact, the interviewer may have meant that, given a function which takes a generic-type input argument, find the specific type of that argument. Of course, if that is indeed the case, then the question is not suitable for C.

In C++, you can use the typeid keyword. For example:

void func(const Object& arg)
{
    cout<<typeid(arg).name();
}

In Java, you can call the getClass() method. For example:

void func(Object arg)
{
    System.out.print(arg.getClass());
}

Upvotes: 2

Related Questions