C Programming Questions and Answers – Typedefs
C Programming Questions and Answers – Typedefs
1.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:a
2.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:d
3.What is the output of this C code?
#include <stdio.h>
typedef int integer;
int main()
{
int i = 10, *ptr;
float f = 20;
integer j = i;
ptr = &j;
printf(“%d\n”, *ptr);
return 0;
}
a) Compile time error
b) Undefined behaviour
c) Depends on the standard
d) 10
View Answer
Answer:d
4.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:a
5.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:b
6.What is the output of this C code?
#include <stdio.h>
typedef struct p
{
int x, y;
};
int main()
{
p k1 = {1, 2};
printf(“%d\n”, k1.x);
}
a) Compile time error
b) 1
c) 0
d) Depends on the standard
View Answer
Answer:a
7.What is the output of this C code?
#include <stdio.h>
typedef struct p
{
int x, y;
}k = {1, 2};
int main()
{
p k1 = k;
printf(“%d\n”, k1.x);
}
a) Compile time error
b) 1
c) 0
d) Depends on the standard
View Answer
Answer:a
8.What is the output of this C code?
#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);
}
a) Compile time error
b) 1
c) 0
d) Depends on the standard
View Answer
Answer:b
9.The correct syntax to use typedef for struct is.
a) typedef struct temp
{
int a;
}TEMP;
b) typedef struct
{
int a;
}TEMP;
c) struct temp
{
int a;
};
typedef struct temp TEMP;
d) All of the mentioned
View Answer
Answer:d
10.For the following expression to work, which option should be selected.
string p = “HELLO”;
a) typedef char [] string;
b) typedef char *string;
c) Both (a) and (b)
d) Such expression cannot be generated in C
View Answer
Answer:b
11.Which of the given option is the correct method for initialization?
typedef char *string;
a) *string *p = “Hello”;
b) string p = “Hello”;
c) *string p = ‘A’;
d) Not more than one space should be given when using typedef
View Answer
Answer:b
12.Which of the following is FALSE about typedef?
a) typedef follow scope rules
b) typedef defined substitutes can be redefined again. (Eg: typedef char a; typedef int a;)
c) You cannot typedef a typedef with other term.
d) All of the mentioned
View Answer
Answer:b
13.typedef which of the following may create problem in the program
a) ;
b) printf/scanf
c) Arithmetic operators
d) All of the mentioned
View Answer
Answer:d
14.typedef int (*PFI)(char *, char *)creates
a) type PFI, for pointer to function (of two char * arguments) returning int
b) Error
c) type PFI, function (of two char * arguments) returning int
d) type PFI, for pointer
View Answer
Answer:a
15.typedef declaration
a) Does not create a new type
b) It merely adds a new name for some existing type.
c) Both a & b
d) None of the mentioned
View Answer
Answer:c
16.What is the output of this C code?
#include <stdio.h>
typedef struct student
{
char *a;
}stu;
void main()
{
stu s;
s.a = “hi”;
printf(“%s”, s.a);
}s
a) Compile time error
b) Varies
c) hi
d) h
View Answer
Answer:a