Data Types in C Programming
C Data Type specifies the type of data that we store in a variable. In C programming language, there are many data types and the type of data also specifies how much amount of memory is allocated to a specific variable. Data types are used along with variables and function’s return type.
There are different data types. The main three classifications are :
- Primary data types
- Derived data types
- Enumerated data types
Primary Data Types
Fundamental data types defined in C are called Primary Data Types. They are :
- Integer
- Signed integer
- int
- Short
- long
- unsigned integer
- Int
- Short
- long
- Signed integer
- Float
- float
- double
- long double
- Character
- signed char
- unsigned char
- Void
Integer
The below given data types will store whole numbers. Whole numbers are 0,1,2,3… Following table shows the ranges for different integer datatypes.
Type | Storage Space in bytes | Range of whole numbers that can be stored | Format |
Int (or)signed int | 2 | -32,768 to 32,767 | %d |
Unsigned int | 2 | 0 to 65,535 | %u |
Short (or) signed short | 1 | -128 to 127 | %d |
Unsigned short | 1 | 0 to 255 | %u |
Signed long | 4 | -2,147,483,648 to 2,147,483,647 | %ld |
Unsigned long | 4 | 0 to 4,294,967,295 | %lu |
The most important objective of programming is saving memory to make the program run as fast as possible. To use memory efficiently, C programming has divided the data types as int, short, long, etc. If you know exactly how big the number is going to be and how often it is going to modified then you can declare the variable as short or int or long, so that you can save as much memory as possible.
Floating point types
Float types are to store decimal point numbers (or) real numbers. Real numbers are for example : 1.00,5.36 etc.
DataType | Storage space in bytes | Range | Precision | Format |
Float | 4 | 3.4E-38 to 3.4E+38 | 6 decimal places | %f |
Double | 8 | 1.7E-308 to 1.7E+308 | 15 decimal places | %lf |
Long double | 10 | 3.4E-4932 to 1.1E+4932 | 19 decimal places | %Lf |
Character
This datatype is used to store ASCII characters. Examples are a
, b
, z
, 8
, $
. char is supposed to store characters only not numbers, so why should it holds range?
In the memory characters are stored in their ASCII codes. For example, the character ‘B’ has the ASCII code 66. In the memory ‘B’ will not be stored as ‘B’ but as 66.
We have unsigned char and signed char.We don’t have negative characters, then why should we have such datatypes?
We used signed and unsigned char to ensure portability of programs that store non character data as char.
Type | Storage space in bytes | Range | Format |
Char or signed char | 1 | -128 to 127 | %c |
Unsigned char | 1 | 0 to 255 | %c |
Void
Void data type variables doesn’t have a value of any type. Void means no value.
Theoretically the void type should use no manipulable memory on stack when defined. That is to say, a variable declared with a “void” type shouldn’t be capable of storing any data, and therefore has a size of zero.
Basically void data type are used in some special places and some of them are following :
- To specify return type of a function(when function returns no value).
- To specify the parameters of a function(when the function accepts no argument from the caller)
- To create generic pointers.
Derived data type:
As the name suggests, derived data types are basically derived from primary data types. They don’t create a new data type but, instead they add some functionality to the basic data types. Derived data types includes arrays, pointers, structures. A pointer is essentially a value which points to another data space. An array is a collection of the same basic types of data that is contiguous in memory.We will have an elaborated discussion on them later.
Enumerated Data Type
Enumerated data type is an user defined data type i.e., user can create their own datatype. “enum” is a key word for defining such. It consists of integral consonants. Enum variable holds only a set of defined values.
Syntax
enum variablename {value1,value 2……};
Example
In the following example, we have defined two Enums.
enum week{Sunday,Monday,Tuesday,wednesday,Thursday,Friday,Saturday};
enum colors{red,blue,green};
Conclusion
In this C Tutorial, we have learnt different primary data types available, derived data types that could be formed using primary data types and also enumerated data types. In our next tutorial, we shall learn, how to use these data type along with variables in C programs.