Namely, number or not?

Closed due to the fact that the essence of the issue is incomprehensible by the participants of sercxjo , Vladimir Glinskikh , LEQADA , cyadvert , Aslan Kussein 20 Oct '15 at 3:12 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    In what context is it needed? - stanislav

3 answers 3

Usually the author of the code knows what type of variable is being used. If the variable is used erroneously, errors either appear at compile time or at run time.

In more complex cases, additional storage measures are required. For example, there is a function

void func(void *p) { char x = *(char *)p; } 

It is good time to say which variable p points to will not work, it can be anything, for example, double:

 double d; func((void *)&d); 

Therefore, if necessary, lead structure

 struct Z { // тип // переменная } 

and work with her.

  • The problem is this: The user enters information via scanf () If there is 0, the check can be done, but if there is not a number, I don’t know how to do it. - systemiv
  • You create a variable of type char, then use the function atoi to translate it into an integer (if there is one at all). - 3JIoi_Hy6
  • 2
    <i> The user enters information through scanf () If there is 0, the check can be done, but if there is not a number, </ i> Very simple. First read the string. Then disassemble it manually. You can f-tsie sscanf , but you can atoi . Also note that scanf and its derivatives return the number of successfully converted items. - gecube
  • 2
    To @byte: Your first example of C code gives a warning ( did you really want to extract the low byte of the pointer?). The second example of the code is error - well, in fact, how to convert a 64-bit floating-point number into a machine address ??? Usually the compiler does not have telepathic abilities and he cannot understand what the programmer really wants. - avp 10:21 pm
  • @avp Thanks, corrected. - stanislav

typeid returns an object of type typeinfo from which type information can be extracted, but this is C ++.

    1. scanf sends the entered data to the array of charms.
    2. using the loop and the isdigit function (from ctype.h) check each element of the array to see if it is a digit. You can enter another condition - a separator (full stop or comma) for real numbers.
    3. If it is not a digit or a separator, interrupt the cycle and say that it is not a number entered. If all the elements of the array are OK and the null-terminating element is reached, we do what we need with it: translate to int, float, etc. on your own.