Showing posts with label Variable. Show all posts
Showing posts with label Variable. Show all posts

Tuesday, June 10, 2014

List Keywords You Can't Use as Variable or Constant Names

Some words are reserved by C++, and you can't use them as variable names or constant names. These keywords have special meaning to the C++ compiler.
asm      else          new                       this
auto     enum        operator                throw
bool     explicit     private                   true
break   export      protected               try
case     extern       public                    typedef
catch    false         register                  typeid
char     float          reinterpret_cast     typename
class    for            return                    union
const   friend        short                     unsigned
goto    signed       const_cast             using
 if        sizeof        virtual                    continue
void    default       inline                     static 
delete  int             static_cast             volatile
do       long          struct                     wchar_t
double mutable    switch                    while
dynamic_cast       namespace            template

Monday, June 9, 2014

Examples: How to use variables in C++


Example 1: Integer Variables
Syntax: int variable;       //comment

#include <iostream>
using namespace std;
int term;  // term used in two expressions
int main(){
term = 4 * 7; 
cout <<"Twice " << term << " is "<< 2*term <<endl; 
cout <<"Three times " << term << " is " << 3*term <<endl; 
return 0;
}

Example 2: Floating Point Variables
Syntax:  float variable;     //comment

#include <iostream>
#include <conio.h>
using namespace std;
int i;    // an integer variable
float f; // a floating point number 
int main(){
f = 1.0 / 2.0;          // assign floating 0.5 
cout<<" f= "<<f<<endl;
i = 1 / 3;              // assign integer 0 
cout<<" i= "<<i<<endl;
f = (1 / 2) + (1 / 2);  // assign floating 0.0 
cout<<" f= "<<f<<endl;
f = 3.0 / 2.0;          // assign floating 1.5 
cout<<" f= "<<f<<endl;
i = f;                  // assign integer 1 
cout<<" i= "<<i<<endl;
return 0;
}

Example 3: Characters Variables
Syntax:  char variable;   //comment

#include <iostream>
#include <conio.h>
using namespace std;
char c1;  // first character 
char c2;  // second character 
char c3;  // third character 
int main(){
c1 = 'X'; 
c2 = 'Y'; 
c3 = 'Z'; 
cout << c1 << c2 << c3 << " reversed is "<<c3 << c2 << c1 << "\n";
return 0;
}

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;