Friday, June 6, 2014

Variable and Using Variable in C++

Declaring Variables to Access and Use Memory
When programming in languages like C++, you define variables to store those values. Defining a variable is quite simple and follows this pattern:
       variable_type variable_name;
               or
       variable_type variable_name = initial_value;

Example:
     int i;   // variable_type: int;  variable_name: i;
     float x=1.25;  //variable_type: float;  variable_name: x; initial_value: 1.25

Declaring and Initializing Multiple Variables of a Type
You could condense the declaration of these multiple variables to one line of code that would

look like this:


Variable Types in C++:

Using Type bool to Store Boolean Values
A sample declaration of an initialized Boolean variable is
     bool male = true;
An expression that evaluates to a Boolean type is

     bool shopping = (UserSelection == “no”);

Using Type char to Store Character Values
     char User_Input = 'A';     // initialized char to ‘A’

Using Type short, int, long to Store Signed Integer Values
     short Age = 22;
     int Number = -70000;
     long LargeNumber = -70000;    //on some platforms, long is an int

     
Using Type float, double to Store Floating-Point Values
     float PI=3.14;
     double x=a/b;

No comments:

Post a Comment