w3resource

size_t in C language

What is size_t in C?

size_t is an unsigned integer type used to represent the size of any object (including arrays) in the particular implementation. The operator sizeof yields a value of the type size_t. The maximum size of size_t is provided via SIZE_MAX, a macro constant which is defined in the header. size_t is guaranteed to be at least 16 bits wide.

You can use the sizeof operator to obtain the size (in bytes) of the data type of its operand. The operand may be an actual type specifier (such as int or float), as well as any valid expression. When the operand is a type name, it must be enclosed in parentheses.
Here are some examples:
size_t a = sizeof(int);
size_t b = sizeof(float);
size_t c = sizeof(7);
size_t d = sizeof(3.234);
size_t e = sizeof a;
The result of the sizeof operator is of a type called size_t, which is defined in the header file <stddef.h>. size_t is an unsigned integer type, perhaps identical to unsigned int or unsigned long int; it varies from system to system.
The size_t type is often a convenient type for a loop index, since it is guaranteed to be able to hold the number of elements in any array; this is not the case with int, for example.
The sizeof operator can be used to automatically compute the number of elements in an array:

Example: size_t in C language

#include <stddef.h>
#include <stdio.h>
static const int values[] = { 1, 2, 4, 9, 15 };
#define ARRAYSIZE(x) (sizeof x/sizeof x[0])
int main (int argc, char *argv[]) 
{
    size_t i;
    for (i = 0; i < ARRAYSIZE(values); i++) 
    {
        printf("%d ", values[i]);
    }
    
    return 0;
}

Output:

1 2 4 9 15


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/c-snippets/size-t-in-c.php