-->




Variables and Data Types in C++ programming

16 comments
Variables
Definition
"Variable is a memory location in C++ Programming language"
Variable are used to store data on memory.

 Why we use variables in C++ language?
Variables are used to store value and that values can be change.
The values of variables can be numeric or alphabet.
There are certain rules on choosing variable name:
  • Variable name can consist of letter, alphabets and start with underscore character.
  • First character of variable should always be alphabet and cannot be digit.
  • Blank spaces are not allowed in variable name.
  • Special characters like #, $ are not allowed.
  • A single variable can only be declared for only 1 data type in a program.
  • As C++ is case sensitive language so if we declare a variable name and one more NAME both are two different variables.
  • C++ has certain keywords which cannot be used for variable name.
  • A variable name can be consist of 31 characters only if we declare a variable more than 1 characters compiler will ignore after 31 characters.
Recommendation in Variable naming:
Always declare variable in readable form e.g. name, sale etc.
Data type in C++ language

 "A data type defines which kind of data will store in variables and also defines memory storage of data"
There are two kinds of data types User defined data types and Standard data types. In the beginning we will discuss only standard data types only.

In C++ language there are four main data types
  1. int
  2. float
  3. double
  4. char
Character data type
  • A keyword char is used for character data type.
  • This data type is used to represents letters or symbols.
  • A character variable occupies 1 byte on memory.
Sensitive case:-
If we chose char data type then we try to store an integer in it like then compiler will give an error if we put single quotes around 5 like that '5'.then compiler will store it as an character not integer.
ASCII VALUES OF CHARACTERS
  • There is an ASCII value for every character in C++ Language.
  • On the basis of these ASCII values characters can be compare, subtract or add.
  • Below chart is showing all the ASCII values of characters.
  • Please do not confuse as your first look at table pay attention to only red characters and decimal values and ignore other values.


Click on Image to view in large size
c++ ASCII values table
Hex, Decimal, ASCII values C++ table














Integer data types

Integers are those values which has no decimal part they can be positive or negative. like 12 or -12.

There are 3 data types for integers
  1. int
  2. short int
  3. long int
int
  • int keyword is used for integers.
  • It takes two bytes in memory.
  • There are two more types of int data type
a) Signed int    or short int (2nd type of integer data type)       
b) Unsigned int or unsigned short int


Signed int

The range of storing value of signed int variable  is -32768 to 32767.It can interact with both positive and negative value.


Unsigned int

This type of integers cannot handle negative values. Its rang is 0 to 65535.
Long int 
  • As name refers it is used to represent larger integers.
  • It takes 4byte in memory.
  • The range of long int is -2147483648 to 2147483648.
float
  • This type of integers contains fractional part.
  • There are two further type of float integer's number.
Below table is Displaying: 
  • Number of bytes or memory taken by numbers
  • Decimal places up to which they can represent value
  • Range
Data type                Bytes        Decimal places    Range  of values


float                          4                        6                3.4E -38 to3.4E+38
double                      8                        15               1.7E - 308 to1.7E +308
long double              10                      19               3.4E -4932 to 304E +4932

16 comments:

  1. superb........thank you very much

    ReplyDelete
  2. thanks for sharing.. I too had written on it..

    ReplyDelete
  3. very good code for prime number yaar

    ReplyDelete
  4. i found this very informative thanks a lot for sharing

    ReplyDelete
  5. thanks for sharing this..it helps a lot...

    ReplyDelete
  6. =can i ask, how to put decimals in programming?

    ReplyDelete
  7. hi i'm a beginner in programming and i'm having trouble inputing string statements as conditions for my if else statement.... can someone please tell me why it doesn't work or how i can make it work.....

    ReplyDelete
    Replies
    1. Adwin Charles.
      As you are a beginner i will suggest you that first explore the string as much as you can like.
      How strings can be declare, how to apply conditions, What functions string class have.
      As a c++ Beginner most of the students faces these kind of errors Just keep the hard work going. If your problem is still not solved you can show your code here..

      Delete
    2. oh sure, thanks for the advice and it worked...at least for a while..... after learning how to input string conditions, i now keep getting the error " no match for 'operator>>' in 'std::cin>>....'" any tips on how to get it fixed???

      Delete
    3. Edwin Charles
      Try This
      #include
      #include
      using namespace std;
      int main()
      {
      string s;
      cin>>s;
      cout<<s;
      return 0;
      }

      Hope it will help you

      Delete