An Engineer's Literature

Language spoken by engineers!

Lesson4: Data Types and Declaration of Variables

Picture13

Variables

Just like in our basic mathematics, we know that variables can hold any numerical values.  But unlike it, C++ variables can also hold values that are numerical such as word, characters, combination of characters and numbers.  Here are the rules in naming variables.  It is important that we follow these:

  1. In naming variables, variable names must always start with a letter otherwise, it should start with an underscore.
  2. Unlike with other programming language, C++ variable names are case sensitive.  So the variable ‘age’ is different from ‘Age’ and also with ‘agE’.
  3. Special characters and spaces are not also allowed.  Underscores are usually used to substitute spaces.
  4. It is advisable to name a variable for what it stands for.  It is quite hard to deal with numerous variables that the names represent what it stands for.
  5. C++ reserved words should not also be used in naming variables.

Declaring Variables

From the example above, we can see how simple it is to declare a variable.  We just have to enter its data type and the variable name after it.  Of course, semi-colon should always be added once the declaration is done.  It is also possible to declare multiple variables at once.

Example:          <datatype> <Varname>;

int myAge;

In declaring multiple variables at once:

int myAge, myAllowance;

Variables, that do not have values initially and once asked in the program without assigning value to it, will display different values in the system.  So it is ideal to assign an initial value for it.  If the initial value isn’t known yet, the value of zero will do.

Example:

Picture14

C++ Basic Data Types

  • char

char data type is used to hold individual characters.  char variables should always be enclosed in single quote.  Letters of the alphabet, numbers and special characters (+, -, %, $, etc.) are allowed.

Example

‘Y’, ’1′, ‘+’

  • bool

bool data type represents Boolean (logical) data.  The only values for this data type are true and false.

  • int

int data type can hold whole numbers only.  Once a decimal is entered as a value for it, it only takes the whole number.  Positive (+) and negative (-) signs can also be added.

  • float

float data types also hold numerical values but unlike int, it can have a number with decimal places.  It can also be positive or negative.

  • string

string data types are different from the other data types.  This data type requires another library called <string>.  Assignment of values to it is the same to the other data types but this requires a call for the library <string>.


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 )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.