Pages

Saturday, 7 March 2020

Data Type in C

Data Types : ANSI C provides three types of data types which are as

   Primary (Built-in) Data Types :
     voidintchardouble and float.
   Derived Data Types :
    ArrayReferences, and Pointers.
   User Defined Data Types :
   StructureUnion, and Enumeration.
   Primary(Built-in) Data Types :

Type

Size (bytes)

Format Specifier

int

at least 2, usually 4

%d

char

1

%c

float

4

%f

double

8

%lf

short int

2 usually

%hd

unsigned int

at least 2, usually 4

%u

long int

at least 4, usually 8

%li

long long int

at least 8

%lli

unsigned long int

at least 4

%lu

unsigned long long int

at least 8

%llu

signed char

1

%c

unsigned char

1

%c

long double

at least 10, usually 12 or 16

%Lf


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

8 bytes

-9223372036854775808 to 9223372036854775807

unsigned long

8 bytes

0 to 18446744073709551615


Floating-Point Types : The following table provide the details of standard floating-point types with storage sizes and value ranges and their precision. 

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


The void Type : The void type specifies that no value is available. It is used in three kinds of situations.

S.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 ); returns a pointer to void which can be casted to any data type.


Derived Data Types:

Data Type

Description

Arrays

Arrays are sequences of data items having homogeneous values. They have adjacent memory locations to store values.

References

Function pointers allow referencing functions with a particular signature.

Pointers

These are powerful C features which are used to access the memory and deal with their addresses.


 User defined Data types:

DataType

Description

Structure

It is a package of variables of different types under a single name. This is done to handle data efficiently. "struct" keyword is used to define a structure.

Union

These allow storing various data types in the same memory location. We can define a union with different members, but only a single member can contain a value at a given time. It is used for save memory."union" keyword is used to define a union.

Enum

Enumeration is a special data type that consists of integral constants, and each of them is assigned with a specific name. "enum" keyword is used to define the enumerated data type.

No comments:

Post a Comment

Program for Largest Number among three numbers

Program for Largest Number among three numbers /* c program to find largest number among 3 numbers*/ #include <stdio.h>   in...