English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

the size_t data type in C

The data type size_t is an unsigned integer type. It represents the size of any object (in bytes) and is returned by the sizeof operator. It is used for array indexing and counting. It is never negative. The return type of the strcspn, strlen functions is size_t.

This is the syntax of size_t in C language

const size_t var_name;

Here,

var_name-This is the name of the variable.

This is an example of size_t written in C language

Example

#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 Result

SIZE_MAX = 18446744073709551615
size = 600

In the above program, a size_t data type variable x is declared. An array of size x is also declared. The data type of the unsigned integer variable x is size_t. It is calculating the size of the variable a (in bytes).

printf("SIZE_MAX = %lu\n", SIZE_MAX);
size_t size = sizeof(a);