size_t data type in C

CProgrammingServer Side Programming

The datatype size_t is unsigned integral type. It represents the size of any object in bytes and returned by sizeof operator. It is used for array indexing and counting. It can never be negative. The return type of strcspn, strlen functions is size_t.

Here is the syntax of size_t in C language,

const size_t var_name;

Here,

var_name − This the name of variable.

Here is an example of size_t in C language,

Example

 Live Demo

#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
   const size_t x = 150;
   int a[x];
   for (size_t i = 0;i < x; ++i)
   a[i] = i;
   printf("SIZE_MAX = %lu\n", SIZE_MAX);
   size_t size = sizeof(a);
   printf("size = %zu\n", size);
}

Output

SIZE_MAX = 18446744073709551615
size = 600

In the above program, variable x of size_t datatype is declared. An array is also declared with the size x. size_t is a datatype of unsigned integral variable x. It is calculating the size of variable a in bytes.

printf("SIZE_MAX = %lu\n", SIZE_MAX);
size_t size = sizeof(a);
raja
Published on 05-Nov-2018 07:08:12
Advertisements