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

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -