What is size_t in C?

前端 未结 13 1702
礼貌的吻别
礼貌的吻别 2020-11-22 05:00

I am getting confused with size_t in C. I know that it is returned by the sizeof operator. But what exactly is it? Is it a data type?

Let\'

相关标签:
13条回答
  • 2020-11-22 05:27

    size_t is an unsigned integer data type which can assign only 0 and greater than 0 integer values. It measure bytes of any object's size and returned by sizeof operator. const is the syntax representation of size_t, but without const you can run the programm.

    const size_t number;
    

    size_t regularly used for array indexing and loop counting. If the compiler is 32-bit it would work on unsigned int. If the compiler is 64-bit it would work on unsigned long long int also. There for maximum size of size_t depending on compiler type.

    size_t already define on <stdio.h> header file, but It can also define by <stddef.h>, <stdlib.h>, <string.h>, <time.h>, <wchar.h> headers.

    • Example (with const)
    #include <stdio.h>
    
    int main()
    {
        const size_t value = 200;
        size_t i;
        int arr[value];
    
        for (i = 0 ; i < value ; ++i)
        {
            arr[i] = i;
        }
    
        size_t size = sizeof(arr);
        printf("size = %zu\n", size);
    }
    

    Output -: size = 800


    • Example (without const)
    #include <stdio.h>
    
    int main()
    {
        size_t value = 200;
        size_t i;
        int arr[value];
    
        for (i = 0 ; i < value ; ++i)
        {
            arr[i] = i;
        }
    
        size_t size = sizeof(arr);
        printf("size = %zu\n", size);
    }
    

    Output -: size = 800

    0 讨论(0)
提交回复
热议问题