Home > Enterprise >  Does C have a concept of primitive types and if so what are they?
Does C have a concept of primitive types and if so what are they?

Time:02-04

For example java has 8 primitive types as documented by Oracle.

boolean, byte, short, char, int, long, float, double

C appears to have many, many types, but what if any are considered primitive types. Please list them for the answer.

I tried to find a solid reference similar to Oracle but for C, but could not.

Wikipedia maintains an obtuse list of "main types", but I'm not sure if they are relevant.

W3Schools simplifies this and lists "basic types", but I'm also not sure if this is relevant.

CodePudding user response:

The answer is no, and for 2 reasons.

  1. The notion of primitive types in Java exists by opposition to object types. This has no sense in C which is not an object language.
  2. C intends to be as efficient as possible on any architecture, from 16 or even 8 bits microcontrollers to 64 bits platforms. For that reason the int type is generally chosen as the natural type for the architecture provided it contains at least 16 bits. On the other hand, Jave targets maximum portability and fixes the size of all its types.

So even if the list on Wikipedia looks large, it is (unfortunately...) accurate.

CodePudding user response:

I would say that C does have the concept of a "primitive" or "basic" type, but as we've seen (in the question, the comments, and the linked Wikipedia article) the list of these types is rather long, not nearly as succinct as you might think, or as it was back in K&R days.

My own definition of a primitive type in C is, probably unsatisfyingly, "anything you can build derived types like array-of, pointer-to, function-returning, and struct-containing out of".

I'm pretty sure the grammar in the C Standard has productions for basic types and derived types, but I don't have my copy handy to check.

The current list of C types looks so long and complicated because it contains, for software engineering and portability reasons, a large number of "aliases", each of which end up mapping to some other type in an implementation-dependent way. For example, int32_t is typically the same as either int or long int, and size_t is typically the same as either unsigned int or unsigned long int.

If you want to build a concise list, I think you start with:

  • void
  • bool
  • int of quite a few sizes (including char and wchar_t)
  • unsigned int of quite a few sizes
  • floating-point of several sizes

And then you've got a set of "symbolic" types like size_t and ptrdiff_t and intptr_t, which typically map to various of the int or unsigned int types in an implementation-defined way.

And then you've got a set of exact-size types like int8_t and int16_t and uint32_t, and they're all mapped in with the other integer types in an implementation-defined way.

You also have to be careful when thinking about the character types. Plain char might be either signed or unsigned, so you can think of there actually being three fundamental character types: char, signed char, and unsigned char (with either char and signed char, or char and unsigned char, being identical on any given platform). There's also a type wchar_t with its own implementation-defined mapping.

CodePudding user response:

C appears to have many, many types, but what if any are considered primitive types.

The Java term "primitive type" distinguishes from reference types. C has no direct analog of Java's reference types, so no need to draw such a distinction.

C does, however, define several categories of types, and among those, the basic types are a reasonably good analogue of Java's primitive types. This category comprises char, the signed and unsigned integer types, and the floating[-point] types. It is ultimately from these, the enumerated types, and type void that all other types are derived, including array types, structure and union types, pointer types, atomic types, and function types.

The basic types can include implementation-defined types, and therefore cannot be exhaustively listed, but those defined by the language spec are:

char

The standard signed integer types
signed char, short int, int, long int, long long int

The standard unsigned integer types
_Bool, unsigned char, unsigned short int, unsigned int, unsigned long int, unsigned long long int

The real floating types
float, double, long double

The complex types
float _Complex, double _Complex, long double _Complex

This is covered in section 6.2.5 of the language spec.

It should be noted that whereas Java specifies the the size and representation of its primitive types, C leaves some of those details of its basic types to implementations, placing constraints on their ranges of representable values and other properties without specifying exact details.

C appears to have many, many types

C has an unbounded number of types, as it provides for types to be derived from other types. The same applies to Java. Ignoring implementation-defined extension types, C has more than twice as many basic types as Java has primitive types, but that's still manageable, especially given the way they are organized.

However, C also has a mechanism for defining type aliases, and a whole family of standard type aliases, and these can make it appear that there are more types than really there are.

  • Related