Variable and Constant in C

Variable and Constant in C

Blog 4
C Programming

Variable and Constant in C

Variable and Constant In C Programming, variables are like a storage vicinity or we can say it’s miles like a field this is used to shop information and facts can be changed additionally relying upon the requirement. right here the container manner in that you keep some substances in daily lifestylesfor instance, you drink water and that water is saved within the water bottle, so right here water is the information, and the water bottles are the box.

similarly, in programming records is saved in a variable, the steady is nothing however the form of variable is best but you may exchange the fee of that variable declared as constants at some stage in the entire application.

constant is a cost that can’t be altered with the aid of the program during regular execution, i.e., the cost is regularwhen associated with an identifier, a constant is said to be “named,” although the phrases “constant” and “named consistent” are often used interchangeably. this is contrasted with a variable, that’s an identifier with a price that can be changed in the course of ordinary execution, i.e., the cost is variable.

Variable In C:  

A variable refers to a memory location, or we can also say it’s a name of a memory location. We can change or modify the values of a variable anytime in a program, and it can be reusable any number of times.

Syntax: datatype variable_name;

Eg:

int num; 

num = 20;

Here, int is a datatype, and num is a variable name.

The 1st step is called a Declaration. We have declared a num variable of Integer datatype & 2nd step is called Initialization, where we have assigned a value 20 to the variable num. We can also declare and initialize variables in one line like below.

int num = 20;

 

Lets now see the types of Variables mainly used while codding in C:

Variables

  • Local Variables:

A variable that is declared inside the function is called a local variable.

E.g.:

void addition()

{

int x,y; // Local variables

}

 

  • Global Variables: 

A variable that is declared outside the function is called a global variable. It is available to all the functions, and any function can change the value of the global variable.

Eg:

Int num1,num2;

void main(){

num1 = 5;

num2 = 10;

printf(“The addition of the number is :” num1+num2);

}

 

  • Static Variable:

A variable declared with the static keyword is called a static variable. It retains its value no matter how many times it has been called, i.e. if the value of the static variable is changed. The next call will use the latest modified value, which is impossible with local or global variables.

 

Eg:

void addition(); //Method Declaration

void main(){

addition(); //calling function name addition

addition();

addition();

}

void addition(){

int num1=1;

static int num2 = 1;

num = num1+1;

num2 = num2+1;

printf(“The values for num1 is %d and num2 is %d \n”,num1,num2);}

 

Output:

Values

As seen in the above programs, num1, defined as a local variable, has the same value as 1, but num2, described as a static variable, has incremented its value in every call because the static variable retains its value at every call.

  • External Variable:

A variable declared by the extern keyword is called External Variable. It can be shared or used in multiple c files.

 

Since we have seen ‘what are Variables the types of it’, let us now see how can we name a variable, i.e. naming convention for Variables.

  1. A variable can contain underscore, numbers, and alphabets, both uppercase and lowercase.
  2. It can only start with alphabets or underscore and not with numbers.

E.g.:

int num1 – Correct

int my_num – Correct

float addition – Correct

float _myPage – Correct

int 5 – Incorrect

int 7_num2 – Incorrect

int num3_8 – Correct

Constants in C

Constant: A variable whose value cannot be changed is called constant. Suppose we want to have a variable whose value should be fixed and no one can change that value, then we must declare it with the ‘const’ keyword

E.g.:

const float PI = 3.14;

Since we have declared PI with the const keyword, the value can’t be changed later. If we try to change the value for PI, we will get an Error. It is advisable to name a variable in the Upper case whenever declared as Constant.

a full explanation of constant in c

conclusion :

variable and constant in C programming is that a steady is just like a variable, however, it can not be changed through this system once it’s miles defined even as a variable is a reminiscence location that holds records. In short, a constant is a special sort of variable that can’t be modified for the duration of execution.

Click here to learn more about the C language

 

Leave your thought here

Your email address will not be published.