How to get the "data type" of an "unknown variable" in "C Language" ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
you cannot do it.
you can use variable arguments like printf does,
var_args
but what you suggest sounds silly.
if they are different types what sort of function is it?
a function should be a simple, cohesive operation operating
on a strict range of data.
I'm more of a C++ that C programmer but maybe you can mange it with void pointers or with a structure that is a union of the different types that you want to manage and (of course) an indicator of the actual type being passed in.
i don't think i understand your problem very well, you say that the program is gonna ask for input and then you want to be able to recognize weather the input is an integer, float, character, string, etc?
in that case you can take the input into a character string and check each character, if it finds that all characters are numbers, then you can assume its an integer, if it finds a period then its a float/double and if it finds a combination then its a string.
again, not sure i understood your problem. hope that helps.
in that case you can take the input into a character string and check each character, if it finds that all characters are numbers, then you can assume its an integer, if it finds a period then its a float/double and if it finds a combination then its a string.
A good way to do this is to attempt parsing from more organized to less organized. For example:
Code:
unsigned int -> int -> double -> char*
This can be done with strtoul, etc. by checking the *endptr to make sure it's either 0x00 or whitespace. If all numerical parsing fails, just take it as a string.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.