< .Net Framework Class Library 8 | Main | .Net Type, Variable & Operator 2 >



Data Types, Variables and Operators 1




This is the starting point of the learning C++ .Net. We start from the basic after knowing and learning the .Net Framework, Managed Extension of C++ and the C++ .NET old syntax. The discussion and the codes used supposed to be based on the new C++ .NET. The following are all the topics in this module.


  1. What Is a Variable?

  2. Types

  3. The Fundamental Data Types

  4. .NET Framework Equivalents to C++ Native Types

  5. Declaring a Variable

  6. Variable Naming

  7. Declaring Multiple Variables

  8. Assigning Values to Variables

  9. Assignment Conversions


  1. Arrays

  2. Pointers

  3. References

  4. Constants

  5. Enumerations

  6. Typedefs

  7. Adding Member Variables to Classes

  8. The .NET Framework String Class

  9. Operators and Expressions

  10. Assignment Operators

  11. Arithmetic Operators

  12. Relational and Logical Operators

  13. Bitwise Operators

  14. The Ternary Operator

  15. The sizeof Operator

  16. Type Casting

  17. Operator Precedence and Associativity

  18. Derived Types

  19. C++ Classes

  20. C++ Structures

  21. C++ Unions

  22. Defining Class Types

  23. Overview of C++ Classes

  24. Class Names

  25. Class Members

  26. Member Functions

  27. Static Data Members

  28. Unions

  29. Declaring a Union

  30. Using a Union

  31. C++ Bit Fields

  32. Nested Class Declarations

  33. friend

  34. Type Names in Class Scope

  35. Integer Limits

  36. Floating Limits

  37. C++  .NET  Types Program Examples


What Is a Variable?


Variables are locations in memory where data can be temporarily stored for use by the program. They have a name, a type, and a value. The value of the variable can be changed during the execution of the program, hence, the name variable. Before a variable can be used, it must be declared that means its type has to be specified, and it has to be given a name. The type of a variable defines the allowable range of values that the type can hold and the operations that can be performed on the variable.




C++ supports three kinds of object types:


  1. Fundamental types are built into the language (such as int, float, or double). Instances of these fundamental types are often called "variables".

  2. Derived types are new types derived from built-in types.

  3. Class types are new types created by combining existing types such as classes, structures, and unions.





The Fundamental Data Types


Fundamental types in C++ are divided into three categories: integral, floating, and void. Integral types are capable of handling whole numbers. Floating types are capable of specifying values that may have fractional parts. The void type describes an empty set of values. No variable of type void can be specified, it is used primarily to declare functions that return no values or to declare generic pointers to untyped or arbitrarily typed data. Any expression can be explicitly converted or cast to type void. However, such expressions are restricted to the following uses:

  1. An expression statement.

  2. The left operand of the comma operator.

  3. The second or third operand of the conditional operator (? :).


The following table explains the restrictions on type sizes of the fundamental types. These restrictions are independent of the Microsoft implementation.







Type char is an integral type that usually contains members of the execution character set, in Microsoft C++, this is ASCII. The C++ compiler treats variables of type char, signed char, and unsigned char as having different types. Variables of type char are promoted to int as if they are type signed char by default, unless the /J compilation option is used. In this case they are treated as type unsigned char and are promoted to int without sign extension.


Type bool is an integral type that can have one of the two values true or false. Its size is unspecified.


Type short int (or simply short) is an integral type that is larger than or equal to the size of type char, and shorter than or equal to the size of type int. Objects of type short can be declared as signed short or unsigned short. signed short is a synonym for short.


Type int is an integral type that is larger than or equal to the size of type short int, and shorter than or equal to the size of type long. Objects of type int can be declared as signed int or unsigned int. signed int is a synonym for int.


Sized integer, where n is the size, in bits, of the integer variable. The value of n can be 8, 16, 32, or 64. (__intn is a Microsoft-specific keyword.)


Type long (or long int) is an integral type that is larger than or equal to the size of type int. Objects of type long can be declared as signed long or unsigned long. signed long is a synonym for long.

long long

Larger than an unsigned long. Objects of type long long can be declared as signed long long or unsigned long long. signed long long is a synonym for long long.



Type float is the smallest floating type.


Type double is a floating type that is larger than or equal to type float, but shorter than or equal to the size of type long double.*

long double1

Type long double is a floating type that is equal to type double.



A variable of __wchar_t designates a wide-character or multibyte character type. By default, wchar_t is a native type but you can use /Zc:wchar_t - to make wchar_t a typedef for unsigned short. Use the L prefix before a character or string constant to designate the wide-character-type constant.


Table 1

* - The representation of long double and double is identical. However, long double and double are separate types.

The following table lists the amount of storage required for fundamental types in Microsoft C++.  It is Microsoft specific.





1 byte

char, unsigned char, signed char

1 byte

short, unsigned short

2 bytes

int, unsigned int

4 bytes


8, 16, 32, 64, or 128 bits depending on the value of n. __intn is Microsoft-specific.

long, unsigned long

4 bytes


4 bytes


8 bytes

long double

8 bytes

long long

Equivalent to __int64.


Table 2


C++ has a built-in set of data types, as shown in the following table. For 32-bit and 64-bit compilers, Microsoft Visual C++ recognizes the types shown in the table below.


Type Name


Other Names

Range of Values




–2,147,483,648 to 2,147,483,647



char, signed char

–128 to 127



short, short int, signed short int

–32,768 to 32,767



signed, signed int, int

–2,147,483,648 to 2,147,483,647



long long, signed long long

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807




false or true




–128 to 127



short int, signed short int

–32,768 to 32,767



long int, signed long int

–2,147,483,648 to 2,147,483,647

long long


none (but equivalent to __int64)

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807




Same as int




3.4E +/- 38 (7 digits)




1.7E +/- 308 (15 digits)

long double

same as double


same as double




0 to 65,535


Table 3


Note that the following types also have unsigned forms.

  1. int (unsigned int)

  2. __int8 (unsigned __int8)

  3. __int16 (unsigned __in16)

  4. __int32 (unsigned __in32)

  5. __int64 (unsigned __in64)

  6. char (unsigned char)

  7. short (unsigned short)

  8. long (unsigned long)

  9. long long (unsigned long long)


A variable of __wchar_t designates a wide-character or multibyte character type. By default wchar_t is a typedef for unsigned short. As mentioned before, use the L prefix before a character or string constant to designate the wide-character-type constant. When compiling with /Zc:wchar_t or /Za, the compiler can distinguish between an unsigned short and wchar_t for function overload purposes. Signed and unsigned are modifiers that can be used with any integral type except bool. The char type is signed by default, but you can specify /J (compiler option) to make it unsigned by default. The int and unsigned int types have the size of the system word: four bytes. However, portable code should not depend on the size of int. Microsoft C/C++ also features support for sized integer types.





.NET Framework Equivalents to C++ Native Types


The following table shows the keywords for built-in C++ types, which are aliases of predefined types in the System namespace.


C++ type

.NET Framework type



signed char


unsigned char




double and long double




int, signed int, long, and signed long


unsigned int and unsigned long


__int64 and signed __int64


unsigned __int64


short and signed short


unsigned short





Table 4


Take note that in C++ .Net coding the dot (.) will be replaced by scope operator (::) as illustrated in the program examples provided at the end of this module. From these built-in types, you can construct other types, as we’ll see later in this module such as:

  1. Pointer types, for example, int*

  2. Array types, for example, int[]

  3. Reference types, for example, double&


Or you can construct user-defined types by creating data structures and classes that you will learn in other modules.


Declaring a Variable


As mentioned earlier, variables have to be declared before they can be used. A simple declaration consists of a type, followed by one or more variable names separated by commas and terminated by a semicolon, as shown in this example:

int      nPrimeNumber;

double   x, y, z;

Each variable can be given a qualifier before the type (for example, unsigned). You can also place an initializer after the variable name to give it an initial value (for example, int i = 0). The qualifier and the initializer are optional and don’t have to appear in the declaration, but the base type and variable name must be present. The declaration is terminated by a semicolon.

[qualifier] type name [initializer];

For examples:

unsigned int i;      // An unsigned integer variable i, note the

                     // qualifier limiting the variable to positive numbers.

long lSalary = 0;    // A long variable initialized to zero.

double y;            // A double variable without qualifier or initializer

Declaring the variable enables the compiler to:

  1. Allocate enough memory to store the variable of that type and to associate the name of the variable with that memory location.

  2. Reserve the name of the variable to prevent it from being used by other variables within the same scope. Scope refers to that part of the code where a variable is visible, where it can be used.

  3. Ensure that the variable is used in a way consistent with its type. For example, if you have declared a variable as a char, you can’t store the value 3.7 in it.


Variable Naming


A C++ 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. Although C++ does not place any restrictions on your choice of variable names, you should use meaningful variable names and be consistent in your naming conventions to increase the readability of your code. C++ is case- sensitive, meaning uppercase and lowercase letters are considered different; for example, myvariable and myVariable are two separate variables. However, it’s not a good idea to differentiate variables solely on the basis of case; doing so could lead to confusion. It would be easy to type a letter in the wrong case and end up using a completely wrong variable!

It’s not a good idea to create identifiers that begin with two underscores or an underscore followed by a capital letter (for example, _myA). Microsoft uses this naming convention to specify macros and Microsoft-specific keywords, so starting your variables with these combinations could lead to name conflicts.


Declaring Multiple Variables


You can declare several variables of the same type in the same statement simply by separating them with commas:

int x = 10, y, z = 11;

This statement creates three integers called x, y, and z. The first integer is initialized to 10 and the third to 11, while the second is not initialized.





Assigning Values to Variables


You assign a value to a variable by using the assignment operator = (an equal sign). The value on the right side of the operator is stored in the variable on the left side. When assigning a value to a variable, the value must belong to the same type as the variable, be a type for which C++ will perform an assignment conversion (such as between float and integral types), or be explicitly cast to the correct type.


Assignment Conversions


Assignment conversions occur when variables on opposite sides of an equal sign are of different types and the compiler can convert between the two types without any possible loss of data. For instance, assigning an integer to a double will result in an assignment conversion because all the compiler has to do is to add .0 to the integer to make the conversion. You might occasionally need to tell the compiler to perform a conversion that it otherwise wouldn’t do. For example, dividing two integers will result in an integer result: if you want a floating point result, you can tell the compiler to convert one of the values to a double, like this:

double result = double(640) / 480;

You give the name of the type to convert to, followed by the value in parentheses. This process is called casting, and it can be rather dangerous because you’re telling the compiler to apply a conversion, and you’d better be sure you’re correct.

int x;

float y;

double z;

x = 1;

z = x;

y = 3.56;

// Assignment conversion from float to int results in loss of data.

//The integer 3 is stored in the variable x.

x = y;

The compiler will generate the warning “C4244: '=' conversion from 'float' to 'int' possible loss of data”. The reason for this is that the assignment to an integer will lose the fractional part, so 3.56 will be truncated to 3.




Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8 | Part 9 | Part 10



< .Net Framework Class Library 8 | Main | .Net Type, Variable & Operator 2 >