![]() |
| Variables in C Programming |
Variables in C: Variable is a name of storage space which is used to store of data. Variable value is changeable. It is a declared always with data types. Variable simple means hold the any value. Variable is most important in every programming languages.
How to define Variable name in C
C variable name must be identified with unique names. It is called variable name. Variable name to define to short name (a and b). But recommend you can define very easy to program based variable name.
Let your C program 100 lines of code but variable name proper not define so very hard understand or maintain your code.
Declaration and Initialization of Variable
float marks; char grade; int id=101; float marks=78.9; char grade='A'; |
Note: Here int, float and char is a data type but id, marks and grade are variables name. so variable are most important part of C language without variable declared not execute any C program.
Rules For Naming variables in C
1.The first letter of variable should be alphabet or underscore(_).
2. The first letter of variable should not be a digit
3. After first character it may be combination of alphabets and digits.
4. Blank space are not allowed in variable name.
5. Variable name should not be a keyword.
6.Variable name are case sensitive like JIOCODING or jiocoding are different variables name.
How to Use Variable in C Programming
|
#include<stdio.h> #include<conio.h> void main(){ clrscr(); int id=112; float marks=89.6; char grade='A'; printf("Integer Variable of Id:%d\n",id); printf("Float variable of Marks:%f\n",marks); printf("Character Variable of Grade:%c",grade) getch(); } ============OUTPUT============ Integer Variable of Id:112 Float variable of Marks:89.599998 Character Variable of Grade:A |
Format Specifiers in C Language
| Data Type | Description | Uses | Size | Format Specifier |
|---|---|---|---|---|
| int | integer | without decimal | 4 | %d |
| float | floating | with Decimal | 4 | %f |
| char | Character | Single Character | 1 | %c |
| double | float double | Long Double | 8 | %lf |
| bool | Boolean | True and False | %d |
Declared Life Time in variable name
Example of Local Variable in C
| #include<stdio.h> #include<conio.h> void local_variable(){ int x=100; printf("I am Local Variable:%d",x); } void main(){ clrscr(); local_variable(); } getch(); } ============OUTPUT============ I am Local Variable:100 |
Example of Global Variable in C
| #include<stdio.h> #include<conio.h> int x=200; void local_variable(){ int x=100; printf("I am Local Variable:%d\n",x); } void main(){ clrscr(); local_variable(); printf("I am Global Variable:%d",x); } getch(); } ============OUTPUT============ I am Local Variable:100 I am Global Variable:200 |
Example of Static Variable in C
| #include<stdio.h> #include<conio.h> void main(){ clrscr(); for(int i=0;i<=5;i++){ static int x=0; printf("Static Variable:%d\n",x++); } getch(); } ============OUTPUT============ Static Variable:0 Static Variable:1 Static Variable:2 Static Variable:3 Static Variable:4 Static Variable:5 |
Example of Automatic Variable in C
| #include<stdio.h> #include<conio.h> void main(){ clrscr(); auto int x=100; printf("Value of x:%d",x); } getch(); } ============OUTPUT============ Value of x:100 |
Example of External Variable in C
|
#include<stdio.h> #include<conio.h> int x=200; void main(){ clrscr(); printf("Value of x:%d\n",x); extern int x; x=500; printf("Value of x:%d",x); } getch(); ============OUTPUT============ Value of x:200 Value of x:500 |
Example of Assign Multiple Variable in C
|
#include<stdio.h> #include<conio.h> void main(){ clrscr(); int a,b,c; a=b=c=100; printf("Value of a,b,c:%d",a+b+c); getch(); } ============OUTPUT============ Value of a,b,c:300 |
Conclusion of Variable in C
Conclusion: A variable is a very important part of C language. It can be used in C Program. Variable are not define your program not readable or understandable. So variable are used in C program.If any query for variable related you can comments or contact me. I can solve variable related question.

0 Comments