Variables in C language. Declaration, Initialisation of Variables
Introduction:
We have discussed about the Identifier in C and rules for naming the Identifiers in our Previous article, In today’s article, We will learn about the Variables in C programming and How to create, declare and initialise the C variables.
Variables in C Language:
Variable is a name of Memory location, Which is used to store the values.
While developing the applications, We need to maintain few data values or Modify the data to perform certain tasks.
For example, If you are trying to create a simple calculator and your calculator app will take two numbers, Then we need some way to store or hold the two values which are entered by the user. In this case, We can use the C Language Variables to store user input and save the values in the memory and The variable names will allow us to access those values from the computer memory.
As the name suggests Variables values can vary or change during the Execution of program
Example:
1 2 3 4 5 |
// Created a Variable 'var1' with value 10 int var1 = 10; // Now, 'var1' is changed from 10 to 44. var1 = 44; |
So unlike the Constants in C Language, The variables can change its value during the program execution.
The main objective of the Variables is to provide a way to store and modify the data.
Each variable need to be created before using the the program. Let’s discuss about the variable creation.
Creation or Declaration of Variables in C :
The C Programming Language is a Strongly-Typed language, So We need to specify the datatype of the each variable. The Variable datatype is going to decide the data that variable can take.
So while creating or declaring the variable, We need to specify the datatype of the variable.
Syntax of Variable declaration:
1 |
datatype variablename; |
As you can see from the syntax the datatype should be any valid C datatype. And variablename is the name of the variable. You are free to name your variables. But you need to follow the certain rules while naming your variables. Learn more at Rules for Naming the Identifier or Variables.
Here are few key points regarding the variable declaration.
- Variables must be declared before using in the program.
- Declaration of variable specify the name and Datatype.
- Variable name should not an Operator, Separator, Keyword and Constant.
- If you are creating multiple variables in single line, Then use the Comma to separate them.
Examples to demonstrate Variable declaration in C Language:
Example 1:
1 2 |
// Declaring a variable 'n' of Integer dataype int n; |
In above snippet, We declared a variable named ‘n’ of Integer datatype. The variable ‘n’ can be used to store the Integer values.
Example 2:
1 2 |
// Declaring variable 'pi' with float datatype float pi; |
We declared a variable ‘pi’ with float datatype, Now ‘pi’ can take floating point data values.
Example 3:
1 2 |
// Declared a Variable 'c' of character datatype char c; |
We created a variable ‘c’ of Character datatype.
Example 4:
1 2 3 |
// We are trying to create a variable with name 'if' // But it is not allowed as 'if' is keyword of C float if; |
In above example, We are trying to create a float variable with name ‘if’. But above program will generate the compilation error. As we discussed earlier, We can’t use the Keyword names for naming our variables. Here ‘if’ is a valid keyword in C Language, So we can’t use it as our variable name.
So don’t use keywords or Reserved word names as your variable names.
📢 You Can’t use Keyword names to name your variables. Which will result into the compilation error.
Example 5:
1 2 3 4 5 6 |
// Declaring multiple variables in a single line // We created four integer variables here, 'i', 'j', 'k', and 'result' int i, j, k, result; // Here we created/declared four float Variables float add, sum, mul, div; |
We can declare multiple variables of same datatype but they should be separated by comma ( , ).
In above example, We have created four Integer variable and Four Float variables.
So far discussed about the variable declaration, We can also initialise the variables with values ( Declaration + Assignment). Let’s learn about the Variable initialization in C Language.
Initialization of Variables in C Programming :
We just discussed about Variable Declaration in C Language. But we yet to assign the values to our variables.
If you declare a variable without assigning any value, Then that variable contains the Garbage value ( Undefined value ) until we assign some value to it.
So It is always recommended to assign desired ( or Default) values to your variables. Here is an example of assigning the values to variable.
1 2 3 4 5 |
// Variable Declaration int n; // Assigning Value to variable 'n' n=10; |
Here we have declared a variable
'n'and then assigned value
'10' to it.
The above operation is two step process, In the first step We created or declared the variable and in the next step we assigned the value to variable.
- Variabel Declaration or Creation
- Assigning the Values
But we can combine those two steps by giving the value at the time of declaration, This is called Variable Initialization.
Variable Initialization Examples:
Let’s Initialize few variables.
1 2 3 4 5 6 |
int n = 10; // Integer Variable Initialization char ch='M'; // Character Variable Initialization // Float Variable 'per' Initialization float per=78.3; |
As you can see from above output, We Created and initialized three variables ‘n’ and ‘M’ and ‘per’.
Quick Notes:
- Always try to initialize your variable. You may Initialize it with some default value. Many times programmers forgot to initialize the variables and which results into the garbage values.
- Give meaningful names to your variables which will improve the code readability. So Instead of giving some random names, Try to name the variables with the intent of variable.
- Try to follow same naming convention throughout the all programs. For example, If you like to use CamelCase then use camelCase for all your variable names.
Conclusion:
In this article, We have discussed about the creation or declaration, Assignment and Initialization of variables in C programming Language.
Related Article:
- Start Here – Step by step tutorials to Learn C programming Online with Example Programs – SillyCodes
- Come Together – The Collection of all Pattern Programs in C – SillyCodes
- C codes Archives – SillyCodes
8 Responses
[…] our previous article, We looked at the Variables in C-Language. In this article We are going to discuss Escape Sequences in C programming language. We will learn […]
[…] the Program by declaring three Matrices (2D Arrays). They are X matrix, Y matrix, and Z Matrix. The row size and column size […]
[…] the Program by declaring two Matrices (2D Arrays). They are X matrix, Y matrix. The row size and column size of the matrices […]
[…] the Program by declaring a Matrix (2D Arrays) named X[ROWS][COLUMNS]. The row size and column size of the matrices are ROWS […]
[…] four variables to count the number of vowels ( nVowels), number of consonants( nConsonants), number of digits( […]
[…] the above example, The pointer variable ptr is pointing to the single element of the files array, that is the first element of the array ( […]
[…] started the above program by dynamically allocating memory for a user-provided number of integer variables using the malloc() function. If the malloc() failed to allocate the memory, Then we terminated the […]
[…] We are allocating the 10 blocks of memory and each block of 4 Bytes size. (Assuming integer size is 4 Bytes). So in total, we are allocating the 40 Bytes, that can accommodate for the storage of ten integer variables. […]