c - why array shows garbage value when only one format specifier used? -
i trying print value of array in c-language.i using 3 format specifier 1 value of array , problem don't understand how other 2 values coming in output .
here code:
int arr[6] = {3,4,42,2,77,8}; printf("%d %d %d ",arr[2]); output : 42 3 4
according c standard
if there insufficient arguments format, behavior undefined.
in call of printf
printf("%d %d %d ",arr[2]);
the arguments exhausted after first format specjfjer. function call has undefined behaviour , output can contain garbage.
you shall write
printf( "%d ", arr[2] );
or example
printf( "%d %d %d ", arr[2], arr[3], arr[4] );
Comments
Post a Comment