size_t in c

116

c size_t -

for (size_t i = 0; i < 5; ++i) {
  printf(
    "element %zu is %g, \tits square is %g\n",
    i,
    A[i],
    A[i]*A[i]
  );
}

size_t in c -

// Declaration of various standard library functions.
  
// Here argument of 'n' refers to maximum blocks that can be
// allocated which is guaranteed to be non-negative.
void *malloc(size_t n);
  
// While copying 'n' bytes from 's2' to 's1'
// n must be non-negative integer.
void *memcpy(void *s1, void const *s2, size_t n);
  
// strlen() uses size_t because the length of any string
// will always be at least 0.
size_t strlen(char const *s);

size_t in c -

Output
Infinite loop and then segmentation fault

size_t in c -

// 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]);
}

Comments

Submit
0 Comments