Safekipedia

Primitive data type

Discoverer · ages 10+Adapted from Wikipedia

In computer science, primitive data types are the basic data types that all other data types are built from. They are the simplest kinds of information a computer can work with. Usually, the term refers to the small set of data representations that a particular processor uses, and every compiled program must use these same representations.

Most processors support a similar set of primitive data types, although the exact ways they are stored can differ. More generally, the term can also mean the standard data types that are built into a programming language. These are called built-in types.

Data types that are not primitive are called derived or composite types. They are made by combining simpler types together.

Primitive types are almost always value types. Composite types can also be value types, though.

Common primitive data types

Main article: Integer (computer science)

Main article: Floating-point arithmetic

Main article: Boolean data type

The most common primitive types are those that computer hardware uses and supports, such as whole numbers of different sizes, floating-point numbers, and true-or-false values. Working with these types is usually quite fast. Integer addition, for example, can be done with a single machine instruction, and some processors have special instructions for handling a row of characters with a single instruction. The type you choose can still affect speed. For example, it is faster to use SIMD operations and types on a group of floating-point numbers.

Integers are whole numbers within a range. They can be signed, which allows negative values, or unsigned, which allows non-negative integers only. Common ranges are:

A floating-point number is a rational number with a limited precision, which means it can have a fractional part. It is stored in a form similar to scientific notation, usually in binary but sometimes in decimal. Because of the limited precision, only some real or rational numbers can be stored exactly, and others can only be stored approximately. Many languages have a single-precision type, often called float, and a double-precision type, often called double. A Boolean type, usually called bool or boolean, is a logical type that can be either true or false. Only one bit is needed for this, but languages usually store Booleans as one or more bytes. Languages such as Java, Pascal, and Ada treat Booleans as a separate logical type. Some languages convert Booleans to numbers at times, which gives Booleans and Boolean expressions extra semantics or keeps older programs working. Early versions of the C language, which followed ANSI C and its earlier standards, had no Boolean type. Instead, zero was false and any other value was true. The newer C99 added a Boolean type, and C++ supports bool as a built-in type.

Size (bytes)Size (bits)NamesSigned range (two's complement representation)Unsigned range
1 byte8 bitsByte, octet, minimum size of char in C99( see limits.h CHAR_BIT)−128 to +1270 to 255
2 bytes16 bitsx86 word, minimum size of short and int in C−32,768 to +32,7670 to 65,535
4 bytes32 bitsx86 double word, minimum size of long in C, actual size of int for most modern C compilers, pointer for IA-32-compatible processors−2,147,483,648 to +2,147,483,6470 to 4,294,967,295
8 bytes64 bitsx86 quadruple word, minimum size of long long in C, actual size of long for most modern C compilers, pointer for x86-64-compatible processors−9,223,372,036,854,775,808 to +9,223,372,036,854,775,8070 to 18,446,744,073,709,551,615

Specific languages

Main article: C data types § Basic types

Each programming language has its own basic data types. In C and C++, there are at least four: char, int, float, and double. Words like short, long, signed, and unsigned change these, so C has many types that depend on the computer. C99 let long be used twice with int, and C23 made bool a core type. C++ has a std::byte type in its standard library, but it is an enum rather than a basic type. C and C++ also have types like size_t for counting things such as positions in lists. C++26 adds a mirror type for reflective programming (see Reflection (C++)). Java's basic types are similar to C's. They include integer types like byte, short, int, long, and char, floating-point numbers like float and double, and a Boolean type called boolean, which holds true or false. The Java virtual machine also has a returnAddress type, but programmers usually cannot use it. The XML Schema Definition language has 19 basic types, including string values made of Unicode code points, Calendar dates and times, binary data written as hexadecimal or Base64, URI addresses, and qualified names.

JavaScript has 7 basic types: string, number, bigint, boolean, symbol, undefined, and null. Their values cannot be changed once they are made (immutable). These basic types are not objects and have no methods or properties, though most of them have object wrappers. In Visual Basic (.NET), the basic types include integer types, floating-point types, a decimal type, a Boolean type, a date and time type, a single character type, and a text string type. Rust has signed and unsigned whole-number types such as u8, i32, and u128, along with usize and isize, which are used for positions in arrays. It also has bool for Boolean values, f32 and f64 for floating point numbers, and char for a single unicode character.

Built-in types

Built-in types are special types that have direct support built into the compiler or runtime, so they cannot simply be defined in a header file or standard library module. Besides integers, floating-point numbers, and Booleans, other built-in types include the void type and the nullptr_t null pointer type (link) in C++11 and C23, tuples in Standard ML, Python, Scala, Swift, and Elixir, and lists in Common Lisp, Python, Scheme, and Haskell. They also include fixed-point numbers with different precisions and a scale chosen by the programmer, and complex numbers in C99, Fortran, Common Lisp, Python, D, and Go, which are made of a real part and an imaginary part. An enumerated type is a list of names for allowed values, which can make code easier to read and check. Other examples are rational numbers in Common Lisp, arbitrary-precision Integer types in Common Lisp, Erlang, and Haskell, associative arrays, records, or sets in Perl, PHP, Python, Ruby, JavaScript, Lua, D, and Go, references (also called a pointer or handle or descriptor), symbols in Lisp, and first-class functions in functional languages, JavaScript, Lua, D, Go, and newer standards of C++, Java, C#, and Perl.

Characters and strings are also built-in types. A character type can represent all Unicode characters, so it must be at least 21 bits wide. Some languages, such as Julia, have a true 32-bit Unicode character type. Languages such as JavaScript, Python, Ruby, and many dialects of BASIC have no primitive character type. Instead, they use strings as a primitive data type, typically with the UTF-8 encoding, and a string of length one usually represents a single character. Some languages have character types too small for all Unicode characters, so they are really integer types with a misleading name. For example, C's char is the smallest addressable unit of memory, which standards such as POSIX require to be 8 bits, and Java's char is a 16-bit integer. Also, the word "string" does not always mean Unicode characters. It can also mean a sequence of bytes, as in x86-64 string instructions that work on items 1, 2, 4, or 8 bytes long.

Quiz time!

5 questions about Primitive data type

Question 1 of 5

What are primitive data types?

Keep exploring

This article is a child-friendly adaptation of the Wikipedia article on Primitive data type, available under CC BY-SA 4.0.