Variables In C++ | Variable data types in C++ | C++ variables declaration and definition

     In any programming language to perform operations we require operands or better we say variables and constants to get any desired output. 


Constant :

                A constant is an entity whose value does not changes throughout the execution of the program.

Syntax :  const data_type variable_name = value;   
Example : const int a = 10;

Variables :

                Variables are just opposite to constant. Their values may change during the execution of program. A variable can store values and perform arithmetic and logical operations on it. Variable can store different types of values such as numerical, alphabetical(characters). To use any variable in any program we have to first declare it(giving a name to the variable).

Syntax : Data_Type variable_name;
Example : int a = 10;

Semicolon is required to end the command. In C++ we have to use semicolon(;) to tell the compiler that this command has ended move to the next command.

 Variable can be of two types

  •  Local Variable
  •  Global Variable

 

Local Variable :

                These are the variables which we can use for a particular block of the program. After the end of that block the variable scope ends. For example, if you declared a variable inside a function then it is accessible only inside that function.


Global Variable :

                As the name suggest the Variable can be used globally i.e. throughout the program



              Now to use or access any variable we have to declare it. To declare or naming any variable there are few set of rules we should follow.


Rules :

  •         It must begin with a letter
  •         It is case sensitive. This means ‘ab’ and ‘Ab’ will be two different variables
  •         The name can consists of [A….Z],[a….z],[0 to 9]and[_]
  •         There should not be any Key word or any special characters like ?, @,etc.
  •         Blank space is not allowed

The rules to declare a variable remains same for both global and local variables.



Keyword :

                This are the words which has a predefined meaning in the computer like static, const, void, int, etc.



Datatypes :

                Datatypes specifies what type of information or value a variable will save. The value can be an integer, character or any float data(example 3.15). To declare a variable we have to specify the data type of the variable to the compiler so that it knows what type of value is going to be store in that variable.



There are 4 basic Datatypes


1.       Character type :

A single character can be saved in this type if variables. Its Keyword is char and it requires 1 byte(8bits) of memory

Example  char abc;

                   abc = “c”;

2.       Integer Type :

Integer type variables save whole numbers or more specifically integer values. Its keyword is int and it requires 2 byte of memory

Example int a;

                  a = 10;

3.       Floating Type :

It stores the float value with 6 digits of precision. Its keyword is float and it requires 4 byte of memory

Example float abc;

                  abc = 11.1111;

4.       Double type :

It saves the float value till 14 digits of precision. Its keyword is Double and it requires 8 byte of memory

 

Example double abc;

                  abc = 21.21424524245;



Also Read !


You would love these Hackerrank solutions (See Here !)  





Post a Comment

0 Comments