how to get the data type of a veriable

S

sunil.reloaded

Consider that a variable is defined as int in the beginning of the
program.

Say, int ver1;

It is a very lengthy program hence i want to know the type of the
varable ver1 in the middle of the program(Assume in some function).

Can you tell me how do i get the type of the variable that was defined
previously???


For example,

int ver1;

main()
{
.........................
.................
}
...........
.....
fun()
{
............

If ( ver1 is an integer )
{
do .............
}
else
{
do .............
}

}

Thanx 4 any help on this in advance.
 
A

Alexei A. Frounze

Consider that a variable is defined as int in the beginning of the
program.

Say, int ver1;

It is a very lengthy program hence i want to know the type of the
varable ver1 in the middle of the program(Assume in some function).

Can you tell me how do i get the type of the variable that was defined
previously???

It's not Delphi or C++. C won't tell you the time. You must watch for the
types yourself. If you can't remember the type, put it into the variable's
name:
int ver1_int;

If ( ver1 is an integer )
{
do .............
}
else
{
do .............
}

But you may use macro:

#define VER1_IS_INT 1

#if VER1_IS_INT
int ver1;
#else
char ver1;
#endif

{
#if VER1_IS_INT
do .............
#else
do .............
#endif
}

Alex
 
X

Xie Yubo

Consider that a variable is defined as int in the beginning of the
program.

Say, int ver1;

It is a very lengthy program hence i want to know the type of the
varable ver1 in the middle of the program(Assume in some function).

Can you tell me how do i get the type of the variable that was defined
previously???


For example,

int ver1;

main()
{
........................
................
}
..........
....
fun()
{
...........

If ( ver1 is an integer )
{
do .............
}
else
{
do .............
}

}

Thanx 4 any help on this in advance.

I think, you cann't get the type of the variable. It is not supported by
the C language self.

Is it very important for you? If it is, I think you can do it in another
way.
For example, you could declare a struct:

enum valType { Integer, Float, Double };
struct Val
{
union{
int ival;
float fval;
double dval;
};
enum valType type;
};

then you can do it like this:

struct Val i;
i.ival = ...;
i.type = Integer;

......
if(i.type == Integer)
{
do....
}
 
E

Eric Sosman

Consider that a variable is defined as int in the beginning of the
program.

Say, int ver1;

It is a very lengthy program hence i want to know the type of the
varable ver1 in the middle of the program(Assume in some function).

Can you tell me how do i get the type of the variable that was defined
previously???

C has no notion of "type" as a run-time object. There
is no `type_t', nothing that expresses or captures the type
of a variable or expression. Types in C are compile-time
constructs and have no existence as such at run-time. Some
languages have "dynamic typing" but C does not: it is a
"statically typed" language, and has no way to manipulate
or query the types of its expressions.

C's only answers to your question are "Remember it" or
"Find the variable's declaration and look at it."
 
A

akarl

Consider that a variable is defined as int in the beginning of the
program.

Say, int ver1;

It is a very lengthy program hence i want to know the type of the
varable ver1 in the middle of the program(Assume in some function).

Can you tell me how do i get the type of the variable that was defined
previously???


For example,

int ver1;

main()
{
........................
................
}
..........
....
fun()
{
...........

If ( ver1 is an integer )
{
do .............
}
else
{
do .............
}

}

I really don't understand what you want to achieve. You reason that
because the variable ver1 is declared far away from the function fun you
want to take different actions depending on the type of it. Why?
 
M

Martin Ambuhl

Consider that a variable is defined as int in the beginning of the
program.

Say, int ver1;

It is a very lengthy program hence i want to know the type of the
varable ver1 in the middle of the program(Assume in some function).

Can you tell me how do i get the type of the variable that was defined
previously???

Use your editor and look at the declaration.
 
K

Krzysztof Olczyk

Uzytkownik said:
Consider that a variable is defined as int in the beginning of the
program.

Say, int ver1;

It is a very lengthy program hence i want to know the type of the
varable ver1 in the middle of the program(Assume in some function).

Can you tell me how do i get the type of the variable that was defined
previously???

Why do you bother to do so?
Is it too hard to scroll up in whatever your editor is?
Or to use "search" function?

And that condition for checking if simple variable
is of certain type and forking flow of the program
depending on the type of variable that is well-known
to be of certain type looks very strange

Certain languages like object pascal (and c++ to some degree)
have RTTI (run-time type info) but it has applications in oop
and when you deal with polymorphism.

But not in C.

I think in your problem just scrolling up or defining the variable
more locally would do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top