Data types in C

What is Data types in C Programming Language?

Well, Data types in C refers to an extensive system used for declaring variables and functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern for stored data is interpreted.

Data types in C or any programming language is something which specifies how to enter data into programs and what type of data to enter. C language has some predefined set of data types to handle various kinds of data that you can use in your program. These data types have different storage capacities.

Types Of Data types in C language

There are various statements on how many Data types in C to be considered. In my opinion, there are four Data types in C that you should consider.

But, you can categorise these four data types in two broad category, PRIMARY and Derived Data types in C.

c-data-types

BASIC Data types in C

They are arithmetic types and are further classified into integer types and floating-point types.

Have You enjoyed article Data types in C!

ENUMERATED Data types in C

They are again arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program.

VOID Data types in C

The type specifier void indicates that no value is available.

Derived Data types in C

Derived data types are nothing but primary data types but a little twisted or grouped together like an arraystructureunion, and pointer.

The array types and structure types are referred collectively as the aggregate types. The type of a function specifies the type of the function’s return value. We will see the basic types in the following section, whereas other types will be covered in the upcoming chapters.

Character type

Character types are used to store characters value.

Size and range of Integer type on a 16-bit machine

Type Size(bytes) Range
char or signed char 1 -128 to 127
unsigned char 1 0 to 255

Integer Types

The following table provides the details of standard integer types with their storage sizes and value ranges −

Type Storage size Value 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 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes. Given below is an example to get the size of int type

#include <stdio.h>
#include <limits.h>
int main()
{
  printf("Storage size for int : %d \n", sizeof(int));
  return 0;
}

When you compile and execute the above program, it produces the following result on Windows on which I am running:

OUTPUT: Storage size for int: 4

Have You enjoyed article Data types in C!

Floating-Point Types

Floating types are used to store real numbers.

Size and range of Integer type on a 16-bit machine

Type Size(bytes) Range
Float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
long double 10 3.4E-4932 to 1.1E+4932

The header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your programs

#include <stdio.h>
#include <float.h>
int main() 
{
printf("Storage size for float : %d \n", sizeof(float));
return 0;
}

OUTPUT: Storage size for float: 4

The void Type

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);

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);

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.


<< Prev                                                                                                              Next >>


Your friends are waiting for this content. Please Share To Aware Them!