C Variables

A variable in C is nothing but a name given to a storage area that our programs can manipulate. This location is used to hold the value of the variable, which determines the size and layout of the variable’s memory. In
C variable might be belonging to any of the data types like int, float, char etc.

Following are the rules for Naming Variables in C:

  • A Variable in C name must begin with a letter or underscore.
  • They are case sensitive.
  • They can be constructed with digits, letters.
  • No special symbols are allowed other than underscore.

Declaration of variables in C

Declaration of a variable must be done before they are used in the program. The 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. You will 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.

Defining a variable

Defining of a variable in c 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 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 comma to separate them.

Initializing of variables in C

Initializing of a variable means to provide it with a value. A variable can be initialized and defined in a single statement, like:

Let’s write a program in which we will use some variables :

Output:

Question related to Variable in C