Hello,
The following is a sample that I've tried...
Code:
struct st
{
char ch[4];
int i;
};
int main()
{
struct st mystruct;
mystruct.ch[0] = 'A'; // Ascii value 0x41
mystruct.ch[1] = 'B'; // Ascii value 0x42
mystruct.ch[2] = 'C'; // Ascii value 0x43
mystruct.ch[3] = 'D'; // Ascii value 0x44
mystruct.i = 97;
printf("Type 1: 0x%x\n",mystruct);
printf("Type 2: %c\n",mystruct);
return 0;
}
And the output is:
Code:
Type 1: 0x44434241
Type 2: A
I've used gcc 3.1 compiler to compile it on Redhat Linux 8.0 (cant change any of them )
My Question
What type will a structure variable assume? As can be seen from above code, if I print the structure variable as an int using %x, it prints the first 4 bytes and if I print it as a character, it prints only the first byte.
So, how is it assuming it all? Or, is it just an Integer and since i've used %c, it is getting type-casted (internally or by printf) and getting printed??