C PROGRAMMINGDATA STRUCTURES

Variables and its types in C

Variables and its types in C


A variable is defined as a meaningful name given to a data storage location in the computer memory. When using a variable, we actually refer to the address of the memory where the data is stored. C language supports two basic kinds of variables.


Numeric Variables

Numeric variables can be used to store either integer values or floating point values. Modifiers like short, long, signed, and unsigned can also be used with numeric variables. The difference between signed and unsigned numeric variables is that signed variables can be either negative or positive but unsigned variables can only be positive. Therefore, by using an unsigned variable we can increase the maximum positive range. When we omit the signed/unsigned modifier, C language automatically makes it a signed variable. To declare an unsigned variable, the unsigned modifier must be explicitly added during the declaration of the variable.


Character Variables

Character variables are just single characters enclosed within single quotes. These characters could be any character from the ASCII character set—letters (‘a’, ‘A’), numerals (‘2’), or special characters (‘&’).


Declaring Variables

To declare a variable, specify the data type of the variable followed by its name. The data type indicates the kind of values that the variable can store. Variable names should always be meaningful and must reflect the purpose of their usage in the program. In C, variable declaration always ends with a semi-colon.


For example

int emp_num;
float salary;
char grade;
double balance_amount;
unsigned short int acc_no;


In C, variables can be declared at any place in the program but two things must be kept in mind. First, variables should be declared before using them. Second, variables should be declared closest to their first point of use so that the source code is easier to maintain.


Initializing Variables

While declaring the variables, we can also initialize them with some value. For example,
int emp_num = 7;
float salary = 9800.99;
char grade = ‘A’;
double balance_amount = 100000000;


Constants

Constants are identifiers whose values do not change. While values of variables can be changed at any time, values of constants can never be changed. Constants are used to define fixed values like pi or the charge on an electron so that their value does not get changed in the program even by mistake.


Declaring Constants

To declare a constant, precede the normal variable declaration with const keyword and assign it a value.


const float pi = 3.14;
const int a = 100;
const char choice = ‘y’;

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button