Variables and Data Types/es

From Lazarus wiki
Revision as of 18:50, 8 January 2011 by Iskraelectrica (talk | contribs) (New page: 1D - Tipos de Datos y de variables (autor: Tao Yue, estado: ''cambiado'')    Las variables son similares a las constantes, pero sus valores pueden ser cambiados durante la ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

1D - Tipos de Datos y de variables (autor: Tao Yue, estado: cambiado)

   Las variables son similares a las constantes, pero sus valores pueden ser cambiados durante la ejecución del programa. Las variables deben ser declaradas en Pascal antes de poder ser utilizadas: <delphi> var

 ListaIdentificadores1 : TipoDato1;
 ListaIdentificadores2 : TipoDato2;
 ListaIdentificadores3 : TipoDato3;
 ...</delphi>

   Donde ListaIdentificadores es una serie de identificadores, separados por comas (,). Todos los identificadores en la lista se declaran como del mismo tipo de datos.

   Los tipos de datos básicos en Pascal incluyen:

  • integer (entero)
  • real
  • char (un byte, un carácter)
  • boolean (lógico: verdadero -true- o falso -false-)

   El Pascal estándar no prevé nada para el tipo de datos de cadena de caracteres, pero los compiladores más modernos si lo hacen. Programadores Pascal avanzados utilizan punteros para la asignación dinámica de memoria, objetos para la programación orientada a objetos, y muchos otros, pero esto te ayudará a comenzar

   Más información sobre los tipos de datos de Pascal:

  • The integer data type can contain integers from -32768 to 32767. This is the signed range that can be stored in a 16-bit word, and is a legacy of the era when 16-bit CPUs were common. For backward compatibility purposes, a 32-bit signed integer is a longint and can hold a much greater range of values.
  • The real data type has a range from 3.4x10-38 to 3.4x1038, in addition to the same range on the negative side. Real values are stored inside the computer similarly to scientific notation, with a mantissa and exponent, with some complications. In Pascal, you can express real values in your code in either fixed-point notation or in scientific notation, with the character E separating the mantissa from the exponent. Thus, 452.13 is the same as 4.5213e2
  • The char data type holds characters. Be sure to enclose them in single quotes, like so: 'a' 'B' '+' Standard Pascal uses 8-bit characters, not 16-bits, so Unicode, which is used to represent all the world's language sets in one UNIfied CODE system, is not supported.
  • The WideChar is a two-byte character (an element of a DBCS: Double Byte Character Set) and can hold a unicode character.
  • Free Pascal supports the Delphi implementation of the PChar type. PChar is defined as a pointer to a Char type, but allows additional operations. The PChar type can be understood best as the Pascal equivalent of a C-style null-terminated string, i.e. a variable of type PChar is a pointer that points to an array of type Char, which is ended by a null-character (#0). Free Pascal supports initializing of PChar typed constants, or a direct assignment. For example, the following pieces of code are equivalent:
  • Free Pascal supports the String type as it is defined in Turbo Pascal: a sequence of characters with an optional size specification. It also supports ansistrings (with unlimited length) as in Delphi. And can be declared as:

<delphi> variable_nombre : string; // si no se especifica la longitud, el valor predeterminado será 255

variable_nombre : string[longitud];          // donde:  1 < longitud <= 255 </delphi>
  • The predefined type ShortString is defined as a string of size 255.
  • AnsiStrings are strings that have no length limit. They are reference counted and are guaranteed to be null terminated. Internally, an ansistring is treated as a pointer: the actual content of the string is stored on the heap, as much memory as needed to store the string content is allocated.
  • Widestrings (used to represent unicode character strings) are implemented in much the same way as ansistrings: reference counted, null-terminated arrays, only they are implemented as arrays of WideChars instead of regular Chars.
  • The boolean data type can have only two values: TRUE and FALSE

   Un ejemplo de declaración de varias variables es: <delphi> var

 edad, anyo, grado : integer;
 circunferencia : real;
 GradoObtenido : char;
 Aprobado: Boolean;</delphi>

   Del manual de FPC

Tipos enteros
Tipo Rango Bytes
Byte 0 .. 255 1
Shortint -128 .. 127 1
Smallint -32768 .. 32767 2
Word 0 .. 65535 2
Integer smallint o longint 2 o 4
Cardinal longword 4
Longint -2147483648 .. 2147483647 4
Longword 0..4294967295 4
Int64 -9223372036854775808 .. 9223372036854775807 8
QWord 0 .. 18446744073709551615 8

   Free Pascal realiza una conversión automática de tipos si en las expresiones se utilizan diferentes tipos de enteros .

Tipos real
Tipo Rango dígitos significativos Bytes
Real depende de la plataforma ??? 4 or 8
Single 1.5E-45 .. 3.4E38 7-8 4
Double 5.0E-324 .. 1.7E308 15-16 8
Extended 1.9E-4932 .. 1.1E4932 19-20 10
Comp -2E64+1 .. 2E63-1 19-20 8
Currency -922337203685477.5808 922337203685477.5807 8


Tipos lógicos (boolean)
Tipo Bytes Ord(True)
Boolean 1 1
ByteBool 1 Valor distinto de cero
WordBool 2 Valor distinto de cero
LongBool 4 Valor distinto de cero
previo índice siguiente