Typedef In C Programming
1 typedef which of the following may create problem in the program
(A) ;
(B) printf/scanf
(C) Arithmetic operators
(D) All of the mentioned
Answer : (D)
Solution :
(;) this is a termination operator in C language. printf/scanf both are the functions. +,-,*,/,%) are the arithmetic
Operators. These all may create problem in a typedef. Because Each has its own task. And typedef is used
to defined the user defined data type . That datatype will create using built-in and Derived datatype.
2 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
Answer : (B)
Solution :
User define Data type Alias works same as user define data type, and unlike array it is valid to assignone
instance of structure to other structure.
3 Which of the following is FALSE about typedef?
(A) typedef follow scope rules
(B) typedef defined substitutes can be redefined again
(C) You cannot typedef a typedef with other term.
(D) All of the mentioned
Answer : (B)
Solution :
(Eg: typedef char a; typedef int a;)
4 typedef declaration
(A) Does not create a new type
(B) It merely adds a new name for some existing type.
(C) Both a & b
1/6 http://www.comprider.in
(D) None of the mentioned
Answer : (C)
Solution :
a keyword called typedef, which you can use to give a type a new name and You can use typedef to give
aname to user defined data type as well. For example you can use typedef with structure to define a new
data type and then use that data type to define structure variables directly
5 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
Answer : (A)
Solution :
In function 'x': warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]
int (*ary)[2] = malloc(sizeof*ary);
return from incompatible pointer type [enabled by default]
return &ary;
warning: function returns address of local variable [-Wreturn-local-addr]
6 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
Answer : (A)
Solution :
Each pointer store the address of another variable. When it return the value of integer is Will be returned.
7 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)
2/6 http://www.comprider.in
(D) Such expression cannot be generated in C
Answer : (B)
Solution :
In option 1st size of array is unknown,so correct answer is option b.
8 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
Answer : (A)
Solution :
struct stu s;
This is invalid statement because above code create alias of structure stu. It is invalid to write struct keyword
with alias.
9 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
Answer : (B)
Solution :
In above code cant give alias name so p k1 = {1, 2};this statement will give error at compile time.
3/6 http://www.comprider.in
10 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
(A) a
(B) b
(C) c
(D) d
Answer : (D)
4/6 http://www.comprider.in
Solution :
The C programming language provides a keyword called typedef, which you can use to give a type a new
name. Following is an example to define a term BYTE for one-byte numbers:
BYTE b1, b2;
By convention, uppercase letters are used for these definitions to remind the user that the type name is really
a symbolic abbreviation, but you can use lowercase, as follows:
typedef unsigned char byte;
You can use typedef to give a name to user defined data type as well. For example you can use typedef with
structure to define a new data type and then use that data type to define structure variables directly as
follows:
#include <stdio.h>
#include <string.h>
typedef struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} Book;
int main( )
{
Book book;
strcpy( book.title, "C Programming");
strcpy( book.author, "Nuha Ali");
strcpy( book.subject, "C Programming Tutorial");
book.book_id = 6495407;
printf( "Book title : %s\n", book.title);
printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
return 0;
}
When the above code is compiled and executed, it produces the following result: Book title : C Programming
Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407
11 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
Answer : (D)
5/6 http://www.comprider.in
Solution :
char *a
is a pointer variable it will easily store the string So hey will be output of this program.
12 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) None of the mentioned
Answer : (B)
Solution :
typedef char *string;
This expression declare alias of string pointer in option a .and b again declare pointer of string this invalid
declaration because of we cant create pointer of alias.
13 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
Answer : (D)
Solution :
Data type Alias works same as real data type.
6/6 http://www.comprider.in