C – Variables3 min read

What are variables in C?

The variable is the basic unit of storage that holds the value while the program is executed. We can also say that it is a name given to the memory location. A variable is defined by data-types provided by C.

It may belong to any data-types in C that can be char, int, float, double, and void.

Rules for naming C Variables:

  • A variable name must begin with a letter or underscore.
  • They are case sensitive that is Score and score are not the same.
  • They can be constructed with digits, letters and underscore.
  • No special symbols are allowed other than underscores.
  • A variable name cannot be a keyword. Example, int cannot be a variable name as it is an in-built keyword.

Variable Definition, Declaration, and Initialization in C

Defining a variable:

Defining a variable means the compiler has to now assign storage to the variable because it will be used in the program. It is not necessary to declare a variable using an extern keyword, if you want to use it in your program. You can directly define a variable inside the main() function and use it.

To define a function we must provide the data type and the variable name. We can even define multiple variables of the same data type in a single line by using a comma to separate them.

type variable_list_name, where type must be the valid C data-type.

Example:


Declaration of Variables:

The declaring variable is useful for multiple files and the variable is declared in one of the files and is used during linking the file.

Declaration of variables must be done before they are used in the program. Declaration does the following things.

  • It tells the compiler what the variable name is.
  • It specifies what type of data the variable will hold.

A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program.

The user needs to use the keyword extern to declare a variable at any place. Though you can declare variable multiple times in your C program, it can be defined only once in a file, a function, or a block of code.


Initialization of Variables:

initialization is simply giving the variable a certain initial value which may be changed during the program.

A variable can be initialized and defined in a single statement, like:

Let see an example for declaring, defining and initializing in C:

The output:


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …