1

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.

  • 3
    I was about to say, "C has a nice, small list of basic types just like Java", but on reflection, [that Wikipedia article](https://en.wikipedia.org/wiki/C_data_types#Main_types), with all its complexity, looks pretty accurate. Note that, in practice, many of the types are basically synonyms for each other. For example, on the vast majority of conventional machines, it will be found that type `int` is identical to either type `int16_t` or `int32_t`, and type `unsigned int` is identical to `uint16_t` or `uint32_t`. – Steve Summit Feb 03 '23 at 17:29
  • What if we remove the unsigned and signed parts of that list ... what are we left with? –  Feb 03 '23 at 17:31
  • 2
    You're left with an incomplete list of types. – user3386109 Feb 03 '23 at 17:31
  • `int` and `unsigned int` are definitely different types. There are simplifications you might want to make, but removing the signedness distinction certainly isn't one of them! – Steve Summit Feb 03 '23 at 17:33
  • 3
    If we remove, we have - `char, short, int, long, float, double` - as the main types which can be further broken down into more types. Interestingly Java's 8 types contain all 6 of these but adds ... `boolean and byte`. –  Feb 03 '23 at 17:33
  • 1
    I am curious, what difference would the name mean? Why does it matter, "main", "basic" or "primitive" word is used? – Eugene Sh. Feb 03 '23 at 17:34
  • Do note that, as the table in the Wikipedia article, saying `long` or `long int` is just two ways of writing the same type. Same with `unsigned` and `unsigned int`, and `unsigned long` and `unsigned long int`. – Steve Summit Feb 03 '23 at 17:34
  • There are at least 5 sizes of integers: `char`, `short`, `int`, `long`, and `long long`. (They won't typically all have different sizes, and some implementations may have even more). There are at least three sizes of floating-point types: `float`, `double`, and `long double`, although again, there may be some overlap, i.e. `long double` may be the same as `double` on a given platform. – Steve Summit Feb 03 '23 at 17:36
  • But then there start being wrinkles: `char` is weird because it might be signed or unsigned, so some say that there are *three* fundamental character types: `char`, `signed char`, and `unsigned char` (with one pair or the other being identical on any given platform). And then there's `bool`. And then there's `intmax_t` and `intptr_t`. And then there's `size_t` and `ptrdiff_t`. And the list goes on... (Addendum: And then there's `wchar_t`...) – Steve Summit Feb 03 '23 at 17:38
  • 1
    [C added a boolean type in C99.](https://en.wikipedia.org/wiki/C_data_types#Boolean_type). And [Java's byte type](https://stackoverflow.com/questions/11239241/byte-type-is-weird) is equivalent to a `signed char` in C. – user3386109 Feb 03 '23 at 17:40
  • 1
    "primitive types" isn't C terminology. In Java, it provides a distinction to reference types. Flipping the question over is probably more meaningful/useful - C does not have Java like reference types. [Looking at this list of Java reference type characteristics](https://www.w3schools.com/java/java_data_types_non-prim.asp) _from a C perspective,_ I'd tend to respond with things like: this doesn't exist in the language, that's not quite the way things work, and that doesn't really matter. Whatever the terminology, there are drastic differences due to not having Java-esque reference types. – Avi Berger Feb 03 '23 at 18:00
  • I suspect that, if you looked "under the hood" at the source code of any C translator (compiler or interpreter), you would find the list of "basic types" you're looking for. In the part of the parser responsible for parsing type names like "`long int`" and "`unsigned char`", you'd find all sorts of rather intricate mappings, but with a decently-small set of "basic types" falling out at the end. – Steve Summit Feb 03 '23 at 18:07
  • @SteveSummit *some say that there are three fundamental character types* And that "some" are correct, because that "some" includes [the C standard](https://port70.net/~nsz/c/c11/n1570.html#6.2.5p15): "The three types char, signed char, and unsigned char are collectively called the character types." – Andrew Henle Feb 03 '23 at 19:47

4 Answers4

10

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Aren't `char`, `long`, `double`, etc...considered to be primitive data types? – GIZ Feb 03 '23 at 18:21
  • 3
    @GIZ [C does not have "primitive" types](https://port70.net/~nsz/c/c11/n1570.html#6.2.5). That word is not applicable to C. C just has "types". – Andrew Henle Feb 03 '23 at 19:49
  • If the list on Wikipedia looks long, it's in part because Wikipedia lists several alternative spellings for many of the types. It's not exactly inaccurate, but it *is* potentially deceiving. For example, `short` is not a different type from `signed short int`. – John Bollinger Feb 04 '23 at 15:28
  • @andrew - that standard is over 10 years old, do you have a link to a more recent version? –  Feb 04 '23 at 16:33
  • @bobbywang There's currently no more-recent approved standard. [C18](https://www.iso-9899.info/wiki/The_Standard#C18) merely "addressed defects in C11 without introducing new language features." C23 is still [unapproved and in draft](https://www.iso-9899.info/n3047.html). – Andrew Henle Feb 04 '23 at 16:57
  • So it is more recently approved, it just adds no more language features. –  Feb 05 '23 at 17:56
4

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
2

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.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 2
    I'm accustomed in C to having a primitive type mean one of two things. Either it's not a struct and not an array, or it's not a struct and not an array and not a pointer, depending on context. – Joshua Feb 03 '23 at 17:53
  • The full list of types is in section 6.7.2 of C11. It's even longer than the wikipedia list. And then there's storage class specifiers and type qualifiers and alignment specifiers. And let's not forget bit fields. – user3386109 Feb 03 '23 at 18:10
  • 1
    *list of these types is rather long* Not really: 1. `[un[signed]] char`, 2. `[un[signed]] short [int]`, 3. `[un[signed]] int`, 4. `[un[signed]] long [int]`, 5. `[un[signed]] long long [int]` 6. `float` 7. `double`, 8. `long double`. – Andrew Henle Feb 03 '23 at 19:53
  • @AndrewHenle you forgot [float/[long] double] _Complex and _Bool – Elzaidir Feb 03 '23 at 20:30
  • @Elzaidir 6, 7, and 8. `_Bool` will be implemented as a value-constrained unsigned integer type drawn from 1-5 of my list, and `_Complex` is both optional and *de facto* an aggregate of two floating point values drawn from 6-8 of my list. – Andrew Henle Feb 03 '23 at 21:04
0

Unfortunately, there is not a cohesive set of vocabulary to extend across multiple languages.

Java's primitive types distinguish them from object types or reference types.

While C has many many types, the closest, to Java's primitive types are the main types.

The languages do different things and hence have different groupings for their types and the types are different themselves.

An int in C might be 2 bytes for an embedded system, but in Java it is always 4 bytes.

chris
  • 34
  • 4