C Variables

C Variable

Variable represents storage location which is allocated some space in memory to store data. A variable in c is nothing but we have store data in memory with the namespace, so it easier to access without remembering the complex memory address and use for future use. It stores in memory so it can be reused when needed. We represent a storage location with a symbol that can be distinguished easily. We provide a name to variable Variables values that can be manipulated during execution. It has a different type that defines which type of data variable hold.

Datatype of variable

Variables have a type that defines how much memory is allocated to the variable in C language and which type of data variable contains.

1. int: Used to store an integer.
2. float: Used to store a 
floating-point number
3. char: Used to store a character in it.
4. double: Used to store a double value.
5. unsigned: use to store only positive whole numbers.

Rules for declaring variables in c


1. Every Variable must start with a letter or underscore i.e. it can’t start with digits.
2. Variable name contains alphabetical letters (a-z and A-Z) or digits (0-9).
3. Use of special symbols like @,#,$,etc. in variable names is prohibited.
4. Reserved words or keywords are not used as variables name like int, auto, etc.
5. Variable names are case sensitive both upper and lower cases treated as different.
6. Blankspace or whitespace in variable names is not allowed.

Types of variables in c

1. Local Variable
2. Global Variable
3. Static Variable

Below we discuss types of variables in detailed:

1. Local Variable: Variables which are declared inside the functions or block are known as local variable and these variables are not accessible from outside function. You have to declare before use it.



main() {
 int=10;  //local variable

}

2. Global Variable: Variables that are declared outside the functions and are accessible from anywhere inside the program by all functions. When we change the value of global variables it affects values of all functions that use this. Global variable declared at the top of the program. So, it became part of the whole program. 

int g=40; //global variable
void main() {
 float=20.0;  //local variable
}

3. Static Variable: We discuss static variable in detailed later.


Difference between variable declaration and variable definition in c programming


Declaration of Variables should be done before to use in the c program. Memory space is not allocating to variables at the time of declaration. Declaration of variables only informs the compiler about the existence of a variable. 


extern int a;     // variable declaration
extern int b, c;

Definition of variables in this compiler has to allocate the memory space to variables and now variables used in the program. We defined one or more variables of the same datatypes with a comma in a single line.


main() {
int p;       //variable definition
int q, r;
}

What is initialization in c with example


Variables initialization means a first value(initial value) provides to a variable. A variable with the first value is said to be an initialized variable.


void main() {
int p;          //variable definition
p=100;       //variable initialization
}

Assigning Values to Variables in C Programming 

Assignment values is a process of assigning a value to a variable.

int lenght=64;

Difference between lvalue and rvalue in c

There are two values associated with the symbolic variable :
  1. lvalue: This is the address in memory at which value is stored or its location value.
  2. rvalue: This is the data value stored at some locations in memory.

How to print a variable in c programming 

All Above concept applies to function declaration where we provide a How to print a variable in c programming. For example −

/* c program to print value of a variable */
#include<stdio.h>
int main() { 
int price = 1001; 
printf("Price is %d Rs.\n", price); 
return 0;
}

Above article, we discuss Variables in C and How to print a variable in c programming also if you have any doubt in the article then comment on your problem below.

Happy Coding!! 😊

If you have any doubts, Please let me know

Previous Post Next Post

Contact Form