Data Types in C
In the C programming language, data types are used to classify a value or a variable based on the size and type of data it can hold. A data type specifies the size of an object in the memory and the kind of data it can represent.
C has several built-in data types, which can be classified into the following categories:
-
Basic types: These include the most fundamental data types in C, such as char, int, float, and double. These data types are used to store characters, integers, and floating-point numbers, respectively.
-
Derived types: These are data types that are derived from basic types, such as arrays, pointers, and structures. Derived data types allow you to define more complex data structures by combining simpler data types.
-
Enumerated types: These are data types that consist of a set of named integer constants, known as an enumeration. Enumerated types are used to define a new data type that consists of a set of named integer constants.
-
Void type: This is a special data type that has no value and is used to specify the type of a function that does not return a value.
Integer Types
An integer is a data type in the C programming language that represents a whole number (without a fractional part) that can be either positive or negative. Integers can be classified into two categories:
-
Signed integers: These can represent both positive and negative numbers, and they include the types
short
,int
, andlong
. The size of the integer (i.e., the number of bits used to represent it) determines its range of values. -
Unsigned integers: These can only represent positive numbers, and they include the types
unsigned short
,unsigned int
, andunsigned long
. Like signed integers, the size of the integer determines its range of values.
The following table provides the details of standard integer types with their storage sizes and value ranges −
Type | Memory Size | Range |
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
short | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | 0 to 65,535 |
long | 8 bytes or (4bytes for 32 bit OS) | -9223372036854775808 to 9223372036854775807 |
unsigned long | 8 bytes | 0 to 18446744073709551615 |
For Example
#include <stdio.h>
int main()
{
// Declaring variables of different integer data types
short s = -32768;
int i = -2147483648;
long l = -9223372036854775807;
unsigned short us = 65535;
unsigned int ui = 4294967295;
unsigned long ul = 18446744073709551615;
// Printing the values of variables
printf("Value of s: %hd", s);
printf("Value of i: %d", i);
printf("Value of l: %ld", l);
printf("Value of us: %hu", us);
printf("Value of ui: %u", ui);
printf("Value of ul: %lu", ul);
return 0;
}
Float Data Types
In the C programming language, a float
is a data type that represents a single-precision floating-point number. It is used to store decimal numbers (numbers with a fractional part) .
Type | Storage size | Value range | Precision |
---|---|---|---|
float | 4 byte | 1.2E-38 to 3.4E+38 | 6 decimal places |
double | 8 byte | 2.3E-308 to 1.7E+308 | 15 decimal places |
long double | 10 byte | 3.4E-4932 to 1.1E+4932 | 19 decimal places |
Here is an example of how float
variables can be declared and used in C:
#include <stdio.h>
int main()
{
// Declaring a float variable and initializing it with a value
float f = 3.14;
// Printing the value of the float variable
printf("Value of f: %f ", f);
return 0;
}
The void Type
The void type specifies that no value is available. It is used in three kinds of situations −
Sr.No. | Types & Description |
---|---|
1 |
Function returns as void There are various functions in C which do not return any value or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status); |
2 |
Function arguments as void There are various functions in C which do not accept any parameter. A function with no parameter can accept a void. For example, int rand(void); |
3 |
Pointers to void A pointer of type void * represents the address of an object, but not its type. For example, a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type. |