1 Ex1 include stdio h struct sample int









![P 5 #include<stdio. h> int main() { union a { int i; char ch[2]; P 5 #include<stdio. h> int main() { union a { int i; char ch[2];](https://slidetodoc.com/presentation_image/879e06a8cfaa0cb388e133af0fcf1dfc/image-10.jpg)






















- Slides: 32
1
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; } 2
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. 3
Ex-2 • • • • #include <stdio. h> int main() { typedef struct tag{ char str[10]; int a; }har; har h 1, h 2={"IHelp", 10}; h 1=h 2; h 1. str[1]='h'; printf("%s, %d", h 1. str, h 1. a); return 0; } 4
02 • Ihelp, 10 It is possible to copy one structure variable into another like h 1=h 2. Hence value of h 2. str is assigned to h 1. str. 5
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 %f", st. a[1], st. b, st. orissa. d); } 6
Output 4 2 p q 1. 400000 7
p 5 • • • #include <stdio. h> int main() { union test { int i; int j; }; union test var=10; printf("%d, %dn", var. i, var. j); } 8
05 • Error: Invalid Initialization You cannot initialize an union variable like this. 9
P 5 #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, %dn", u. ch[0], u. ch[1], u. i); u. ch[0]=‘a’; u. ch[1]=‘b’; printf(“%d , %c”, u. ch[0], u. ch[1]); return 0; } 10
05 • 3, 2, 259 • 97, 98, a, b 11
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. 1 st byte [0]= 00000011 (3) 2 nd byte [1]=00000001 (1) so it is 00001 (256) 00000011 (3). So 256+3=259 12
p 6 • int main() { struct value { int bit 1: 1; int bit 3: 4; int bit 4: 4; }bit; printf("%dn", sizeof(bit)); return 0; } 13
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. 14
p 7 • #include<stdio. h> int main() { struct bits { int i: 40; }bit; printf("%dn", sizeof(bit)); return 0; } 15
07 Error : bit field too large 16
p 8 1. 2. 3. 4. 5. 6. 7. 8. 9. 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); } 17
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) 18
p 9 • • • #include <stdio. h> struct student { }; void main() { struct student s[2]; printf("%d", sizeof(s)); } 19
09 • 0 20
p 10 #include <stdio. h> struct point { int x; int y; }; struct notpoint { int x; int y; }; void foo(struct point); int main() { struct notpoint p 1 = {1, 2}; foo(p 1); } void foo(struct point p) { printf("%dn", p. x); } 21
010 • Compile time error 22
p 11 • • • #include <stdio. h> typedef struct p { int x, y; }k; int main() { struct p p = {1, 2}; k k 1 = p; printf("%dn", k 1. x); } 23
011 • 1 24
p 12 • • • • #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("%dn", p. x); } 25
012 • 0 26
p 13 • • • • #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("%dn", u. p. x); } 27
013 • 2 28
p 14 • • • • #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("%dn", u. p. x); } 29
014 • Compile time error 30
p 15 • • • • #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("%dn", p. y); } 31
015 • 0 32