Proud Member
Proud Member

Reputation: 40496

How to check what type a variable is?

I want to know if someVariable is of type int or float. How would I do the check?

Edit: In code at runtime! In the IDE that would be simple. I want to know if there is some kind of typeof operator to see if you're dealing with int, float, long, char, unsigned char, long long or whatever.

Upvotes: 2

Views: 3583

Answers (3)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215251

if ((1?1:var)/2) {
    /* it's floating point */
} else {
    /* it's an integer */
}

Upvotes: 10

Moszi
Moszi

Reputation: 3236

Since sizeof(float) == sizeof(int) (depending on platform, but on i386 and current iPhone this is valid) - i believe you can't decide during runtime whether your variable is int or float.
EDIT: Greg is right in the comment. This check doesn't really make sense.

Upvotes: 0

Ben Zotto
Ben Zotto

Reputation: 71008

Is someVariable an NSNumber? If it's a primitive C type (an actual int or float), then you'll already know because it can be declared as only one of those things, and the compiler (and Xcode) will enforce that. As another commenter noted, Command-Double-Click the name to find the declaration in Xcode.

Upvotes: 2

Related Questions