Aquarius_Girl
Aquarius_Girl

Reputation: 22936

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?

Upvotes: 1

Views: 373

Answers (5)

imaximchuk
imaximchuk

Reputation: 748

int is a built-in type, no header file defines it. What int a; means depends on where this line of code is located.

It can be

  • local variable
  • object field
  • global variable

Note that int a; is not a good way to define local variable. you should always initialize it, there are several ways to do so:


int a = 5; //value is 5
int a = int(); //default constructor, value is 0

Upvotes: -1

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234624

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.

Upvotes: 13

The int type is built-in inside the language and the compiler. On most implementations, it is some kind of machine word (fitting into the processor's registers), as efficiently handled by the processor. On my (Debian/Linux/AMD64) system, it is a 32 bits word, usually aligned to 4 bytes.

There is no header defining it. The compiler has an intimate knowledge about int. And it is not a class or some aggregate type, it is atomic in the sense of not being composed of smaller stuff.

All languages I know have predefined or built-in types (or names), which are specially known to the compiler. For example, in Ocaml, the Pervasives module is built-in.

Upvotes: 4

buddhabrot
buddhabrot

Reputation: 1586

When you type "int a;", you let the compiler know that any symbols "a" in a's scope have the datatype "int".

Upvotes: 1

fge
fge

Reputation: 121800

(as far as C is concerned)

int is a primitive type, to the extend that it is a language keyword. No header of any sort declares it.

However, there are a lot of typedefs defined in headers which do use primitive types as "backends" and which do require that you include the relevant headers (i.e uint32_t, uint16_t, etc etc).

Upvotes: 1

Related Questions