WEBINAR: On-demand webcast
How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >
Variable declaration and manipulation with various data types is considered to be the most essential task in any of high-level programming language. In this article, you'll learn how to create and use variables, the diverse fundamental data types of C++/CLI, as well the importance and usage of constants.
Fundamental Data Types
Variables are spots in memory where data of any type can be temporarily stored for the application's use. They have a name, a type, and a value as syntax. The value of the variable is not fixed at all; in fact, the value can be changed during the execution of the application. In "classic" C++, the sizes of the basic data types are not fixed. But in the .NET version of C++/CLI, the sizes of the basic types are fixed. C++/CLI has a built-in set of data types, as outlined in the following table.
|char, __int8||A single-byte integral type, often used to hold ASCII values||Values can range from -128 to +127.|
|short, __int16||An integral type; stores whole numbers||Values can range from -32,768 to +32,767. An unsigned short can range from 0 to 65,535.|
|int, __int32||An integral type; stores whole numbers||Values can range from -2,147,483,648 to 2,147,483,647. An unsigned int can range from 0 to 4,294,967,295.|
|long||An integral type like int, except on many compilers, it's twice the size||In Microsoft Visual C++, the long is the same size as the int. Therefore, it can only store the same range of values.|
|long long, __int64||An integral type||Values can range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.|
|float||Stores floating-point numbers; for example, 3.7||In Visual C++, the float stores up to seven decimal places. The range of values is 3.4E+/-38.|
|double||Stores floating-point numbers like the float but with greater precision and more accuracy||The double can store up to 15 decimal places. The range of values is 1.7E+/-308|
|wchar_t||A wide character or multi-byte character type|
|Bool||A Boolean type contains values either true or false|
From these built-in types, you can construct other types. Or, you can construct user-defined types by creating data structures and classes as follows:
- Array types
- Reference types
- Handle types ( int^)
Variable declaration in CLI allocates memory to store the variable of that type and to associate the name of the variable with that memory location. Variables can be declared by many ways in C++/CLI. In addition, a variable name can be any combination of letters, numbers, and underscores, as long as the first character of the variable name is a letter or an underscore. However, a simple declaration consists of a type, followed by one or more variable names separated by commas and terminated by a semicolon, as shown below:
int itemCount; string str; char val;
You can assign a value to a variable by using the assignment operator = (the equal sign). The value on the right side of the operator is stored in the variable, which is on the left side. When assigning a value to a variable, the value must belong to the same type as the variable. Moreover, you can place an initializer after the variable name to initialize it with a default value. It is not mandatory that the qualifier and the initializer appear in the declaration, but the base type and variable name must be present, as follows:
int itemCount=5; char val='x';
In addition, you also can declare multiple variables of the same type in the same statement simply by separating them with commas, as shown in the following:
int x = 5, y, z = 1;
You use handles (^, caret character). A handle contains the address of a variable that will be updated by the runtime. Handle variables are declared to be data-type specific, although a handle contains an address and therefore can store a memory address of any data type. A handle variable is declared in the same way as the data-type variable, but the handle operator ^ must be pre-appended to the variable name, as follows:
int ^itemCount; Car ^obj;
Further, you can create an object dynamically and obtain a handle to it by using the gcnew operator, as in the following:
Car ^obj=gcnew Car();
Constants are named data-storage locations and are very similar to variables, indeed. However, the value of a constant can't be altered after having been declared. Instead, it has to be initialized when it's created and can't be assigned a new value later. C++/CLI typically has two kinds of constants: literal and symbolic. A literal constant is simply a value typed into the application, whereas the symbolic constant is represented by a name. You can define a constant by using the keyword const and the variable must be initialized. After declaration, you freely can use the constant name anywhere in the program like a variable of that type, as follows:
Const double pi=3.14;
In C++/CLI, fixed values of any kind are referred to as literals. A literal is a value of a specific type as shown in the following table.
|wchar_t||L'9', L'*' , L'A'|
In this article, you have explored the essentials of programming in C++/CLI. By the end of it, you'll be able to understand the various data types involved in this programming language and how to define and initialize a variable. Moreover, you have learned the importance of literals as well.