An interesting outcome of C standard

Here is a little fellow!

Run:

#include<stdio.h>

int main(){

char a[6]=”abcdef”;

printf(“\n a[5] = %c\n”, a[5]);

printf(“\n 5[a] = %c\n”, 5[a]);

return 0;

}

and be amused ! 😉

If you find this code funny due to second printf statement, just like I thought, it should be noted that in C standard,

type a[b] = *(a + b*sizeof(type))

Also sizeof character is 1 byte making this funny thing to happen!!!

Thanks : http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1

Comments

Leave a Reply

Discover more from Autumn leaves

Subscribe now to keep reading and get access to the full archive.

Continue reading