Now, lets understand the concepts of variable in c++
VARIABLE : It is a container that is used by c++ to store values.
The above statement is only a abstract view and actually a variable is a space allocated(in bytes having an address) by c++ in the main memory of the machine running the code
int a=12;
*address is the position of the allocated block with respect to the first byte in the memory
types of variable : This specifies the characteristics of the variable you are using i.e.
1> what type of value it can store(real or integer)??
2> what is the range of value it can store??
the following is a snippet from http://www.cplusplus.com/ showing the types and ranges of variables
now lets understand a bit about signed and unsigned variables
our machines represent negative numbers by a different method than we usually do(with a minus '-' sign)
a signed variable is the one which is capable of holding values with minus sign(it uses the first bit to represent sign).
In a modern computer a int is usually 4 bytes that is 4*8 bits = 32 bits
now the first bit is reserved for sign representation so the remaining 31 bit are used resulting in
2^31-1('^' represents exponentiation ) = 2,147,483,647 (at max) and -2,147,483,647(minimum)
but the unsigned version uses all the 32 bits for storing values
2^32 -1 = 4,294,967,295(at max) and 0(minimum)
(
probably some of you may be thinking why 2^31-1. It is just a bit of simple math(more specifically series)
the maximum value results when all the bits of the binary representation are 1
so converting that value in decimal :
2^0 + 2^1 + .................................... + 2^ 30 = 1.(2^31 - 1) / (2-1) = 2^31 - 1 (GP series)
my head is already starting to hurt!!!!!!
)
for larger range you can use long long 8 bits and hence 64 bits (do the maths bro!!!!!)
float and double is used for representing real numbers(numbers having decimal poriton)
for eg.
float a = 3.12;
in scientific notation same thing can be written as
float a = 312e-2;
here 312 is base part and -2 is the exponent part (whattt!!!!!)
3.12 can be written as 312e-2 i.e. 312 * 10^(-2) => 312 / 10^(2) => 312 / 100 => 3.12 (hmmm!!!)
i am telling the above because this is how a real number is stored in computer memory
the [base part] and the [exponent part]
and the number of digits after the decimal point represent the precession
lets see a program :
Now let's have a look at the various stages of a variable's life
Now let's elaborate more on the runtime initialization :
the above program does two things :
take input from the user durnig runtime
and
display the output given by the user on the screen
after taking a look at the code you will realize that i have used "cin" to take input from the user
cin : It is a stream object which creates a connection between the keyboard and the program. So, that the program can store the input given from the keyboard in the variable named 'a'.
SYNTAX :
cin>>(variable_name);
and for displaying the value of the variable on screen. We use ouptut stream object "cout".
SYNTAX :
for printing the value of a variable
cout<<(variable_name);
for printing a statement eg. Hello World!!
cout<<"Hello World!!!";
remember to keep these in the double quotes
here's another simple program using cin and cout
1> cascading varibles with cin : when we need input for multiple variables we can write them in a single cin
statement without writting cin again and again
SYNTAX:
cin>>var1>>var2>>............................>>varn;
var1, var2,........, varn are variables
and remember that it is necessary to give the input in the order in which you have written the variables.
eg:
cin>>a>>b;
you give : 1 2
then '1' is assigned to 'a' and '2' is assigned to 'b'
2> cascading varibles with cout : when we need to output multiple variables we can write them in a single cout statement without writting cout again and again
SYNTAX:
cout<<var1<<var2<<............................<<varn;
var1, var2,........, varn are variables
and remember that the order of output is the order in which the variables are written.
eg:
int a=1, b=2;
int a=1, b=2;
cout<<a<<" "<<b;
output : 1 2
Let's see some other type of variables namely char, bool
char : It is a variable that can store character values, It takes 1 byte space. So it can store numbers(0-9),
characters ('A'-'Z' 'a-'z') special characters(. /, ? etc).
In more technical term it can store any character whose ASCII values are from 0 to 255.
(Why 255 ???
well!!! 2^8-1 = 255 it is unsigned so it uses all the 8 bits)
eg: char c = 'a';
bool : It is a variable that is used to store true or false
This completes the basics about variables in c++
I have uploaded the source code of the programs in google drive Learning c++ having the
same name as in the first line in the picture of the program(programs not bearing any name don't have any source code)
char : It is a variable that can store character values, It takes 1 byte space. So it can store numbers(0-9),
characters ('A'-'Z' 'a-'z') special characters(. /, ? etc).
In more technical term it can store any character whose ASCII values are from 0 to 255.
(Why 255 ???
well!!! 2^8-1 = 255 it is unsigned so it uses all the 8 bits)
eg: char c = 'a';
bool : It is a variable that is used to store true or false
Constants and Literals in C++
constants are nothing but variables whose values can't be changed during runtime'
Literals are nothing but values specified during the compile time(i.e. hardcoded) of the program
there are various types of literals :
1> Integer literals : used to express integer constants and values
eg : int a = 12; // here 12 is the integer constant
2> Floating Point literals : used to express floating point values
eg : float a = 12.12;
3> character and string literals : used to express character constants or group of characters(string)
eg 'a', 'b', "Hello world!!"
In c++ there are two ways to declare a constant variale
1> with the const keyword :
// const integer variable declaration with the constant keyword
const int consta = 2;
// const float variable declaration with the constant keyword
const float consta = 2.2122;
// const char variable declaration with the constant keyword
const char constch = 'a';
2> With the #define keyword :
e.g. #define iamaconstant 2
here iamaconstant can be used just like a constant with value 2
I have uploaded the source code of the programs in google drive Learning c++ having the
same name as in the first line in the picture of the program(programs not bearing any name don't have any source code)
Nice Blog Tamojit
ReplyDelete