Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

Variable, Constants and Data types in C++

 

Variable, Constants and Data types in C++

In this C++ tutorial, you will learn about variable, constants and data types in C++, rules for defining variable name, short int, int, long int, float, double, long double, char, bool, declaring variables and constants.



Variables

A variable is the storage location in memory that is stored by its value. A variable is identified or denoted by a variable name. The variable name is a sequence of one or more letters, digits or underscore, for example: character _


Rules for defining variable name:

  • A variable name can have one or more letters or digits or underscore for example character _.
    .
  • White space, punctuation symbols or other characters are not permitted to denote variable name. .
  • A variable name must begin with a letter.
    .
  • Variable names cannot be keywords or any reserved words of the C++ programming language.
    .
  • C++ is a case-sensitive language. Variable names written in capital letters differ from variable names with the same name but written in small letters. For example, the variable name EXFORSYS differs from the variable name exforsys.

As previously explained, a variable is the storage location in memory that is stored by variable value. The amount of memory allocated or occupied by each variable differs as per the data stored. The amount of memory used to store a single character is different from that of storing a single integer. A variable must be declared for the specific data type.


Data Types

Below is a list of the most commonly used Data Types in C++ programming language:


  • short int
  • int
  • long int
  • float
  • double
  • long double
  • char
  • bool

short int : This data type is used to represent short integer.


int: This data type is used to represent integer.

long int: This data type is used to represent long integer.

float: This data type is used to represent floating point number.


double: This data type is used to represent double precision floating point number.

long double: This data type is used to represent double precision floating point number.

char: This data type is used to represent a single character.

bool: This data type is used to represent boolean value. It can take one of two values: True or False.


Using variable names and data type, we shall now learn how to declare variables.


Declaring Variables:

In order for a variable to be used in C++ programming language, the variable must first be declared. The syntax for declaring variable names is


data type variable name;


The date type can be int or float or any of the data types listed above. A variable name is given based on the rules for defining variable name (refer above rules).


Example:


int a;


This declares a variable name a of type int.

If there exists more than one variable of the same type, such variables can be represented by separating variable names using comma.

For instance


int x,y,z


This declares 3 variables x, y and z all of data type int.

The data type using integers (int, short int, long int) are further assigned a value of signed or unsigned. Signed integers signify positive and negative number value. Unsigned integers signify only positive numbers or zero.


For example it is declared as


unsigned short int a;
signed int z;


By default, unspecified integers signify a signed integer.

For example:


int a;


is declared a signed integer


It is possible to initialize values to variables:


data type variable name = value;


Example:


int a=0;
int b=5;


Constants

Constants have fixed value. Constants, like variables, contain data type. Integer constants are represented as decimal notation, octal notation, and hexadecimal notation. Decimal notation is represented with a number. Octal notation is represented with the number preceded by a zero character. A hexadecimal number is preceded with the characters 0x.

Example


80 represent decimal
0115 represent octal
0x167 represent hexadecimal


By default, the integer constant is represented with a number.


The unsigned integer constant is represented with an appended character u. The long integer constant is represented with character l.

Example:


78 represent int
85u present unsigned int
78l represent long


Floating point constants are numbers with decimal point and/or exponent.

Example


2.1567
4.02e24


These examples are valid floating point constants.


Floating point constants can be represented with f for floating and l for double precision floating point numbers.



Character constants have single character presented between single quotes.


Example


‘c’
‘a’


are all character constants.


Strings are sequences of characters signifying string constants. These sequence of characters are represented between double quotes.

Example:


“Exforsys Training”


is an example of string constant.



Read Next: C++ Objects and Classes



 

 

Comments


Rod Nikko said:

  very nice and very complete info. :) i learn lots of new thing in computer :D
August 15, 2006, 12:24 am

farhi khan said:

  hi,
the provided here is great. it had helped me alot..
thanks exforsys :)
April 1, 2007, 9:23 am

nagasatish said:

  constants topic is not clear.but remaing concepts are very good and easy to understand
May 15, 2007, 1:14 am

devik said:

  The topics are very clear to understand
September 14, 2007, 4:59 am

anum said:

  hi, please clear the topic of variables. because i want to know about the difference b/w variable and data types variables. thanx if possible
October 10, 2007, 10:39 pm

rajankanth said:

  smply superb
January 28, 2009, 5:02 am

Umar Salim said:

  Easy to understand
February 5, 2009, 4:44 am

sumiya said:

  please explain variable data types in detail.......
February 10, 2009, 3:26 am

vnd said:

  variable is a name for holding data value and is named by a programmer according to language convention, data type tells that variable what kind of value it will have and is restricted to it.
eg int total_book = 5
float price_per_book=200.50
here total_book is a variable with data type int (telling variable that it can have only non decimal number and to a limit 2^31)
June 17, 2009, 8:58 am

Janani said:

  When considering about data types , it is very important to explain about the range(limits) of databytes.

eg:- for int , the range is from -327678 to +32768

if we assign value to integer variable below 0f -32768 or above of 32768, the value will be roll back. or ends in unexpected result
September 27, 2009, 4:01 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape