Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';

Similar presentations


Presentation on theme: "Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';"— Presentation transcript:

1

2 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; }

3 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.

4 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; }

5 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.

6 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); }

7 Output 4 2 p q

8 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); }

9 05 Error: Invalid Initialization You cannot initialize an union variable like this.

10 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; }

11 05 3, 2, 259 97,98,a,b

12 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]= (3) 2nd byte [1]= (1) so it is (256) (3) . So 256+3=259

13 p6 int main() { struct value { int bit1:1; int bit3:4; int bit4:4;
printf("%d\n", sizeof(bit)); return 0; }

14 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.

15 p7 #include<stdio.h> int main() { struct bits { int i:40; }bit;
printf("%d\n", sizeof(bit)); return 0; }

16 07 Error :bit field too large

17 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);  }

18 08 2 2 1 ----------- Explanation:
Binary value of 2:  (Select three two bit) Binary value of 6: Binary value of -6: = (Select last three bit) Binary value of 5:  (Select last two bit)

19 p9 #include <stdio.h> struct student { }; void main()
struct student s[2]; printf("%d", sizeof(s)); }

20 09

21 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);

22 010 Compile time error

23 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); }

24 011 1

25 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); }

26 012

27 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); }

28 013 2

29 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); }

30 014 Compile time error

31 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); }

32 015


Download ppt "Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';"

Similar presentations


Ads by Google