BEST TUTORIAL FOR PROGRAMMING        
BEST TUTORIAL FOR PROGRAMMING

Variables in C

What is a variable in programming : A variable is a name which is given to memory location, where we can store, access and manipulate data. We can consider a variable as a storage container or placeholder that is used to hold data or information. A variable is a way to represent memory location through symbol or name and the value of a variable can be changed or altered during the program execution.


In simple words, A variable is a name of memory location like name of the computer's memory (RAM) location.


variables in c
variables in c



What is variable in C

?

Variables in C programming : As we know that you are able to answer of this question very easily now. The definition of C variables are same, as we given above. In C language, A variable is such an identifier that we can use to store, access and manipulate data or information in C programs. All the variables must have unique name.




Variable declaration in C

Now, We will see that what is the syntax to declare a variable and how to declare it in C programming.


The syntax to declare a variable in C

< Data Type > < Variable Name >;
Or
< Data Type > < Variable Name >, < Variable Name >, < Variable Name >;
Or
< Data Type > < Variable Name > = < Variable Value >, < Variable Name > = < Variable Value > ;



Example to declare variable in C

int variable1;
Or
float variable2, variable3;
Or
char variable4 = 'V' , variable5 = 'K' ;


Here, variable1 is an int data type variable that can be used to store number or integer value. variable2 and variable3 are float data type variables that can be used to store float value or real number. variable4 and variable5 are char data type variables that can be used to store a character, as we stored. int, float and char are data types in C programming. Don't worry about data types! We will learn about C data types in next section.



Example to declare variable in C and initialize in one go

int variable1 = 14;
Or
float variable2 = 12.93, variable3 = 9.99;
Or
char variable4 = 'V', variable5 = 'K', variable6 = 'T';


Initialization means to assign some value to the variable. A variable must be initialized before it is used.



Rules for declaring variables in C

1  A variable can contain one character or more than one.

2  No white space is allowed within name of a variable.

3  A variable can have alphabets, digits and underscore like a_1.

4  A variable can not be any reserved word or keyword like int, float, char, etc.

5  A variable can start with alphabet and underscore only.

6  First letter of the variable can not be a digit like 1var, it's wrong.

7  Each variable must be given a unique name.



Examples of valid variable names in C

int v = 14;

float var1 = 12;

char _v_1 = 'V';

char _1_k = 'K';

float ___ = 14.93;



Examples of invalid variable names in C

int if = 14; // If is a reserved keyword.

float 1var = 12; // Variable name can not start with digit.

char vk k = 'V'; // No white space is allowed within variable name.

char sum-money = 'K'; // hyphen (-) sign is not allowed in variable names.

float vk=t = 14.93; // equal (=) sign is not allowed in variable names.





C variable types

Type of a variable defines specific scope of a variable, where we can access that variable. A list of C variable types are given below. In coming sections (C Storage Classes), we will learn about C variable types in detail.

1  Local Variable
2  Global Variable
3  Automatic Variable
4  Static Variable
5  Register Variable
6  External Variable


C local variable

A variable that is declared inside a function or a block ( {...} ) is called local variable. We can access and use such variables inside the function or block, where they are declared.




C global variable

A variable that is declared outside all functions is called global variable. All functions can access global variables and manipulate data or information of global variables.




Scope of variables in C

In programming, A scope is a region of a program and scope of a variable refers to the region or area of the program where the variable can be accessed and used after its declaration. A variable can not be accessed and used outside of its scope.

There are basically three scopes where we can declare variables in C programming :

  • Inside a function or a block ({...}) which are called local variables.

  • Outside of all functions which are called global variables.

  • The variables are declared as function parameters are called formal parameters.



INFO : In Coming sections, We will learn about C data types and C variable types in more detail.