[ Pobierz całość w formacie PDF ]
.263 1 signed 64-bitByte 0.255 unsigned 8-bitWord 0.65535 unsigned 16-bitLongword 0.4294967295 unsigned 32-bitIn general, arithmetic operations on integers return a value of type Integer which, inits current implementation, is equivalent to the 32-bit Longint.Operations return avalue of type Int64 only when performed on an Int64 operand.Hence the followingcode produces incorrect results.varI: Integer;J: Int64;’I := High(Integer);J := I + 1;To get an Int64 return value in this situation, cast I as Int64:’J := Int64(I) + 1;For more information, see Arithmetic operators on page 4-6.Note Most standard routines that take integer arguments truncate Int64 values to 32 bits.However, the High, Low, Succ, Pred, Inc, Dec, IntToStr, and IntToHex routines fullysupport Int64 arguments.Also, the Round, Trunc, StrToInt64, and StrToInt64Deffunctions return Int64 values.A few routines including Ord cannot take Int64values at all.When you increment the last value or decrement the first value of an integer type, theresult wraps around the beginning or end of the range.For example, the Shortint typehas the range 128.127; hence, after execution of the codevar I: Shortint;’I := High(Shortint);I := I + 1;the value of I is 128.If compiler range-checking is enabled, however, this codegenerates a runtime error.5-4 Obj ec t Pas c al Language Gui deSi mp l e t y p e sCharacter typesThe fundamental character types are AnsiChar and WideChar.AnsiChar values arebyte-sized (8-bit) characters ordered according to the locale character set which ispossibly multibyte.AnsiChar was originally modeled after the ANSI character set(thus its name) but has now been broadened to refer to the current locale characterset.WideChar characters use more than one byte to represent every character.In thecurrent implementations, WideChar is word-sized (16-bit) characters orderedaccording to the Unicode character set (note that it could be longer in futureimplementations).The first 256 Unicode characters correspond to the ANSIcharacters.The generic character type is Char, which is equivalent to AnsiChar.Because theimplementation of Char is subject to change, it s a good idea to use the standardfunction SizeOf rather than a hard-coded constant when writing programs that mayneed to handle characters of different sizes.A string constant of length 1, such as 'A', can denote a character value.Thepredefined function Chr returns the character value for any integer in the range ofAnsiChar or WideChar; for example, Chr(65) returns the letter A.Character values, like integers, wrap around when decremented or incremented pastthe beginning or end of their range (unless range-checking is enabled).For example,after execution of the codevarLetter: Char;I: Integer;beginLetter := High(Letter);for I := 1 to 66 doInc(Letter);end;Letter has the value A (ASCII 65).For more information about Unicode characters, see About extended character setson page 5-13 and Working with null-terminated strings on page 5-13.Boolean typesThe four predefined Boolean types are Boolean, ByteBool, WordBool, and LongBool.Boolean is the preferred type.The others exist to provide compatibility with otherlanguages and operating system libraries.A Boolean variable occupies one byte of memory, a ByteBool variable also occupiesone byte, a WordBool variable occupies two bytes (one word), and a LongBool variableoccupies four bytes (two words).Dat a t y pes , v ar i abl es, and c ons t ant s 5-5Si mp l e t y p e sBoolean values are denoted by the predefined constants True and False.Thefollowing relationships hold.Boolean ByteBool, WordBool, LongBoolFalse 'A' returns True.Zero-lengthstrings hold the lowest values.You can index a string variable just as you would an array.If S is a string variableand i an integer expression, S[i] represents the ith character or, strictly speaking,the ith byte in S.For a ShortString or AnsiString, S[i] is of type AnsiChar; for aWideString, S[i] is of type WideChar.The statement MyString[2] := 'A'; assigns thevalue A to the second character of MyString.The following code uses the standardUpCase function to convert MyString to uppercase.var I: Integer;beginI := Length(MyString);while I > 0 dobeginMyString[I] := UpCase(MyString[I]);I := I - 1;end;end;Be careful indexing strings in this way, since overwriting the end of a string can causeaccess violations.Also, avoid passing long-string indexes as var parameters, becausethis results in inefficient code.You can assign the value of a string constant or any other expression that returns astring to a variable.The length of the string changes dynamically when theassignment is made.Examples:MyString := 'Hello world!';MyString := 'Hello ' + 'world';MyString := MyString + '!';MyString := ' '; { space }MyString := ''; { empty string }For more information, see Character strings on page 4-4 and String operators onpage 4-9.Dat a t ypes, var i abl es, and const ant s 5-11St r i n g t y p e sShort stringsA ShortString is 0 to 255 characters long.While the length of a ShortString can changedynamically, its memory is a statically allocated 256 bytes; the first byte stores thelength of the string, and the remaining 255 bytes are available for characters.If S is aShortString variable, Ord(S[0]), like Length(S), returns the length of S; assigning avalue to S[0], like calling SetLength, changes the length of S.ShortString uses 8-bitANSI characters and is maintained for backward compatibility only.Object Pascal supports short-string types in effect, subtypes of ShortString whosemaximum length is anywhere from 0 to 255 characters.These are denoted by abracketed numeral appended to the reserved word string.For example,var MyString: string[100];creates a variable called MyString whose maximum length is 100 characters.This isequivalent to the declarationstype CString = string[100];var MyString: CString;Variables declared in this way allocate only as much memory as the type requiresthat is, the specified maximum length plus one byte.In our example, MyString uses101 bytes, as compared to 256 bytes for a variable of the predefined ShortString type.When you assign a value to a short-string variable, the string is truncated if it exceedsthe maximum length for the type
[ Pobierz całość w formacie PDF ]