sizeof(tab)
yields the size in bytes of all the array tab
that contains 13
elements each of them having the size equal to sizeof( int )
(that is the same as sizeof( *tab )
). That is
sizeof(tab)
is equal to 13 * sizeof( int )
that in turn can be equal to 52
provided that sizeof( int )
is equal to 4
.
But you need to output only 13 elements of the array.
So you should write for example
int tab[] = {0,2,5,3,9,0,8,9,2,0,4,3,0};
const size_t N = sizeof( tab ) / sizeof( *tab );
printf( "taille %zu: \n", N );
for ( size_t i = 0; i < N; i++ )
{
printf( "indice : %zu et valeur :%d \n", i, *(tab + i ) );
}
putchar( '\n' );
Pay attention to that to output an object of the type size_t
you need to use the conversion specifier zu
.
Here is a demonstrative program where the array output is placed in a separate function.
#include <stdio.h>
FILE * output( const int tab[], size_t n, FILE *fp )
{
fprintf( fp, "taille %zu: \n", n );
for ( size_t i = 0; i < n; i++ )
{
fprintf( fp, "indice : %zu et valeur :%d \n", i, *(tab + i ) );
}
return fp;
}
int main(void)
{
int tab[] = {0,2,5,3,9,0,8,9,2,0,4,3,0};
const size_t N = sizeof( tab ) / sizeof( *tab );
fputc( '\n', output( tab, N, stdout ) );
return 0;
}
The program output is
taille 13:
indice : 0 et valeur :0
indice : 1 et valeur :2
indice : 2 et valeur :5
indice : 3 et valeur :3
indice : 4 et valeur :9
indice : 5 et valeur :0
indice : 6 et valeur :8
indice : 7 et valeur :9
indice : 8 et valeur :2
indice : 9 et valeur :0
indice : 10 et valeur :4
indice : 11 et valeur :3
indice : 12 et valeur :0