![]() When a computer comes up (boots), part of this memory is filled with some values that primary applications, such as the operating system, need. Since the other parts of the memory are not used, they may be filled with garbage, that is, insignificant values you can't use because they mean nothing: ![]() The memory (RAM) is huge enough to contain various values. The particularity of this memory is that it is temporary. In other words, it holds its values for a while and looses them when they are not needed anymore (an example is when the computer shuts down, the RAM is emptied). Normally, the operating system is in charge of the management of this whole memory, including when to put ("push") things in it and when to remove ("pop") things from it. This memory is also made available to you so that, while your application is "running", you can "put" things in it. To better manage its memory, the computer sets aside an amount of memory space for programs like yours so that, when your application runs, it can use that memory to put values in it. This area of memory is called the stack. As you may guess from its name, values are entered ("pushed") in it from a side, like entering a tunnel. Another particularity of this scenario is that the values are removed ("popped") from another side: ![]() Keep in mind that the operating system (OS) removes any value when it (the OS) judges or finds out that the value(s) is(are) not used or needed anymore. When your application runs, it will use (part of) this memory space as necessary. One way you can use it is to put values in the space as you judge necessary.
In order to store these values in the memory, you must tell the computer how much space each value will need. This is because the compiler cannot predict what your program will need. When specifying the amount of space, the compiler will reserve an amount of space for each value you specify, in the stack memory. To locate that memory, you must give it a name. The name is not stored as a value; it only allows you to be able to refer to the space memory that was reserved for your variable. Reserving an area of memory is referred to as declaring a variable. There are rules you must follow when doing this. The general formula used to declare a variable in C is: TypeOfVariable VariableName; When you ask the compiler to reserve an amount of memory space for your variable, you must tell it how much memory the variable will need. This is because different values are meant to do different things. For example, while a number can be used to hold the salary of an employee, a word or a group of words is necessary to hold the name of an employee. The second piece of information you must specify, as mentioned already, is a name that you will use to refer to the memory that was reserved for a particular variable. If you want to declare more than one variable of the same type, you can declare each with the type, its name, and its semi-colon. The formula to follow would be: TypeOfVariable Variable1; TypeOfVariable Variable2; TypeOfVariable Variable_n; An alternative is to use one data type followed by the names of variables that would share this type. The names of variables must be separated by commas. The formula to follow would be: TypeOfVariable Variable1, Variable2, Variable_n; If you want to declare different variables that use different data types, you can declare each with the type, its name, and its semi-colon, on the same line. The formula to follow would be: TypeOfVariable1 Variable1; TypeOfVariable2 Variable2; TypeOfVariable3 Variable3; A better alternative is to declare each variable on its own line. This makes the program easier to read. The formula to follow would be: TypeOfVariable1 Variable1; TypeOfVariable2 Variable2; TypeOfVariable3 Variable3;
A name can consist of one word such as country. A name could also be a combination of more than one word, such as firstname or dateofbirth. The C++ and the C++/CLI languages have a list of words reserved for its own use and you must not use any of these words to name your objects or functions. The reserved words are:
Avoid starting the name of a variable with two underscores; sometimes, the compiler would think that you are trying to use one of the words reserved for the compiler. C++ is case-sensitive; this means that CASE, Case, case, and Case are four completely different words. To make your programming experience easier and personal, you can add your own rules to those above. Some (most) companies also adopt a naming convention throughout their documentation. Throughout this book, a name will:
When you declare a variable, as mentioned already, the compiler reserves a space in memory for that variable in the stack. In most languages, that space is left empty until you put a value in it. Initialization is a technique of putting a value into the space memory of a variable. We also state that variable is assigned a value. To control the behavior of your program, you can assign the desired value to a variable when you declare it. That is, you can initialize a variable when declaring it. There are two main techniques used to initialize a variable. You can use the equal symbol (also called the assignment operator) to initialize a variable. To do that, after typing the name of the variable, type = followed by the desired value. The formula used would be: TypeOfVariable VariableName = InitialValue; Another technique of initializing a variable is by using parentheses. The syntax is: TypeOfVariable VariableName(InitialValue);
If many different variables are using the same data type, you can declare them on the same line, separating two with a comma, except the last one that would end with a semi-colon. The formula to follow is: DataType Variable1, Variable2, Variable3;
An integer is a natural number typically used to count items. An integer is considered a "whole number" because it doesn't display a decimal fraction. A number can be expressed as being positive or negative. When a number is written like 248, it is considered positive. Such a number is greater than 0. To specify that a number is negative, you must type - to its left. An example would be -248. When declaring a variable that would hold a number, you will have the option of specifying that the variable can hold a positive or a negative value. A variable is referred to as signed when it can hold either a positive or a negative number. The positive sign can be set by either not typing any sign to its left, as in 248, or by adding + to its left, as in +248. A variable is referred to as unsigned if it must hold only a positive number. If it is given a negative number, in the best case scenario, the compiler would not do anything. In the bad case scenario, the value would be unpredictable. With some compilers, the program would crash. The most fundamental number in C++/CLI is represented by the Byte data type (this data type is not formally used in C++). A Byte is an unsigned number whose value ranges from 0 to 255. To declare such a variable, you can use the Byte keyword. The Byte data type is defined in the System namespace. Here is an example: using namespace System;
int main()
{
Byte Age = 242;
Console::WriteLine(Age);
return 0;
}
This would produce: 242 Press any key to continue If you want to use a small number but whose variable can hold either positive or negative values, you can declare it using the SByte data type. A variable declared with SByte can hold a value between -127 and 128, not more, not less. The SByte data type is defined in the System namespace. Here is an example: using namespace System;
int main()
{
SByte Age = 24;
Console::WriteLine(Age);
return 0;
}
This would produce: 24 Press any key to continue
If you need to represent a number that is a little higher than the Byte can hold, you can declare it using the short keyword. Here is an example: using namespace System;
int main()
{
short Pages = 424;
Console::WriteLine(Pages);
return 0;
}
A short variable can hold a natural number whose value ranges from -32768 to 32767. Based on this, it is important to note that a Byte value can fit in a short. Therefore, it is normal to declare as short a variable that would hold even small numbers such as people's ages. When assigning a value to the variable, you can use a number in that range. If you assign a value out of that range, you would receive a warning and the number would be "truncated". That is, the number would be converted to the value of the other extreme. Consider the following program:
using namespace System;
int main()
{
short Number = 32769;
Console::WriteLine(Number );
Console::WriteLine();
return 0;
}
This would produce the following warning: warning C4309: 'initializing' : truncation of constant value The program would produce: -32767 Press any key to continue If you want to enforce the idea that the variable is signed, you can type the signed keyword before short. Here is an example:
using namespace System;
int main()
{
signed short Number = -326;
Console::WriteLine(Number );
Console::WriteLine();
return 0;
}
If you want the variable to hold only positive numbers, you can type the unsigned keyword to the left of short and make sure you omit the signed keyword. When using the unsigned keyword, you can store numbers that range from 0 to 65535. If you assign either a negative number or a number higher than 65535, the number would be truncated. This time, the truncation is done differently but the result is not as intended. Here is an example:
using namespace System;
int main()
{
unsigned short Number = -326;
Console::WriteLine(Number );
return 0;
}
After a warning, this would produce: 65210 Press any key to continue Besides the short keyword, to declare a variable for a relatively small number, you can use the Int16 data type. The Int16 data type is defined in the System namespace. Its variable can hold values in the same range as the short. Here is an example: using namespace System;
int main()
{
System::Int16 Number = -326;
Console::WriteLine(Number );
Console::WriteLine();
return 0;
}
Because an Int16 variable can hold negative values, if you want to use only positive values, you can declare the variable using the UInt16 data type (The U stands for "unsigned"). The UInt16 data type is defined in the System namespace. Here is an example: using namespace System;
int main()
{
System::UInt16 Number = 60326;
Console::WriteLine(Number );
Console::WriteLine();
return 0;
}
This would produce: 60326 Press any key to continue In some cases you may need a variable that hold a number larger than the short or the Int16 can carry. To declare such a variable, you can use the int keyword. The int data type is used for a variable whose value can range from 2,147,483,648 to 2,147,484,647. Here is an example:
Compiled, the program would produce:
It is important to note that, based on its range of values, an int variable can hold the same values as the Byte, the short, or the Int16. Therefore, it is perfectly normal and sometimes preferable to use int for a variable intended to hold natural numbers. By default, an int declared variable can hold either positive or negative values. You can still enforced this by typing the signed keyword to the left of int: using namespace System;
int main()
{
signed int Number = 602;
Console::WriteLine(Number);
Console::WriteLine();
return 0;
}
If you want the variable to hold only positive numbers, you can type the unsigned keyword to its left and omit the signed. Here is an example: using namespace System;
int main()
{
unsigned int Number = 46082;
Console::WriteLine(Number);
return 0;
}
Besides the int keyword, you can use the Int32 data type. An Int32 holds the same range of values as the int. The Int32 data type is defined in the System namespace. Here is an example: using namespace System;
int main()
{
System::Int32 Number = 46802;
Console::WriteLine(Number);
Console::WriteLine();
return 0;
}
To specify that the variable must hold only positive numbers, besides unsigned int, you can use the UInt32 data type to declare the variable. The UInt32 data type is defined in the System namespace. Here is an example:
While the int and the Int32 data types can hold significantly large numbers, you may still need a variable that can hold very large numbers. Such a variable can be declared using the long data type. Such a variable can hold a number that ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Based on this, it is important to note that a long variable can hold a short, an int, an Int16 or an Int32 variable but, unlike the int and the Int32 that are more commonly used, the long is more appropriate if you know that the variable will really need to carry very large numbers. Otherwise, the long can use significant memory. Here is an example: using namespace System;
int main()
{
long Number = 46082;
Console::WriteLine(Number);
return 0;
}
This would produce: 46082 Press any key to continue When declaring a variable as long, it can hold either positive or negative numbers. You can still enforce this by declaring the variable as signed long.
After declaring an integer variable, you can initialize it with an appropriate value. As far as the value is concerned, you have two main alternatives. You can initialize an integral variable with a decimal value as we have done so far. Alternatively, you can initialize an integral variable with a hexadecimal value.
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||
| Previous | Copyright © 2006-2009 FunctionX, Inc. | Next |
|
|
||