3.9 – Additional data types in C99 and C11

by subbu on September 9, 2013

Additional data types in C language:

Latest specification of C language is added with some of new types to meet changing requirements. In this session we are going to discourse some of the data types like long long, wchar_t, bool, fixed length integers.

 Note: All the example programs are tested under MinGw gcc

long long data type in C language:

  • It is an additional integer type in C11.
  • It takes 8 bytes, used to store integer values of higher ranges.
  • It can be defined as unsigned type.
  • The range of long long int is -263 to +263 -1, range of unsigned long long int is 0 to 264 -1
  • %lld is the format specifier to long long int and %llu is the format specifier to unsigned long long int.
#include<stdio.h>
int main()
{
long  long int x;
x=1924201005;
printf("%llu",x);
printf("\nsize of long long int is %d",sizeof(x));
return 0;
}

Output:
1924201005
size of long long int is 8

wchar_t type in C language:

Because char type takes only 1byte, it can represent maximum of 256 characters. We need more space to represent more characters of different languages.

  • wchar_t is a multi byte character type, takes 2bytes and capable to represent different characters of different non-English languages.
  • It can’t be defined as unsigned type.
  • It is available from C99 onwards.
  • %c is used as a format specifier for both char and wchar_t

Note: In case of Turbo C 3.0 add #include<stddef.h> to use wchar_t type.

#include<stdio.h>
int main()
{
wchar_t ch=3077;
printf("%c",ch);
printf("\nSize of wchar_t is %d",sizeof(wchar_t));
return 0;
}

Output:

Size of wchar_t is 2

bool type in C language:

  • It is a logical type introduced from C99. It is the replacement of _Bool type.
  • It takes just 1byte to store either true or false. 1 is stored when true is assigned and 0 is stored when false is assigned.
  • A pre-processor statement #include<stdbool.h> is added to the program to use bool type.
  • %d is the format specifier used to represent bool in printf() and scanf()
#include<stdio.h>
#include<stdbool.h>
int main()
{
bool x=40<20;/* gives 0 as 40bool y=true;/* Assigns 1 */
printf("%d",x);
printf("\n%d",y);
printf("\nSize of bool is %d",sizeof(bool));
return 0;
}

Output:
0
1
Size of bool is 1

Fixed length integers in C language:

The size of int and long are platform dependent, changes compiler to compiler.
For example say, int takes 2bytes in Turbo C 3.0, 4bytes in MinGW gcc and 4bytes in Microsoft visual C++ environment.
C99 specification defines a set of fixed length integers where size and range do not change platform to platform, helps to develop portable applications.
A pre-processor statement #include<stdint.h> is added to the program to use fixed length integers.
%d is a format specifier for all signed fixed length integers; %u is a format specifier for all unsigned fixed length integers

Name Type
int8_t 1 byte signed
uint8_t 1 byte unsigned
int16_t 2 byte signed
uint16_t 2 byte unsigned
int32_t 4 byte signed
uint32_t 4 byte unsigned
int64_t 8 byte signed
uint64_t 8 byte unsigned
#include<stdio.h>
#include<stdint.h>
int main()
{
int64_t x=-56875689;
uint64_t y=200567889;
printf("%d",x);
printf("\n%u",y);
return 0;
}

Output:
-56875689
200567889

#include<stdio.h>
#include<stdio.h>
int main()
{
printf("Size of int8_t is %d",sizeof(int8_t));
printf("\nSize of uint8_t is %d",sizeof(uint8_t));
printf("\nSize of int16_t is %d",sizeof(int16_t));
printf("\nSize of uint16_t is %d",sizeof(uint16_t));
printf("\nSize of int32_t is %d",sizeof(int32_t));
printf("\nSize of uint32_t is %d",sizeof(uint32_t));
printf("\nSize of int64_t is %d",sizeof(int64_t));
printf("\nSize of uint64_t is %d",sizeof(uint64_t));
return 0;
}

Output:
Size of int8_t is 1
Size of uint8_t is 1
Size of int16_t is 2
Size of uint16_t is 2
Size of int32_t is 4
Size of uint32_t is 4
Size of int64_t is 8
Size of uint64_t is 8

Previous post:

Next post: