Variables

Variables are an essential part of writing code and are the information that is passed to process/display information. First you must declare it then you can use it. Practice writing in data to the variable names as well as inserting data to see how the compiler handles the data.

Syntax

The most important part of using variables is the name and the type. We can focus on the name first.

1. The first character of variable name must be a character.
2. Underscore can be used as the first character of variable name.
3. Spaces are not allowed in a variable name underscores are.
4. Special characters such as arithmetic operators, #,^ can not be used in a variable.     Reserved words cannot be used as variable names.
5. A variable name declared for one data type cannot be used to declare another data type.
6. C++ language is a case sensitive language. Thus variable names with same spelling but different cases are treated as different variable name e.g. A and a are two different variables.

Types of Variables

Characters:    //using#include <iostream>  using namespace std
You can also read in data from the user using the following:
char variable1;//initializes a character variable
    cin>>variable1;//prompts user to input a character 
    cout<<variable1;//prints that same character to the screen
Note: iostream is a great way to save time handling data!

Integers:

int variable2 = 100;
 printf("%i", variable2);//using libraries; stdio.h, stdlib.h

or

using iostream & namespace std
int variable2 = 100;
cout<<variable3;

You can also read in data from the user using the following:

int variable2;//whole numbers
scanf("%d", variable2);
 printf("%i", variable2);

or
using iostream & using namespace std
int variable3;
cin>>variable3;
cout<<variable3;
                                                                              
float variable3; //decimal number i.e. 3.22, 4.1, 5.2
scanf("%(interger value)", variable3);//insert an integer value where it says integer value this changes how many decimal places it displays
printf("%i", variable3);

or 

using iostream & using namespace std
float variable3;
cin>>variable3;
cout<<variable3;  //cout<<variable3 These are decimal integers. They can be good for handling fractions as well as displaying percentages.

You can also use void. void is the absence of a type.  This might sound odd at first, but it can be a good thing because it handles pointers well (keep this  in mind later) since it can be anything you want (character integer float). Think of it as a blank slate that can be what you want.

Global vs Local Declaration

The last thing to be concerned with is where the integer is placed in the code. You can make it present everywhere in your code if you define it below the libraries, but above the function declaration(globally). Or you can write it in the function and it will only be present in that function(locally).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s