Ex-1 #include <stdio.h> struct sample { int a=0; char b='A'; float c=10.5; }; int main() struct sample s; printf("%d,%c,%f",s.a,s.b,s.c); return 0; }
01 Error: Can not initialize members here We can only declare members inside the structure, initialization of member with declaration is not allowed in structure declaration.
Ex-2 #include <stdio.h> int main() { typedef struct tag{ char str[10]; int a; }har; har h1,h2={"IHelp",10}; h1=h2; h1.str[1]='h'; printf("%s,%d",h1.str,h1.a); return 0; }
02 Ihelp,10 It is possible to copy one structure variable into another like h1=h2. Hence value of h2.str is assigned to h1.str.
Example 4 #include<stdio.h> void main() { struct india {char c; float d;}; struct world {int a[3]; char b; struct india orissa; }; struct world st ={{1,2,3},'P','q',1.4}; clrscr(); printf("%d \t %c \t %c \t %f", st.a[1],st.b,st.orissa, st.orissa.d); }
Output 4 2 p q 1.400000
p5 #include <stdio.h> int main() { union test { int i; int j; }; { int i; int j; }; union test var=10; printf("%d,%d\n",var.i,var.j); }
05 Error: Invalid Initialization You cannot initialize an union variable like this.
P5 #include<stdio.h> int main() { union a { int i; char ch[2]; }; union a u; u.ch[0]=3; u.ch[1]=1; printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); u.ch[0]=‘a’; u.ch[1]=‘b’; printf(“%d ,%d ,%c ,%c”,u.ch[0],u.ch[1],u.ch[0],u.ch[1]); return 0; }
05 3, 2, 259 97,98,a,b
05-reason Reason :- The system will allocate 2 bytes for the union. The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below. 1st byte [0]= 00000011 (3) 2nd byte [1]=00000001 (1) so it is 000000001 (256) 00000011 (3) . So 256+3=259
p6 int main() { struct value { int bit1:1; int bit3:4; int bit4:4; printf("%d\n", sizeof(bit)); return 0; }
06 2 Note :- Since C is a compiler dependent language, in Turbo C (DOS) the output will be 2, but in GCC (Linux) the output will be 4.
p7 #include<stdio.h> int main() { struct bits { int i:40; }bit; printf("%d\n", sizeof(bit)); return 0; }
07 Error :bit field too large
p8 struct marks{ int p:3; int c:3; int m:2; }; void main(){ struct marks s={2,-6,5}; printf("%d %d %d",s.p,s.c,s.m); }
08 2 2 1 ----------- Explanation: Binary value of 2: 00000010 (Select three two bit) Binary value of 6: 00000110 Binary value of -6: 11111001+1=11111010 (Select last three bit) Binary value of 5: 00000101 (Select last two bit)
p9 #include <stdio.h> struct student { }; void main() struct student s[2]; printf("%d", sizeof(s)); }
09
p10 #include <stdio.h> struct point { int x; int y; }; struct notpoint void foo(struct point); int main() struct notpoint p1 = {1, 2}; foo(p1); } void foo(struct point p) printf("%d\n", p.x);
010 Compile time error
p11 #include <stdio.h> typedef struct p { int x, y; }k; int main() struct p p = {1, 2}; k k1 = p; printf("%d\n", k1.x); }
011 1
p12 #include <stdio.h> struct p { char x : 2; int y : 2; }; int main() struct p p; p.x = 2; p.y = 1; p.x = p.x & p.y; printf("%d\n", p.x); }
012
p13 #include <stdio.h> union u { struct unsigned char x : 2; unsigned int y : 2; }p; int x; }; int main() union u u; u.p.x = 2; printf("%d\n", u.p.x); }
013 2
p14 #include <stdio.h> union u { struct unsigned char x : 2; unsigned int y : 2; }p; int x; }; int main() union u u.p.x = 2; printf("%d\n", u.p.x); }
014 Compile time error
p15 #include <stdio.h> struct p { unsigned int x : 1; unsigned int y : 1; }; int main() struct p p; p.x = 1; p.y = 2; printf("%d\n", p.y); }
015