Lisa Chan
0
Q:

c++ size_t

Alias of one of the fundamental unsigned integer types.
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.
2
// size_t is a type definition for unsigned long long
// This means that writing size_t is the excact same as
// writing unsigned long long
4
// C program to demonstrate that size_t or 
// any unsigned int type should be used  
// carefully when used in a loop. 
#include<stdio.h> 
  
#define N 10 
  
int main() 
{ 
    int a[N]; 
  
    // This is fine. 
    for (size_t n = 0; n < N; ++n) { 
        a[n] = n; 
    } 
          
    // But reverse cycles are tricky for unsigned  
    // types as they can lead to infinite loops. 
    for (size_t n = N-1; n >= 0; --n) 
        printf("%d ", a[n]); 
} 
1

New to Communities?

Join the community