i found this exercise online but i can't understand why the result is "aaaaaaaa". can you give me a hand ?
#include <stdio.h>
void a(char * s) {
while(*s++ != '\0')
printf("a");
}
int main() {
int data[5] = {-1,-3,256,-4,0};
a((char *) data);
return 0;
}
The output is aaaaaaaa (eight a:s) if the size of int is four bytes and int:s are stored using two's complement. In the numbers -1 and -3, all bytes are non-zero, so eight a:s are printed. In the number 256, the least significant (and the most significant) byte is zero so this halts the while loop.
If you print the array byte per byte with
you get
Here you can see that the ninth number is zero.