Here is a listing of C programming questions on “Typedefs” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
typedef struct student
-
{
-
char *a;
-
}stu;
-
void main()
-
{
-
struct stu s;
-
s.a = "hi";
-
printf("%s", s.a);
-
}
a) Compile time error
b) Varies
c) hi
d) h
Answer: a
Clarification: None.
2. What will be the output of the following C code?
-
#include
-
typedef struct student
-
{
-
char *a;
-
}stu;
-
void main()
-
{
-
struct student s;
-
s.a = "hey";
-
printf("%s", s.a);
-
}
a) Compile time error
b) Varies
c) he
d) hey
Answer: d
Clarification: None.
3. What will be the output of the following C code?
-
#include
-
typedef int integer;
-
int main()
-
{
-
int i = 10, *ptr;
-
float f = 20;
-
integer j = i;
-
ptr = &j;
-
printf("%dn", *ptr);
-
return 0;
-
}
a) Compile time error
b) Undefined behaviour
c) Depends on the standard
d) 10
Answer: d
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
int (*(x()))[2];
-
typedef int (*(*ptr)())[2] ptrfoo;
-
int main()
-
{
-
ptrfoo ptr1;
-
ptr1 = x;
-
ptr1();
-
return 0;
-
}
-
int (*(x()))[2]
-
{
-
int (*ary)[2] = malloc(sizeof*ary);
-
return &ary;
-
}
a) Compile time error
b) Nothing
c) Undefined behaviour
d) Depends on the standard
Answer: a
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
int *(*(x()))[2];
-
typedef int **(*ptrfoo)())[2];
-
int main()
-
{
-
ptrfoo ptr1;
-
ptr1 = x;
-
ptr1();
-
return 0;
-
}
-
int *(*(x()))[2]
-
{
-
int (*ary)[2] = malloc(sizeof * ary);
-
return &ary;
-
}
a) Compile time error
b) Nothing
c) Undefined behaviour
d) Depends on the standard
Answer: b
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
typedef struct p
-
{
-
int x, y;
-
};
-
int main()
-
{
-
p k1 = {1, 2};
-
printf("%dn", k1.x);
-
}
a) Compile time error
b) 1
c) 0
d) Depends on the standard
Answer: a
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
typedef struct p
-
{
-
int x, y;
-
}k = {1, 2};
-
int main()
-
{
-
p k1 = k;
-
printf("%dn", k1.x);
-
}
a) Compile time error
b) 1
c) 0
d) Depends on the standard
Answer: a
Clarification: None.
8. What will be the output of the following C code?
-
#include
-
typedef struct p
-
{
-
int x, y;
-
}k;
-
int main()
-
{
-
struct p p = {1, 2};
-
k k1 = p;
-
printf("%dn", k1.x);
-
}
a) Compile time error
b) 1
c) 0
d) Depends on the standard
Answer: b
Clarification: None.