|
|
|
|
|
Defining Data Types in ProgrammingNumbers, integer and floating point ranges, characters, strings, pointers and more.An introduction to one of the core programming issues – data types. We look at what a data type is, why it is required, and why it is so useful.
IntroductionData types are essential to any computer programming language. Without them, it becomes very difficult to maintain information within a computer program. Since the main principle behind computer programming is to take information, process it, and deliver the information in a different form to the user, data types obviously play a large part in determining how this is achieved. Different languages have different constraints upon the data types that they provide. In this article we shall distinguish across languages where relevant, but you will need to cross check with the documentation for the specific language that you are using to determine some aspects. Before we begin, we should also note that some languages are strongly typed, meaning that the data type of a piece of information has to be declared before that variable (or slot) can be used. Weakly typed languages, on the other hand (like many BASIC variants) do not require that a variable's data type is declared before use, but it should always maintain the same data type throughout its lifespan. Languages usually allow the possibility to cast (convert) between compatible types. NumbersIn most languages, numbers are either integers or floating points. The number 320 is an integer, as is -125. The number 27.1 is a floating point number, as is -34.2. The larger the range of numbers needing to be represented, the larger the (fixed) data storage requirement will be. Subsequently, and because memory and other storage has traditionally been limited, computer programming languages also provide different sizes of numbers:
The names may change (Modula-2 uses REAL, INTEGER and LONGREAL, LONGINT for example), but the general principles remain the same. The exact ranges may also change, but can be calculated if one knows the byte size of the data type - we can only represent the numbers 0 to 255 (or -127 to 128) in a single byte, for example. Support for complex numbers (with a real and imaginary part) is not always assured, and matrix calculations will usually have to be performed manually (programmatically). Characters & StringsA character value is usually one byte of ASCII, but other character sets (EBCDIC, for example) may change this. It is not usual for a programming language to deal directly with strings (sequences of characters), and usually a scalar variable (array), or pointer to a memory block of a known size will need to be declared. Where the string type is available, it should usually be an abstract data type (or class) which hides the internals from the programmer (encapsulation), and must be manipulated using the exposed functions (methods) for doing so. For example, in C++ we might have a statement 'MyString-GetLength()'. PointersA pointer is a reference to a piece of memory, whether that be 'raw' memory, or formatted according to another data type, or a piece of executable code, such as a function (procedure, method...). Pointers can be used to store arrays (like strings of characters), or as a reference to s static single value. Care needs to be taken when using pointers, as they can have some strange properties under certain conditions. ConclusionThe above is intended as an introduction to the most common data types that a computer programming project will entail. It is not the end of the story, and the General Programming Index will point to other related topics. Navigation Links
The copyright of the article Defining Data Types in Programming in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Defining Data Types in Programming in print or online must be granted by the author in writing.
|
|
|
|