|“I couldn't ignore that banner at the top of the site anymore... I use Wikipedia far too often to ignore the need!”||
Primitive data type
From Wikipedia, the free encyclopedia
||This article does not cite any references or sources.
Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (May 2009)
||This article is in need of attention from an expert on the subject. WikiProject Computer science or the Computer science Portal may be able to help recruit one. (May 2009)|
- a basic type is a data type provided by a programming language as a basic building block. Most languages allow more complicated composite types to be recursively constructed starting from basic types.
- a built-in type is a data type for which the programming language provides built-in support.
In most programming languages, all basic data types are built-in. In addition, many languages also provide a set of composite data types. Opinions vary as to whether a built-in type that is not basic should be considered "primitive".
Depending on the language and its implementation, primitive data
types may or may not have a one-to-one correspondence with objects in
the computer's memory. However, one usually expects operations on basic
primitive data types to be the fastest language constructs there are. Integer addition, for example, can be performed as a single machine instruction, and some processors offer specific instructions to process sequences of characters with a single instruction. In particular, the C
standard mentions that "a 'plain' int object has the natural size
suggested by the architecture of the execution environment". This means
int is likely to be 32 bits long on a 32-bit architecture. Basic primitive types are almost always value types.
Most languages do not allow the behavior or capabilities of primitive (either built-in or basic) data types to be modified by programs. Exceptions include Smalltalk, which permits all data types to be extended within a program, adding to the operations that can be performed on them or even redefining the built-in operations.
Typical basic primitive types may include:
- Character (
- Integer (
byte) with a variety of precisions;
- Floating-point number (
- Fixed-point number (
fixed) with a variety of precisions and a programmer-selected scale.
- Boolean having the values true and false.
- Reference (also called a pointer or handle), a small value referring to another object's address in memory, possibly a much larger one.
More sophisticated types which can be built-in include:
- Tuples in ML, Python
- Linked lists in Lisp
- Complex numbers in Fortran, C (C99), Lisp, Python
- Rational numbers in Lisp
- Hash tables in various guises, in Lisp, Perl, Python, Lua
- First class functions, closures, continuations in Functional programming languages such as Lisp and ML
 Specific primitive data types
 Integer numbers
An integer data type can hold a whole number, but no fraction. Integers may be either signed (allowing negative values) or unsigned (nonnegative values only). Typical sizes of integers are:
|Size||Names||Signed Range||Unsigned Range|
|8 bits||Byte||−128 to +127||0 to 255|
|16 bits||Word, short int||−32,768 to +32,767||0 to 65,535|
|32 bits||Double Word, long int||−2,147,483,648 to +2,147,483,647||0 to 4,294,967,295|
|64 bits||long long (C), long (Java)||−9,223,372,036,854,775,808 to +9,223,372,036,854,775,807||0 to 18,446,744,073,709,551,615|
Literals for integers consist of a sequence of digits. Most programming languages disallow the use of commas for digit grouping, although FORTRAN (77 and Fortran 90 and above fixed form source but not free form source) allows embedded spaces, and Perl, Ruby, and D allow embedded underscores. Negation is indicated by a minus sign (−) before the value. Examples of integer literals are:
type, typically denoted "bool" or "boolean", is a single-bit type that
can be either "true" or "false". In some languages (e.g., C++),
boolean values may be implicitly converted to integers, usually
according to the mapping false → 0 and true → 1 (for example,
true + true would be a valid expression evaluating to 2), but other languages (e.g., Java and Pascal) disallow this.
 Floating-point numbers
A floating-point number represents a limited-precision rational number that may have a fractional part. These numbers are stored internally in a format equivalent to scientific notation, typically in binary but sometimes in decimal. Because floating-point numbers have limited precision, only a subset of real or rational numbers are exactly representable; other numbers can be represented only approximately.
Literals for floating point numbers include a decimal point, and typically use "e" or "E" to denote scientific notation. Examples of floating-point literals are:
 Fixed-point numbers
A fixed-point number represents a limited-precision rational number that may have a fractional part. These numbers are stored internally in a scaled-integer form, typically in binary but sometimes in decimal. Because fixed-point numbers have limited precision, only a subset of real or rational numbers are exactly representable; other numbers can be represented only approximately. Fixed-point numbers also tend to have a more limited range of values than floating point, and so the programmer must be careful to avoid overflow in intermediate calculations as well as the final results.
 Characters and strings
Characters may be combined into strings. The string data can include numbers and other numerical symbols but will be treated as text.
In most languages, a string is equivalent to an array of characters or code units, but Java treats them as distinct types (
char). Other languages (such as Python, and many dialects of BASIC) have no separate character type; strings with a length of one are normally used to represent (single code unit) characters.
Literals for characters and strings are usually surrounded by quotation marks: sometimes, single quotes (') are used for characters and double quotes (") are used for strings.
Examples of character literals in C syntax are:
- '\t' (tab character)
Examples of string literals in C syntax are:
- "Hello World"
 Numeric data type ranges
Each numeric data type has its maximum and minimum value known as the range. Attempting to store a number outside the range may lead to compiler/runtime errors, or to incorrect calculations (due to truncation) depending on the language being used.
The range of a variable is based on the number of bytes used to save the value, and an integer data type is usually able to store 2n values (where n is the number of bits that contribute to the value). For other data types (e.g. floating point values) the range is more complicated and will vary depending on the method used to store it. There are also some types that do not use entire bytes, e.g. a boolean that requires a single bit, and represents a binary value (although in practice a byte is often used, with the remaining 7 bits being redundant). Some programming languages (such as Ada and Pascal) also allow the opposite direction, that is, the programmer defines the range and precision needed to solve a given problem and the compiler chooses the most appropriate integer or floating point type automatically.
 See also