Anda di halaman 1dari 9

Integer Types

Pgina 1 de 9

MQL4 Reference / Language Basics / Data Types / Integer Types

Integer Types
In MQL4 integers are represented by eleven types. Some types can be used together with other ones, if
required by the program logic, but in this case it's necessary to remember the rules of typecasting..
The table below lists the characteristics of each type. Besides, the last column features a type in C++
corresponding to each type.
Type

Size in
Bytes

Minimum Value

Maximum Value

C++ Analog

char

-128

127

char

uchar

255

unsigned char,
BYTE

bool

0(false)

1(true)

bool

short

-32 768

32 767

short, wchar_t

ushort

65 535

unsigned short,
WORD

int

- 2 147 483 648

2 147 483 647

int

uint

4 294 967 295

unsigned int,
DWORD

color

-1

16 777 215

int, COLORREF

long

-9 223 372 036 854


775 808

9 223 372 036 854 775 807

__int64

ulong

18 446 744 073 709 551 615

unsigned __int64

datetime

0 (1970.01.01
0:00:00)

32 535 244 799 (3000.12.31


23:59:59)

__time64_t

Integer type values can also be presented as numeric constants, color literals, date-time literals,
character constants and enumerations.
See also
Conversion Functions, Numeric Type Constants

MQL4 Reference / Language Basics / Data Types / Integer Types / Char, Short, Int and
Long Types

Char, Short, Int and Long Types


char
The char type takes 1 byte of memory (8 bits) and allows expressing in the binary notation 2^8=256
values. The char type can contain both positive and negative values. The range of values is from -128 to
127.

uchar
The uchar integer type also occupies 1 byte of memory, as well as the char type, but unlike it uchar is
intended only for positive values. The minimum value is zero, the maximum value is 255. The first letter
u in the name of the uchar type is the abbreviation for unsigned.

short

file:///C:/Users/wiliams/AppData/Local/Temp/~hh5FF8.htm

03/05/2016

Integer Types

Pgina 2 de 9

The size of the short type is 2 bytes (16 bits) and, accordingly, it allows expressing the range of values
equal to 2 to the power 16: 2^16 = 65 536.Since the short type is a signed one, and contains both positive
and negative values, the range of values is between -32 768 and 32 767.

ushort
The unsigned short type is the type ushort, which also has a size of 2 bytes. The minimum value is 0, the
maximum value is 65 535.

int
The size of the int type is 4 bytes (32 bits). The minimal value is -2 147 483 648, the maximal one is 2
147 483 647.

uint
The unsigned integer type is uint. It takes 4 bytes of memory and allows expressing integers from 0 to 4
294 967 295.

long
The size of the long type is 8 bytes (64 bits). The minimum value is -9 223 372 036 854 775 808, the
maximum value is 9 223 372 036 854 775 807.

ulong
The ulong type also occupies 8 bytes and can store values from 0 to 18 446 744 073 709 551 615.
Examples:
char ch=12;
short sh=-5000;
int
in=2445777;

Since the unsigned integer types are not designed for storing negative values, the attempt to set a
negative value can lead to unexpected consequences. Such a simple script will lead to an infinite loop:
//--- Infinite loop
void OnStart()
{
uchar u_ch;
for(char ch=-128;ch<128;ch++)
{
u_ch=ch;
Print("ch = ",ch," u_ch = ",u_ch);
}
}

The correct variant is:


//--- Correct variant
void OnStart()
{
uchar u_ch;
for(char ch=-128;ch<=127;ch++)
{
u_ch=ch;
Print("ch = ",ch," u_ch = ",u_ch);
if(ch==127) break;
}
}

Result:

file:///C:/Users/wiliams/AppData/Local/Temp/~hh5FF8.htm

03/05/2016

Integer Types

ch= -128
ch= -127
ch= -126
ch= -125
ch= -124
ch= -123
ch= -122
ch= -121
ch= -120
ch= -119
ch= -118
ch= -117
ch= -116
ch= -115
ch= -114
ch= -113
ch= -112
ch= -111
...

Pgina 3 de 9

u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=
u_ch=

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

Examples:
//--- Negative values can not be stored in unsigned types
uchar u_ch=-120;
ushort u_sh=-5000;
uint
u_in=-401280;

Hexadecimal: numbers 0-9, the letters a-f or A-F for the values of 10-15; start with 0x or 0X.
Examples:
0x0A, 0x12, 0X12, 0x2f, 0xA3, 0Xa3, 0X7C7

See also
Typecasting

MQL4 Reference / Language Basics / Data Types / Integer Types / Character


Constants

Character Constants
Characters as elements of a string in MQL4 are indexes in the Unicode character set. They are
hexadecimal values that can be cast into integers, and that can be manipulated by integer operations
like addition and subtraction.
Any single character in quotation marks or a hexadecimal ASCII code of a character as '\x10' is a character
constant and is of ushort type. For example, a record of '0' type is a numerical value 30, that corresponds
to the index of zero in the table of characters.
Example:

file:///C:/Users/wiliams/AppData/Local/Temp/~hh5FF8.htm

03/05/2016

Integer Types

Pgina 4 de 9

void OnStart()
{
//--- define character constants
int symbol_0='0';
int symbol_9=symbol_0+9; // get symbol '9'
//--- output values of constants
printf("In a decimal form: symbol_0 = %d, symbol_9 = %d",symbol_0,symbol_9);
printf("In a hexadecimal form: symbol_0 = 0x%x, symbol_9 = 0x%x",symbol_0,symbol_9);
//--- enter constants into a string
string test="";
StringSetCharacter(test,0,symbol_0);
StringSetCharacter(test,1,symbol_9);
//--- this is what they look like in a string
Print(test);
}

A backslash is a control character for a compiler when dealing with constant strings and character
constants in a source text of a program. Some symbols, for example a single quote ('), double quotes ("),
backslash (\) and control characters can be represented as a combination of symbols that start with a
backslash (\), according to the below table:
Character name

Mnemonic code or
image

Record in
MQL4

Numeric value

new line (line


feed)

LF

'\n'

10

horizontal tab

HT

'\t'

carriage return

CR

'\r'

13

backslash

'\\'

92

single quote

'

'\''

39

double quote

"

'\"'

34

hexadecimal code

hhhh

'\xhhhh'

1 to 4 hexadecimal characters

decimal code

'\d'

decimal number from 0 to


65535

If a backslash is followed by a character other than those described above, result is undefined.
Example

file:///C:/Users/wiliams/AppData/Local/Temp/~hh5FF8.htm

03/05/2016

Integer Types

Pgina 5 de 9

void OnStart()
{
//--- declare character constants
int a='A';
int b='$';
int c='';
// code 0xA9
int d='\xAE';
// code of the symbol
//--- output print constants
Print(a,b,c,d);
//--- add a character to the string
string test="";
StringSetCharacter(test,0,a);
Print(test);
//--- replace a character in a string
StringSetCharacter(test,0,b);
Print(test);
//--- replace a character in a string
StringSetCharacter(test,0,c);
Print(test);
//--- replace a character in a string
StringSetCharacter(test,0,d);
Print(test);
//--- represent characters as a number
int a1=65;
int b1=36;
int c1=169;
int d1=174;
//--- add a character to the string
StringSetCharacter(test,1,a1);
Print(test);
//--- add a character to the string
StringSetCharacter(test,1,b1);
Print(test);
//--- add a character to the string
StringSetCharacter(test,1,c1);
Print(test);
//--- add a character to the string
StringSetCharacter(test,1,d1);
Print(test);
}

As it was mentioned above, the value of a character constant (or variable) is an index in the table of
characters. Index being an integer, it can be written in different ways.

file:///C:/Users/wiliams/AppData/Local/Temp/~hh5FF8.htm

03/05/2016

Integer Types

void OnStart()
{
//--int a=0xAE;
// the code of corresponds
int b=0x24;
// the code of $ corresponds
int c=0xA9;
// the code of corresponds
int d=0x263A;
// the code of corresponds
//--- show values
Print(a,b,c,d);
//--- add a character to the string
string test="";
StringSetCharacter(test,0,a);
Print(test);
//--- replace a character in a string
StringSetCharacter(test,0,b);
Print(test);
//--- replace a character in a string
StringSetCharacter(test,0,c);
Print(test);
//--- replace a character in a string
StringSetCharacter(test,0,d);
Print(test);
//--- codes of suits
int a1=0x2660;
int b1=0x2661;
int c1=0x2662;
int d1=0x2663;
//--- add a character of spades
StringSetCharacter(test,1,a1);
Print(test);
//--- add a character of hearts
StringSetCharacter(test,2,b1);
Print(test);
//--- add a character of diamonds
StringSetCharacter(test,3,c1);
Print(test);
//--- add a character of clubs
StringSetCharacter(test,4,d1);
Print(test);
//--- Example of character literals in a string
test="Queen\x2660Ace\x2662";
printf("%s",test);
}

Pgina 6 de 9

to
to
to
to

the
the
the
the

'\xAE' literal
'\x24' literal
'\xA9' literal
'\x263A' literal

The internal representation of a character literal is the ushort type. Character constants can accept
values from 0 to 65535.
See also
StringSetCharacter(), StringGetCharacter(), ShortToString(), ShortArrayToString(), StringToShortArray()

MQL4 Reference / Language Basics / Data Types / Integer Types / Datetime Type

Datetime Type
The datetime type is intended for storing the date and time as the number of seconds elapsed since
January 01, 1970. This type occupies 8 bytes of memory.

file:///C:/Users/wiliams/AppData/Local/Temp/~hh5FF8.htm

03/05/2016

Integer Types

Pgina 7 de 9

Constants of the date and time can be represented as a literal string, which consists of 6 parts showing
the numerical value of the year, month, day (or day, month, year), hours, minutes and seconds. The
constant is enclosed in single quotation marks and starts with the D character.
Values range from 1 January, 1970 to 31 December, 3000. Either date (year , month, day) or time (hours,
minutes, seconds), or all together can be omitted.
With literal date specification, it is desirable that you specify year, month and day. Otherwise the
compiler returns a warning about an incomplete entry.
Examples:
datetime NY=D'2015.01.01 00:00';
// Time of beginning of year 2015
datetime d1=D'1980.07.19 12:30:27'; // Year Month Day Hours Minutes Seconds
datetime d2=D'19.07.1980 12:30:27'; // Equal to D'1980.07.19 12:30:27';
datetime d3=D'19.07.1980 12';
// Equal to D'1980.07.19 12:00:00'
datetime d4=D'01.01.2004';
// Equal to D'01.01.2004 00:00:00'
datetime compilation_date=__DATE__;
// Compilation date
datetime compilation_date_time=__DATETIME__;
// Compilation date and time
datetime compilation_time=__DATETIME__-__DATE__;// Compilation time
//--- Examples of declarations after which compiler warnings will be returned
datetime warning1=D'12:30:27';
// Equal to D'[date of compilation] 12:30:27'
datetime warning2=D'';
// Equal to __DATETIME__

The string representation of datetime type depends on compilation mode:


datetime date=D'2014.03.05 15:46:58';
string str="mydate="+date;
//--- str="mydate=1394034418" - without #property strict
//--- str="mydate=2014.03.05 15:46:58" - with #property strict

See also
Structure of the Date Type, Date and Time, TimeToString, StringToTime

MQL4 Reference / Language Basics / Data Types / Integer Types / Color Type

Color Type
The color type is intended for storing information about color and occupies 4 bytes in memory. The first
byte is ignored, the remaining 3 bytes contain the RGB-components.
Color constants can be represented in three ways: literally, by integers, or by name (for named Webcolors only).
Literal representation consists of three parts representing numerical rate values of the three main color
components: red, green, blue. The constant starts with C and is enclosed in single quotes. Numerical
rate values of a color component lie in the range from 0 to 255.
Integer-valued representation is written in a form of hexadecimal or a decimal number. A hexadecimal
number looks like 0x00BBGGRR, where RR is the rate of the red color component, GG - of the green one,
and BB - of the blue one. Decimal constants are not directly reflected in the RGB. They represent a
decimal value of the hexadecimal integer representation.
Specific colors reflect the so-called Web-colors set.
Examples:

file:///C:/Users/wiliams/AppData/Local/Temp/~hh5FF8.htm

03/05/2016

Integer Types

Pgina 8 de 9

//--- Literals
C'128,128,128'
// Gray
C'0x00,0x00,0xFF' // Blue
//color names
clrRed
// Red
clrYellow
// Yellow
clrBlack
// Black
//--- Integral representations
0xFFFFFF
// White
16777215
// White
0x008000
// Green
32768
// Green

See also
Web Colors, ColorToString, StringToColor, Typecasting

MQL4 Reference / Language Basics / Data Types / Integer Types / Bool Type

Bool Type
The bool type is intended to store the logical values of true or false, numeric representation of them is 1
or 0, respectively.
Examples:
bool a = true;
bool b = false;
bool c = 1;

The internal representation is a whole number 1 byte large. It should be noted that in logical expressions
you can use other integer or real types or expressions of these types - the compiler will not generate any
error. In this case, the zero value will be interpreted as false, and all other values - as true.
Examples:
int i=5;
double d=-2.5;
if(i) Print("i = ",i," and is set to true");
else Print("i = ",i," and is set to false");
if(d) Print("d = ",d," and has the true value");
else Print("d = ",d," and has the false value");
i=0;
if(i) Print("i = ",i," and has the true value");
else Print("i = ",i," and has the false value");
d=0.0;
if(d) Print("d = ",d," and has the true value");
else Print("d = ",d," and has the false value");
//--- Execution results
//
i= 5 and has the true value
//
d= -2.5 and has the true value
//
i= 0 and has the false value
//
d= 0 and has the false value

See also
Boolean Operations, Precedence Rules

file:///C:/Users/wiliams/AppData/Local/Temp/~hh5FF8.htm

03/05/2016

Integer Types

Pgina 9 de 9

MQL4 Reference / Language Basics / Data Types / Integer Types / Enumerations

Enumerations
Data of the enum type belong to a certain limited set of data. Defining the enumeration type:
enum name of enumerable type
{
list of values
};

The list of values is a list of identifiers of named constants separated by commas.


Example:
enum months // enumeration of named constants
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};

After the enumeration is declared, a new integer-valued 4-byte data type appears. Declaration of the
new data type allows the compiler to strictly control types of passed parameters, because enumeration
introduces new named constants. In the above example, the January named constant has the value of 0,
February - 1, December - 11.
Rule: If a certain value is not assigned to a named constant that is a member of the enumeration, its new
value will be formed automatically. If it is the first member of the enumeration, the 0 value will be
assigned to it. For all subsequent members, values will be calculated based on the value of the previous
members by adding one.
Example:
enum intervals // Enumeration of named constants
{
month=1,
// Interval of one month
` two_months, // Two months
quarter,
// Three months - quarter
halfyear=6, // Half a year
year=12,
// Year - 12 months
};

Notes
Unlike C++, the size of the internal representation of the enumerated type in MQL4 is always equal to 4
bytes. That is, sizeof(months) returns the value 4.
Unlike C++, an anonymous enumeration can't be declared in MQL4. That is, a unique name must be
always specified after the enum keyword.
See also
Typecasting

file:///C:/Users/wiliams/AppData/Local/Temp/~hh5FF8.htm

03/05/2016

Anda mungkin juga menyukai