Data Types and Variables in C Language - BunksAllowed

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

Community

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 Type Size in bits Range
char 8 -128 to 127
unsigned char 8 0 to 255
signed char 8 -128 to 127
int 16 to 32 -32768 to 32767
unsigned int 16 to 32 0 to 65535
signed int 16 to 32 -32768 to 32767
short int 16 -32768 to 32767
unsigned short int 16 0 to 65535
signed short int 16 -32768 to 32767
long int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
signed long int 32 -2147483648 to 2147483647
long long int 64 -2^63 to 2^63 - 1
unsigned long long int 64 0 to 2^64 - 1
signed long long int 64 -2^63 to 2^63 - 1
float 32 1E-37 to 1E+37 with six digits of precision
double 64 1E-37 to 1E+37 with ten digits of precision
float 80 1E-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

Note: Only a member of this blog may post a comment.