Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C

Learn step by step



C - Data Types

Data types in C
  • Fundamental Data Types
    • Integer types
    • Floating Type
    • Character types
  • Derived Data Types
    • Arrays
    • Pointers
    • Structures
    • Enumeration

In the C programming language, data type refers to an broad system used for declaring different different type of variables or functions. The type of a variable determines how much storage space a variable occupies in memory and how the bit pattern stored is interpreted.

The data types in C can be classified as follows:

S.N.Types and Description
1Basic Types:
They are of arithmetic types and consists of the two types: (a) integer types and (b) floating-point types.
2Enumerated types:
They are also arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program.
3The type void:
The type void indicates that no value is available.
4Derived types:
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.

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

Integer Types

Table below gives you details about standard integer types with its storage sizes and value ranges:

TypeStorage sizeValue range
char1 byte-128 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
int2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long4 bytes-2,147,483,648 to 2,147,483,647
unsigned long4 bytes0 to 4,294,967,295

To get the exact size of a type or variable, you can use the sizeof operator. The expressions sizeof(type) gives the storage size of the object or type in bytes. Given the example to get the size of int type:


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

Floating-Point Types

Given table gives you details about standard floating-point types with storage sizes and value ranges and their precision:

TypeStorage sizeValue rangePrecision
float4 byte1.2E-38 to 3.4E+386 decimal places
double8 byte2.3E-308 to 1.7E+30815 decimal places
long double10 byte3.4E-4932 to 1.1E+493219 decimal places

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. Given example will print storage space taken by a float type and its range values:


#include <stdio.h>
#include <float.h>
int main()
{
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Float storage size : %d \n", sizeof(float));
printf("Precision value: %d\n", FLT_DIG );
return 0;
} RUN

Character types

char keyword is used for declaring a character type variable. Given example:

char var4='h';

Here we declare a variable of type character (i.e. var4) which is storing a character 'h'.

The size of char is 1 byte. The character data type consists of ASCII values. Each character is given a specific ASCII value. For example:

For, 'a', value =97
For, 'b', value=98
For, 'A', value=65
For, '&', value=33
For, '2', value=49
ASCII Character Codes
caption ASCII Character Codes List
 0123456789
0nulsohstxetxeotenqackbelbsht
1nlvtnpcrsosidledcldc2dc3
2dc4naksynetbcanemsubescfsgs
3rsussp!"#$%&'
4()*+,-./01
523456789:;
6<=>?@ABCDE
7FGHIJKLMNO
8PQRSTUVWXY
9Z[\]^_'abc
10defghijklm
11nopqrstuvw
12xyz{|}~del  
How to use ASCII character code table

Suppose you want to find the ASCII code of capital A then, watch the corresponding bold number in left, i.e, 6 in this case and the bold number on the top of it, i.e, 5. Then, ASCII value of 'A' equals to 65

Qualifiers

It alters the meaning of base data types to yield a new data type.

Size qualifiers:

Size qualifiers alters the size of basic data type. The keywords short and long are two size qualifiers. Given example:

long int i;

The size of int is 2 bytes or 4 bytes but, when using long keyword, that variable will be either 4 bytes of 8 bytes. Short keyword can be used in similar manner as long keyword.

Sign qualifiers:

Whether a variable can hold only positive value or both values this is specified by sign qualifiers. signed and unsigned keywords are used for sign qualifiers.

unsigned int a;
// unsigned variable can only holds zero or positive values

It is not necessary to define variable with keyword signed because, It is signed by default. Sign qualifiers can be applied to int and char data types only. A int variable of size 4 bytes it can hold data from -231 to 231-1 but, if that variable is defined unsigned, it can hold data from 0 to 232 -1.

Constant qualifiers

Constant qualifiers can be declared with const keyword. An object which is declared by const keyword cannot be modified

const int p=20;

The value of variable p cannot be changed in the program.

Volatile qualifiers:

Variable should be declared volatile whenever it's value can be changed by some external sources outside program.For indicating volatile variable keyword volatile is used.

The void Type

The void type specifies that no value is available. void is used in three situations:

S.N.Types and Description
1Function returns as void
There are various functions in C which do not return value or you can say they return void. Function with no return value or a void return value has the return type as void. For example void exit (int status);
2Function arguments as void
There are various functions in C which do not accept any parameter. Function with no parameter can accept as a void. Example, int rand(void);
3Pointers to void
A pointer of type void * represents the address of an object only not it's 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.


Related Videos