Variables in C language

Introduction of variables in c language

Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Variables in c language

If you want to store data in the computer’s memory then for that you have to give a name for that memory location first. The variables in c language’s memory contain the name of a location.

Let’s say this is your computer memory space. You want to store some data in it. Such as the age of a person etc. Before you can store data in computer memory, you will know exactly what kind of data you store. These define you through data types. Accordingly, you get space in memory. That is, if you have defined int, then by 2 bytes you will allot the compiler in memory.

Along with that, you have to define the name of that memory location too. So that whenever you want to access the memory stored in that memory location, you can access it by this name. This name is called variable only. Values of variables in c language are variable. You can delete a value and enter another value. You can do it yourself manually or dynamically (during program execution) too.

Creating Variables in C language

To create variables in c language, first, you define the data type. After this, you define a unique name. Its structure is as follows.
dataType variableName;                   // without assigning value
dataType variableName = value;       // with value assignment

For example, see the below-given statement.

int Age = 25;

An integer variable has been created by this statement, which is named Age and this variable has been assigned 25 values. Let us now understand how the compiler interprets this statement.

When the compiler first interprets int, it allot memory of 2 bytes of the computer’s memory. After this, when the compiler interprets the age, then it gives the name of a memory of that 2 bytes to age. After this, when compiler = 25 interprets, 25 stores this memory location.

Now, whenever you want to access this value you can access it by age.

Scope of Variables

Can work up to some variable program. This is his scope. The scope of the scope has been divided into 2 categories.

Scope of Variables in C language

Local Variables in C language

Local variables are variables in c language that are defined in a small block of the program such as function or control statement blocks or any other block ({}). The use of such variables is limited to this block only. Like if you have created a variable in any function, you can not access that variable outside of that function. For example, look at the given program below.
#include  <stdio.h>

void myFunction();

int main()
{
int num=6;
     myFunction();
     printf(“Num in main() : %d”, num);

return 0;

}
void myFunction()
{
         int num= 5;
         printf(“Num in myFunction : %d\n”,num);
}

The above program produces the below-given output.

Num in myFuntion() : 5
Num in main() : 6

Global Variables in C language

Global variables are those variables in c language whose scope is in the entire program. These variables allow you to access anything in the entire program. An example of this is being given below.

 

#include <stdio.h>

int num=5;

void myFunction();

int main()
{
    myFunction();
    printf(“Num in main() : %d”,num);
    return 0;
}
void myFunction()
{
    printf(“Num in myFunction : %d\n”,num);
}

The above program produces the below given output.

Num in myFunction() : 5
Νum in main() : 5

Constants in C language.

Constants are those variables in c language whose value does not change. Whenever you declare a constant, its value remains fixed during the execution of the program. If there is an attempt to change its value then there is an error in the program. These are also called literals.

Constants can declare you in 2 ways.

  • Using #define
  • Using Const keyword

Using #define

#define is a pre-processor by using it you can continuously declare. An example of this is being given below.
# include <stdio.h>
#define result 10
int main()
{
    int a=5, b=6;
    result = a + b; /* WRONG, (ERROR) Value of constant result variable can not be changed. */
    printf(“%d”, result);
    return 0;
}

The above program produces the below-given output.

error: lvalue required as left operand of assignment
           result = a + b;
                     ^

 

Using const Keyword

You can also declare constants by Const keyword. An example of this is being given below.

#include <stdio.h>
int main()

{

const int a=5;

const int b=6;

int c;
c = a+b;
printf(“Result is : %d”,a);
return 0;
}
The above program produces the below-given output.
The result is: 11

 

 


Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

18 comments

Leave a Reply

Your email address will not be published. Required fields are marked *