Data stored in our computer system have some data types and a place in memory. It is nearly impossible to retrieve a value from memory without any variable name. So, we use variables to ease access to our data stored in memory. In other words, we can say that Variables are a type of container that is used to store a value in our system memory.
Types of Variables in C
In C programming there are mainly three types of variables.
- int –> Stores integer value like 1,2,4,-1,-5,-7,etc
- float –> Stores point numbers like 1.2,2.4,-5.6,-7.3,etc
- char –> Stores character values like a,v,x,r, etc.
We have already learned about Variables in C programming. But how to declare these variables? There is a simple syntax to declare a variable like the one given below.
var_type var_name = var_value
Print a Variable in C
We know how to create a variable. But we don’t know how to retrieve the value of the variable from its name. We will use the printf() function to print a variable. You can see the given syntax to print the value of a variable. %d, %c, %f are format specifier. %d is used for an integer, %f is used for float-pointing numbers, and %c is used for a character. Format Specifier is used in double-quotes.
printf(" Value of Variable = %d, %f, %c",int_var, float_var, char_var );
Let’s take a look at an example to understand variables more efficiently.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=34; // Integer Type Variable
float f = 34.5; // Float Type Variable
char c = 'A'; // Character Type Variable
printf(" Value of i= %d \n " , i); // To Print Integer Value
printf(" Value of f= %f \n " , f); // To Print Float Value
printf(" Value of c= %c \n " , c); // To Print Char Value
getch();
}
Rules of Naming a Variable
All variables have unique names such that they can be uniquely identified. These unique names are also known as Identifiers. A relative name of a variable makes a program easy to understand by others. Let’s take a look at the rules of naming the variable names.
- A name can contain a letter, number & underscore.
- A variable name must be starting with an underscore or a letter.
- Variable names are case-sensitive.
- Variable names can’t contain any special character or space.
- C Programming reserved words can’t be used as a variable name.
I hope you all like a quick revision of variables in C Programming. For any help regarding anything just comment now. Share it with your friends to support us.
Check out the articles given below for more cool hacking & Tech stuff…