0
#include "stdio.h"
#include "stdlib.h";

int main()
{
    char x[] = "234";
    int y;
    
    y = atoi(x);
    printf("\nCONVERTED TO INT: %d",y);
}

In the above code how can I find or check the datatype of variable y

jaswanth
  • 29
  • 1
  • 1
  • 8
  • Does this answer your question? [How do I check if a variable is of a certain type (compare two types) in C?](https://stackoverflow.com/questions/6280055/how-do-i-check-if-a-variable-is-of-a-certain-type-compare-two-types-in-c) – zr0gravity7 Dec 27 '21 at 07:44
  • 1
    One of the things that makes C so blisteringly fast is that much of the responsibility for providing proper code falls where it should, on the programmer. When you write code, it is your responsibility to ensure only type-compatible types are use, and to ensure adequate storage exists -- are two prime examples. Now there are compile-time checks made that do provide type compatibility checks and bounds checking, but you must enable and then read the warnings generated by the compiler. Compile-time checks do not add runtime-overhead. – David C. Rankin Dec 27 '21 at 09:39

2 Answers2

1

As commented something might work with help of c11 generic and GNU extensions in determining the data type variable.

#define IS_COMPATIBLE(x, T) _Generic((x), T:1, default: 0) It is used to determine the datatype of T by choosing the data type which match with x and replace with 1 on success otherwise default 0.

_Generic(x) is keyword acts as a switch that chooses operation based upon data type of argument passed to it.

And typeof(y) is GNU extension.

Note: Only variable modified(dynamic) types can be determined at runtime by typeof() extension.

#define IS_COMPATIBLE(x, T) _Generic((x), T:1, default: 0)
int main()
{
    char x[] = "234";
    int y;
    int int_var;
    y = atoi(x);
    if(IS_COMPATIBLE(int_var,typeof(y))){
        printf("The underlying data type of y is INT");
    }
    printf("\nCONVERTED TO INT: %d",y); 
}
mohammed yaqub
  • 100
  • 1
  • 8
  • 1
    `typeof` does not determine the type at runtime unless its operand is variably modified. For operands that are not variably modified, the compiler simply knows the type of the operand and uses that as the result of `typeof`. – Eric Postpischil Dec 27 '21 at 12:12
0

C is a quite strongly typed language. Any variable (or function parameter) that is visible has a data type defined by the near-by source code.

You have defined the variable in your source:

int y;

So y is an int.

You can find the data type of any variable by looking at its declaration or definition.

the busybee
  • 10,755
  • 3
  • 13
  • 30