0% found this document useful (0 votes)
21 views7 pages

08 UseDefineDataTypeWithAnswers

The document contains examples of C code using various data structures like structures, unions, enums, typedefs, and bitfields. Some key points: 1. Structures are used to define custom data types that can group together different data types. Structures allow defining related data items that may need to be accessed and used together. 2. Unions allow storing different data types in the same memory location. 3. Enums allow defining a list of named integer constants. 4. Typedefs allow defining an alias for existing types for readability and consistency. 5. Bitfields allow packing multiple fields together into a single storage unit like an int or char to save space. They are useful

Uploaded by

nitin kachhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

08 UseDefineDataTypeWithAnswers

The document contains examples of C code using various data structures like structures, unions, enums, typedefs, and bitfields. Some key points: 1. Structures are used to define custom data types that can group together different data types. Structures allow defining related data items that may need to be accessed and used together. 2. Unions allow storing different data types in the same memory location. 3. Enums allow defining a list of named integer constants. 4. Typedefs allow defining an alias for existing types for readability and consistency. 5. Bitfields allow packing multiple fields together into a single storage unit like an int or char to save space. They are useful

Uploaded by

nitin kachhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

structure

1.
#include <stdio.h>
struct student
{
char *name;
};
struct student fun(void)
{
struct student *s;
s->name = "Rajesh";
return *s;
}
int main( void )
{
struct student s1 = fun();

printf("%s", s1.name);

return 0;
}

A. Rajesh
B. error
C. RajeshRajesh
D. print memory address
Answer: B

2.
#include <stdio.h>
struct test
{
int capacity;
char *course;

}st[]={ 220, "DAC Pune",


120, "KDAC Karad ",
30, "DBDA",
60, "DESD",
120, "DMC"
};

int main (void)


{
struct test *p = st;
p += 1;
++p -> course;

printf(" %s,", p++ -> course);


printf(" %c,", *++p -> course);
printf(" %d,", p[0].capacity);
printf(" %s \n", p -> course);

return 0;
}
A. KDAC Karad , D, 30, DBDA
B. DAC Karad , B, 30, BDA
C. DAC Pune , K, 120, DAC
D. KDAC , K , 120, DAC KARAD
Answer: B

3.
#include <stdio.h>
typedef struct Cell
{
int isParent;
Cell child;
}Cell;

int main(void)
{
Cell parent,child;

parent.child=child;

return 0;
}

A. successfully complied
B. successfully complied and executed
C. Compile time error
D. none of above
Answer: C

4.
#include <stdio.h>
#include <string.h>
int main(void)
{
struct Cell
{
int isParent;
char *child;
};

struct Cell c1={23,"Remote"};


struct Cell c2=c1;

strcpy(c2.child,"center");
printf("\n %s",c1.child);
return 0;
}

A. Remote
B. center
C. Remotecenter
D. runtime error
Answer: D
5.
#include <stdio.h>
struct course
{
int capacity;
char coursename[10];
};
int main (void)
{
struct course arr[] = {
{220, "DAC Pune"},
{120, "KDAC Karad"},
{30, "DBDA"},
{60, "DESD"},
{120, "DMC"},
};

printf("%d, %d",arr[2].capacity, ( *( arr + 2)).capacity);

return 0;
}

A. 30, ASCII value of D


B. 30, 30
C. 30, ASCII value of K
D. 120, ASCII value of K
Answer: B

union
6.
#include<stdio.h>
union testUnion
{
short int num;
char ch[2];
};
int main()
{
union testUnion ut;

ut.ch[0]=4;
ut.ch[1]=4;

printf("%d", ut.num);

return 0;
}

A. 4
B. 44
C. 1028
D. 516
Answer: C
7.
#include<stdio.h>
int main(void)
{
union result
{
short int persentage;
char grade[2];
};

union result r1={88.5f,"A+"};

printf("%d",r1.grade[1]);

return 0;
}

A. 0
B. 88
C. 5
D. 8
Answer: A

8.
#include<stdio.h>
union account
{
int accNumber;
static int accID;
};
int main( void )
{
union account acc1;
printf("%d", sizeof(acc1));

return 0;
}

A. 1
B. 4
C. 8
D. compile time error
Answer: D

enum
9.
#include<stdio.h>
int main( void )
{
enum color {red,green,yellow,pink,black=0};
enum color c1,c2;
c1=red;
c2=black;
printf("color code for c1=%d,c2=%d",c1,c2);
return 0;
}
A. color code for c1=0,c2=0
B. color code for c1=-4,c2=0
C. error
D. color code for c1=1,c2=0
Answer: A

10.
#include<stdio.h>
int main( void )
{
int n1=3,n2=5;
enum Code {code1=n1,code2=n2,code3=n1+n2,code4=n2-n1};
enum Code c1=code3;

printf("value=%d",c1);

return 0;
}

A. 8
B. 5
C. error
D. 0
Answer: C

11.
#include<stdio.h>
int main( void )
{
enum months {jan=1,feb,mar,apl,may,jun,jul,aug};
enum months m;
printf("size=%d",sizeof(m));

return 0;
}

A. 1
B. 4
C. 2
D. 8
E. 0
Answer: B

typedef
12.
#include<stdio.h>
typedef char *string;
int main( void )
{
string str1 = “abc”;
*string str2= “pqr”;

printf("str1=%s str2=%s",str1,str2);

return 0;
}
A. str1=abc str2=pqr
B. str1= str2=
C. error
D. prints the addresses of str1 and str2
Answer: C

13.
typedef struct time
{
int hr;
int min;
int sec;
}TIME;
int main( void )
{
struct time time = {1, 22, 33};
TIME t1 = time;

printf("%d\n", t1.hr);

return 0;
}

A. 1
B. 0
C. Compile time error
D. Run time error
Answer: A

14.
#include <stdio.h>
int modify(int num)
{
return num + 1;
}
typedef <???> funPtr;
int main(void)
{
int num;

funPtr add = &modify;


num = add(3);
printf("result=%d",num);

return 0;
}

A. typedef int (*funPtr)(int);


B. typedef int (funPtr*)(int);
C. typedef int (funPtr*)(*int);
D. typedef int (funPtr)(int);
Answer: A
bitfield
15.
#include <stdio.h>
struct student
{
int rollNum: 5;
char grade;
};
int main( void )
{
struct student st;
printf("Address of st.rollNum is %d", &st.rollNum);
printf("Address of st.grade is %c", &st.grade);

return 0;
}

A. prints the same address of st.rollNum and st.grade


B. prints the different addresses of st.rollNum and st.grade
C. Compile time error
D. Run time error
Answer: C

16.
#include <stdio.h>
#include <string.h>
struct
{
unsigned int age : 2;
}Age;
int main(void )
{
Age.age = 3;
printf( "Age.age : %d ", Age.age );

Age.age = 4;
printf( "updated Age.age : %d\n", Age.age );

return 0;
}

A. Age.age : 3 updated Age.age : 4


B. Age.age : 0 updated Age.age : 0
C. Age.age : 3 updated Age.age : 0
D. Age.age : 3 updated Age.age : 3
Answer: C

You might also like