Variables in C language. Declaration, Initialisation of Variables

Variables-in-C-language-Declare-initialize-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:

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:

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:

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:

We declared a variable ‘pi’ with float datatype, Now ‘pi’ can take floating point data values.

Example 3:

We created a variable ‘c’ of Character datatype.

Example 4:

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:

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.


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.

  1. Variabel Declaration or Creation
  2. 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.

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:



Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

8 Responses

  1. […] 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 […]

  2. […] the Program by declaring three Matrices (2D Arrays). They are X matrix, Y matrix, and Z Matrix. The row size and column size […]

  3. […] the Program by declaring two Matrices (2D Arrays). They are X matrix, Y matrix. The row size and column size of the matrices […]

  4. […] the Program by declaring a Matrix (2D Arrays) named X[ROWS][COLUMNS]. The row size and column size of the matrices are ROWS […]

  5. […] four variables to count the number of vowels ( nVowels), number of consonants( nConsonants), number of digits( […]

  6. […] 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 ( […]

  7. […] 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 […]

  8. […] 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. […]

Leave a Reply