Loading, please wait...

A to Z Full Forms and Acronyms

Variables in C Programming

Jun 24, 2021 variable, decelaration, c programming, 1272 Views
In this article you will learn about variables

Variables in C Programming

Variables are the basic objects manipulated in a program. Declaration gives an introduction of variable to compiler and its properties like scope, range of values and memory required for storage. A variable is used to store values. It has memory location and can store single value at a time.

When a program is executed, many operations are carried out on the data. The data types are integer, real or character constants. The data is stored in the memory, and at the time of execution it is fetched for carrying out different operations on it.

A variable is a data name used for storing a data value. Its value may be changed during the program execution. The variable value keeps on changing during the execution of the program. In other words, a variable can be assigned different values at different times during the program execution. A variable name may be declared based on the meaning of the operation. Variable names are made up of letters and digits. Like height, avg12, sum etc.

* WAP to declare and initialize variables and display them.

Void main()
{
int age=20;
float height=5.4;
char sex=’M’;
clrscr();
printf(“age : %d\n Height : %g \n sex : %c”,age, height, sex);
}

Output:

age: 20
Height: 5.4
sex: M

Explanation:

In the above program int, height and char variables are declared and values are signed. Using printf() statement values are displayed.

Rules for defining Variables

  1. A variable must begin with a character or an underscore without spaces. The underscore is treated as one type of character. It is very useful to increase readability of variables having long names. It is advised that the variable names should not start with underscore because library routines mostly use such variable names.

  2. The length of the variable varies from compiler to compiler. Generally, most of the compilers support eight characters excluding extension. However, the ANSI standard recognize the maximum length of a variable up to 31 characters. Names of functions and external variables length may be less than 31 because of use external names may be used by assembler and loaders over which language has no control. For external names, the varibale names should names should be of six character and in single case.

  3. The variable name should not be a C keyword.

  4. The variable names may be a combination of uppercase and lowercase characters. For example, sum and sum are not the same. The traditional practice is to use lowercase characters for variable name and uppercase letters for symbolic constants.

  5. The variable name should not start with a digit.

  6. Blanks and commas are not permitted within a variable name.

A to Z Full Forms and Acronyms