Could I retrieve the datatype from a variable in C?

后端 未结 5 1829
灰色年华
灰色年华 2020-12-11 05:42

Has a way to get the datatype in C?

For example:

int foo;

if (foo is int)
{
    // do something
}

or something like:

if (typeof(foo         


        
5条回答
  •  死守一世寂寞
    2020-12-11 06:26

    Since C11, you can do that with _Generic:

    if (_Generic(foo, int: 1, default: 0)) // if(typeof(foo)==int)
    {
        // do something
    }
    

提交回复
热议问题