Data Types


Variable Initialization

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);

Introduction to Data Types

We have mentioned that the syntax of declaring a variable was:

TypeOfVariable VariableName;

The TypeOfVariable factor lets the compiler know the amount of memory that will be needed for the variable. This TypeOfVariable is commonly called the data type. As we will find out, there are various data types available in C++.

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;

Practical LearningPractical Learning: Introducing Data Types Applications

  1. To start a new program, launch Microsoft Visual C++ 2005
  2. On the main menu, click File -> New -> Project...
  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project
  4. In the Name box, replace the string with RealEstate2 and click OK
  5. To create a source file, on the main menu, click Project -> Add New Item...
  6. In the Templates list, click Source File (.cpp)
  7. In the New box, type Exercise and click Add
  8. In the empty file, type:
    using namespace System;
    int main()
        Console::WriteLine("=//= Altair Realty =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        return 0;
  9. To execute the application, on the main menu, click Debug -> Start Without Debugging
  10. Click Yes

Previous Copyright © 2006-2016, FunctionX, Inc. Next