Search code examples
c++ctypesbuilt-in-types

What is the type of a builtin datatype in C and C++?


When we write int a;, it doesn't mean that we are creating an object of class int.

  1. What does it mean?
  2. What is the type of the datatype int in C and C++?
  3. Which header file shows what it is?

Solution

  • When we write int a;, it doesn't mean that we are creating an object of class int.

    int a; does indeed create an object in C++. It's an object of type int with indeterminate value if it has automatic storage duration; or with value 0, if it has static storage duration. But there is no "class int" because int is not a class type.

    int is a:

    • integral type;
    • signed type;
    • arithmetic type;
    • fundamental type;
    • scalar type;
    • standard layout type;
    • trivially copyable type;
    • POD type;
    • trivial type;

    Seems like you got a bit confused in your previous question :)

    In int x = 12;, you are creating an object of type int that is named x and has value 12.

    The idea of object in C++ is not the same as in most other languages, and most certainly is not the same as is commonly used in object-oriented programming circles. An object in C++ is a region of storage.

    If something has a type, it's either an object, a reference, or a function.

    Which header file shows what it is?

    The language simply requires that the type int has to exist and have certain characteristics (like being integral and having a sign). All compilers I know of simply treat all the builtin types specially and that's why you won't find a definition for them in the standard library headers. In fact, they can't provide a definition for them in any header using C++, because the language doesn't provide any means of defining fundamental types. They could only either:

    • define it as a compound type (which would be wrong); or
    • define it using compiler-specific extensions.

    The builtin types are effectively magic.