Data Types and Variables in C Language - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Data Types and Variables in C Language

Share This
In any programming language, when we want to work with data, the data needs to be stored in some location of the main memory. To store this data, we need to understand the type of the data and its size. Data is nothing but a value.

A high-level programming language, like C, provides an alias or name of the memory location where the data is actually stored. Hence, we don't need to remember the address of the memory location. We can access the data by name instead of its address. The name by which we access this data is known as a variable.

There are few basic data types in C Programming Language. These data types are as follows:

char a single byte to hold one character.
int         holds an integer, but the size is dependent on the
float single precision floating point number.
double double precision floating point number.

At the time of writing a program, we may use any name for a variable (except keywords). But in good practice, we should use meaningful names for every variable. Note that name of a variable should not be a keyword. Its length should be a maximum of 31 characters. The name of a variable may start with any character within a-z and A-Z and with a special character _.

A variable name should not start with any digit or any other special symbol (except _). Moreover, a variable name should not contain any special symbol except _.

For example: 1age, $age, ag#e are invalid variable names

In addition, with the basic data types (mentioned above) sometimes two qualifiers are used at the time of variable declaration. These are short and long. Generally, these qualifiers are used where we need to store data with a lesser or greater range.

For example, the data type of the variable age is an integer, but to store the value we don't need 4 bytes of memory space. Its value will never exceed 2 bytes. Hence, we can declare the variable as short int, instead of int.
short int age;

Other qualifiers are signed and unsigned. These are used before int and char data types. By default, a variable is signed. If the value of a variable is always positive (it can not be negative, like age), we can declare the variable as unsigned. For example:
unsigned short int age;

In the following table, the sizes of data types are shown.

Data TypeSize in bitsRange
char8-128 to 127
unsigned char80 to 255
signed char8-128 to 127
int16 to 32-32768 to 32767
unsigned int16 to 320 to 65535
signed int16 to 32-32768 to 32767
short int16-32768 to 32767
unsigned short int160 to 65535
signed short int16-32768 to 32767
long int32-2147483648 to 2147483647
unsigned long int320 to 4294967295
signed long int32-2147483648 to 2147483647
long long int64-2^63 to 2^63 - 1
unsigned long long int640 to 2^64 - 1
signed long long int64-2^63 to 2^63 - 1
float321E-37 to 1E+37 with six digits of precision
double641E-37 to 1E+37 with ten digits of precision
float801E-37 to 1E+37 with ten digits of precision
Another special data type is void. It will be discussed later.


Happy Exploring!

No comments:

Post a Comment