×
>
<

Aptitude

Variables and Constants

Variables in programming

In computer terms a variable is the name of the memory location of the data which can be accessed with the help of that name and can be rewritten or used for calculations later on. Variable is a storage area. which is used to store the data. variable data is contained, which can be changed at any time during program execution. After declaring the variable it is given a value, this value is to be assigned in the many form. Like x = 5; or a=10;    Before using the variable it is necessary to declare in C. Values of variables are changeable. We can delete a value and enter another value.  You can also do this on compile time and dynamically(during program execution).

Rules to declare a Variable ?

Rules for Variable:-

  • These stores the values inside it.
  • Variable is also the name of a memory location.
  • Variable is case-sensitive. For example, int a or int A both are different variable.
  •  Variable starts with any alphabet (a-z, A-Z) or underscore(_).
  • Variables name can be alphanumeric. for example, a1=5,  var1, var2
  • Variable does not allow space.
  • Variable name does not have any C keywords.
  • The name of any variable can not be start with any number.
  • Any upper case and lower case character can be used in any variable name.

 

Datatype of Variables:

A variable should be given a type in the C language,  which determines what kind of data the variable will hold. it can be char, int, float or double.

DECLARATION OF A VARIABLE :

While programming you first need to tell the computer/compiler the variable’s name and it’s data type, when you do that the compiler creates an empty memory reserved for that data .

In other words, we can say that

  • When we declare a variable , then it allocates memory according to the data type of variable.
  • After declaration of th variable, it takes Garbage Value inside it.

Single Variable Declaration

data_type single_variable_name;

Example:

  
#include "stdio.h";
int main(){
	int a=25;
	printf("value of a : %d", a);
	return 0;
}
  
output
  
value of a : 25
  
Declaring Multiple Variables
  data_type multiple_variable_name;
  
Source Code
  
#include
int main()
{
   int a=25,b=4,c=67;
   printf("value of a: %d",a);
   printf("\nvalue of b: %d",b);
   printf("\nvalue of c: %d",c);
   return 0;
}
  
output
  
value of a: 25
value of b: 4
value of c: 67 
  
Scope of Variables

By scope of a variable we mean which part of the code a variable is accessible (visible) to .A variable can have many scopes in c++. let’s discuss some of them .

According to Scope, variables is divided into two categories:-

Local Variables
Global Variables

Local Variable

Local variables are those variables that are defined in a small block of the program such as function, control statement block etc. Such variables are used only by the same block.

  • A variable inside the function/block is a local variable .
  • Local Variables is inside the function.
  • The default value of the Local variables is ‘garbage value’.

Example :

  
#include "stdio.h"; 
int main()
{
     int a,b,c; // local variables
     a =10;
     b = 30;
     c = a + b;
     printf("%d",c);
}
  

Here a, b, c all are local variables and can not be used by any other function except main. On execution of the program the compiler prints 40

 

Global variable

As oppose to local variable a global variable is out side every function and is accessible to all the functions and the value of a global variable can be changed by any function.

Global variables are those variable whose scope is in whole program. These variables are defined at the beginning of the program.

  • Global variables are out of the function.
  • Global variables have visibility throughout the program.
  • The default value of ‘Global Variables’ is ‘0’.

Example :

Global Variable

  
#include "stdio.h";
int d=20; // global variable
int main()
{
      int a,b,c; // local variables
      a = 10;
      b = 30;
      d = d + 10;
      c = a + b + d;
      printf("%d",c);
      return c;
}
  
output
  
70
  
Constants in C++

Constant are those Whose value can not be change during program execution. whenever you declare the constant its value remain fixed during execution of the program.If there is an attempt to change its value then there is an error in the progam.

  • A value or a keyword that has a definite set value is called a constant.
  • Value of Constant is fixed.
  • Constant can be any data types.
  • Constant is also known as Literals.
  • Constant is also the pointer.

Floating constant :
  • A floating constant is a decimal value is called a floating constant.
  • The floating constant works as a normal float variable.
  • It contains the decimal point.
  • If the value of the floating constant is of integer type, it takes the decimal point.

Example: 3.14; is a floating constant.

  
#include "stdio.h"
int main()
{
	const float num1 = 5;
	const float num2 = 3.123; 
	printf("floating constant is %f", num1);
	printf("floating constant is %f", num2);
	return 0;
}
  
output
  
floating constant is 5
floating constant is 3.123 


  
Character Constant :
  • When character values to it then it is called a character constant.
  • Character constant works as a normal variable.
  • Character constant takes only a single character.
  • The character constant is also used with escape sequences.

example: “A�; is a character constant.

  
#include "stdio.h>
int main()
{
	const char ch = 'S';
	const char escape[] = "Hello\tWorld";
	printf("character constant is %c", ch);
	printf("string constant is %s",escape);
	return 0;
}
  
output
  
character constant is S
string constant is Hello    World
  
String constant :
  • A string constant is a string array (coming up in later lessons) having fixed string values to it.
  • String constant takes a single character and multiple characters.
  • The value of the string constant is written within the double quotation mark (“â€?).
  • a string constant is also used with escape sequences.

example : {‘b’,’o’,’o’,’k’,’s’,’\0′} .

  
#include <stdio.h<
int main()
{
	const char st1[] = "S";    //string with single character
	const char st2[] = "Hey Somya";    //Normal string constant
	const char st3[] = "Hey\nSomya";      //string constant with escape sequences
	printf("string constant with single character: %s", st1);
	printf("normal string constant: %s", st2);
	printf("string with escape sequences: %s", st3);
	return 0;
}
  
output
  
string constant with single character : S
normal string constant : Hey Somya
string with escape sequences : Hey
Somya
  
Constant keyword

We can define a constant to a program using #define or const int or const float or const char whichever we want and that keyword (identifier) will have a fixed value and can not be changed later (remains a constant).

Example: area of a triangle

  
#include "stdio.h"
#define LENGTH 10
void main()
{
	const int BREADTH=5;
	int area;
	area=LENGTH*BREADTH;
	printf("%d",area);
}
  
output
  
50