See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/318283734
A Comprehensive Guide to Crack C Aptitude
Book · July 2017
CITATIONS
0
READS
3,044
2 authors:
Some of the authors of this publication are also working on these related projects:
Big Data : A text mining appraoch View project
Soft Computing Model for Secure Auto Program Evaluator View project
Poornima Naik
CHHATRAPATI SHAHU INSTITUTE OF BUSINESS EDUCATION AND RESEARCH
108 PUBLICATIONS   92 CITATIONS   
SEE PROFILE
Kavita Oza
Shivaji University, Kolhapur
65 PUBLICATIONS   60 CITATIONS   
SEE PROFILE
All content following this page was uploaded by Poornima Naik on 06 August 2018.
The user has requested enhancement of the downloaded file.
1
A Comprehensive Guide to
Crack C Aptitude
By
Dr.Poornima G. Naik
Dr. Kavita S. Oza
2
Acknowledgements
Many individuals share credit for this book‟s preparation. We extend our sincere thanks
to Late Prof. A.D.Shinde, the Founder Director and Managing Trustee who has been a constant
source of inspiration for us throughout our career. His support is really a driving force for us.
Also, we would like to thank Dr.R.A.Shinde, Hon‟ble Secretary, CSIBER for his whole hearted
support and continuous encouragement. We are grateful to Dr. M.M.Ali, Director CSIBER for
his invaluable guidance. We take this opportunity to thank Dr.V.M.Hilage, Trustee Member,
CSIBER, Dr. R.V.Kulkarni, H.O.D., Department of Computer Studies, Dr. R.R.Mudholkar,
H.O.D., Department of Computer Science, Shivaji University for showing a keen interest in the
matter of this book and extending all support facilities for the in-timely completion of this book.
The material covered in this book is a systematic effort taken towards solving the various queries
which we received from our students time to time, during our 15+ years tenure of teaching C at
PG level. Last but not the least we thank all faculty members and non-teaching staff of
department of computer studies, CSIBER, Kolhapur and department of Computer Science,
Shivaji University, Kolhapur who have made contribution to this book either directly or
indirectly.
Dr. Poornima.G.Naik
Dr. Kavita.S.Oza
3
Preface
It gives us an immense pleasure to bring out a book on C concepts entitled „A
Comprehensive Guide to Crack C Aptitude‟. This book is intended to serve those who
have just started their tour of C. In this book, we have tried to stick to how, why and what of
C rather than what a typical book of C offers. It serves as a reference material for those who
have just started learning C and want to learn more of C internals. This book will help you
to fill in the “gaps” by answering several „why‟ and „how to‟ questions in C. To understand
the contents of this book a working knowledge of C and some basic concepts of C are
required. This is not a complete reference to C, so some familiarity with C is essential.
C is one of the oldest languages still having its prominent position in IT industry as it
forms the basis for employment. It is learnt at School level to Postgraduate Level. In the boom
of new languages emerging everyday still there are system level projects which are developed
using C. In short C is become heart of computer science curricula and basic need for
employability. In view of this, the proposed book aims at providing understanding of C language
in a very simple way in the form of short aptitude questions and answers. C is one of the most
popular language due to its versatile nature. C forms the basis for developing other programming
languages like Java, it is also main source for developing Operating Systems like Unix/Linux.
Every Company while recruiting the students first check their C language compatibility in the
form of technical aptitude. So „C‟ becomes one of the stepping stone for the career of any
student. The proposed book aims at providing the essentials of C language needed for
employability.
There are plenty of books available in the market on C language. These books are
conservative in their approach in putting the core concepts. The proposed book is collection of
technical questions collected from various IT companies used by them for recruitment. Authors
through their experience of teaching C for more than 20 years found that there is no book in
market which has a pin point focus only on technical concepts with needed theory for
employability. There is a need for book which can give core C concepts in simple and interesting
way with compact number of lines. Proposed book is handy reference to the candidates to
4
prepare for their technical aptitudes as well as interview. It will help students during their
academics and even during their job hunting. Authors have used two approaches while writing
the proposed book one is the questions which arises in students mind while studying C and
second one is what an expert might expect from a new comer in the IT industry through their
interactions with students and alumni working in IT industries. It‟s a collection of simple and
trick questions with practically tested answers. Here 251 questions covering all core concepts of
C language are discussed along with output and explanation for output. Language used is very
simple with necessary comments wherever required. The book is the result of the questions
collected from the various resources available on the internet whose references are provided at
the end of the book. The authors have tried to provide more conceptual insight in to the c
constructs unleashing the C internals both textually and visually on many occasions. The most
threatening concepts of C such as pointers, unions can be easily comprehended from their
memory representations. The same methodology is adopted whereas needed.
As per authors knowledge there is no book in which C aptitude questions are made
interesting and simple to motivate student learning. The current book is fully dedicated to hands
on approach which will instill and create interest among students. As students are not much
interested in long theory and find it uninteresting. Proposed book will make students active user
as they have to execute the programs given and check the output. Book provides required pin
point knowledge without any unnecessary theory associated to it so it helps in quick learning.
Complex topics like pointers , abstract data types, variable declaration issues have been
explained in very simple language in the form of questions and answers which makes it more
interesting. C is interdisciplinary in the sense it is not only part of Computer science but is also
part of other faculty curricula like MBA, Electronics, Mathematics, Bioinformatics,
Biotechnology, Nano- science, Physics etc. so proposed book will prove to be a good reference
book. It stimulates interactive learning due coverage of all concepts in the form of questions and
answers.
Dr. Poornima.G.Naik
Dr. Kavita.S.Oza
5
Q. No. 1 Category : Storage Class
What is the output generated on execution of the following C Program?
#include<stdio.h>
int main()
{
extern int i; Linker Error
i = 100;
printf("%dn", sizeof(i));
return 0;
}
Output :
Explanation :
The statement
extern int i
specifies to the compiler that the memory for 'i' is allocated in some other program and that
address will be given to the current program at the time of linking. But linker finds that no other
variable of name 'i' is available in any other program with memory space allocated for it. Hence a
linker error has occurred.
Read More
A storage class in C is used as a qualifier to modify data type declaration and to define the scope
and life time of variables in a function. C defines four storage classes as shown below:
 auto
 register
 static
 extern
The default storage class is auto.
6
Rules for auto storage class
1. It can only be used to define the variables with local scope.
2. Auto variables are stored on stack.
3. Memory management for auto variables is automatic. When the auto variable goes out of
scope it is automatically deleted from memory.
Consider the following C program
#include <stdio.h>
void main()
{
int month;
auto int month; Line 6
}
On execution of the above C program, the following error message is displayed.
In the first declaration, the storage class by default is auto. Hence both declarations are identical.
Rules for register storage class
1. The variables are stored in register instead of main memory.
2. The address operator cannot be used for accessing the location where register variable
is stored.
3. The maximum size of the register storage class variable depends on the register size.
4. Depending on the implementation details and if the registers are used for storing
some other data, register storage class may default to auto.
Consider the following C program
#include <stdio.h>
#include <conio.h>
7
void main()
{
register int x=10;
clrscr();
printf("%p",&x); Line 8
getch();
}
On execution of the above program the following error message is displayed.
Since x is a register variable, it does not conform to the memory location and hence & operator
cannot be used for retrieving the address of a memory location.
Re-execute the above program by change register storage class to auto storage class. The
following output is generated.
Rules for static storage class
1. Scope of a static variable is equal to the life time of a program.
2. Static variables retain their values between function calls.
Rules for extern storage class
1. It is used for sharing data between different program files.
2. extern variable cannot be initialized.
3. When auto variables with the same name as extern variables exist, auto variables are
given the preference over extern variables.
8
Consider the following C program
#include <stdio.h>
#include <conio.h>
void main()
{
extern int x=10; Line 6
clrscr();
printf("%p",&x);
getch();
}
On execution of the above program the following error message is displayed.
Create the following files.
extern.c
#include <stdio.h>
int count=5;
prg1.c
#include <stdio.h>
#include “extern.c”
void main()
{
extern int count ;
printf("%d",count);
}
On execution of prg1.c the following output is generated.
9
Change the prg1.c program as shown below:
#include <stdio.h>
#include “extern.c”
void main()
{
int count=10 ;
printf("%d",count);
}
On execution of prg1.c now, the following output is generated.
Q. No. 2 Category : Pointer
What is the output generated on execution of the following C Program?
#include<stdio.h>
int main()
{
char *s1;
char far *s2;
char huge *s3;
printf("%d, %d, %dn", sizeof(s1), sizeof(s2), sizeof(s3));
return 0;
}
Output :
10
Explanation :
Any pointer size is 2 bytes which includes only 16-bit offset. Offset address varies from 0000 to
FFFF (in hexadecimal).
Hence,
char *c = 2 bytes.
int *i = 2 bytes
float *f = 2 bytes
However, far and huge pointers consist of two parts
16-bit segment value + 16-bit offset value
Hence,
int | char | float far *s2; = 4 bytes.
Similarly,
int | char | float huge *s2; = 4 bytes.
Read More
There are three types of pointers:
 Near
 Far and
 Huge
If a pointer variable is not qualified with a type, then it defaults to near pointer. A near pointer
can only point a 64-bit data segment or a segment no 8. Hence the limitation of near pointer is
that we can only access 64kb of data at a time using near pointer and That is near pointer cannot
access beyond the data segment like graphics video memory, text video memory etc.
A far pointer can access memory outside current segment and is 32-bit in size. In the case of a
far pointer, compiler allocates a segment register to store segment address, then another register
to store offset within current segment.
11
Difference between huge and a far pointer.
In case of far pointers, a segment is fixed whereas huge pointer can access multiple segments. In
far pointer, the segment part cannot be modified, but in the case of huge pointer it can be.
How to access segment and offset addresses of far/huge pointers?
The header file dos.h, declares three macros which return the offset address and
Segment address of a far pointer. The macro functions are:
FP_OFF(): To get offset address of far pointer.
FP_SEG(): To get segment address of far pointer.
MK_FP(): To construct far pointer address from segment and offset addresses.
Q. No. 3 Category : Abstract Data Types
What is the output generated on execution of the following C Program?
#include<stdio.h>
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Ashok"};
printf("%d, %fn", e.age, e.sal);
return 0;
}
Output :
12
Explanation :
Rule 1:
When an automatic structure is uninitialized all the members will have garbage values.
Rule 2:
When an automatic structure is partially initialized remaining elements are initialized to 0(zero).
Re-write the program as shown below:
#include<stdio.h>
#include <conio.h>
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Ashok"};
clrscr();
printf("%d, %fn", e.age, e.sal);
getch();
return 0;
}
Execute the program, the following output is generated.
Q. No. 4 Category : Operators
What is the output of the following program?
1. #include<stdio.h>
2. int main()
3. {
4. int x = 10, y = 20, z = 5, i;
5. i = x < y < z;
13
6. printf("%dn", i);
7. return 0;
8. }
Output :
Explanation :
The operator < has left associativity. Hence RHS of statement in line 5 is evaluated as shown
below:
Step 1 : Since x < y turns to be TRUE it is replaced by 1.
Step 2 : 1 < z is evaluated and found to be TRUE. Hence 1 is assigned to i.
Consider the following C program.
1. #include<stdio.h>
2. #include <conio.h>
3. int main()
4. {
5. int x = -1, y = 20, z = 0, i;
6. i = x < y < z;
7. clrscr();
8. printf("%dn", i);
9. getch();
10. return 0;
11. }
On execution of the above program, the following output is generated.
The statement i = x < y < z; is now evaluated as follows:
Step 1 : Since x < y turns to be TRUE it is replaced by 1.
14
Step 2 : Since 1 < z turns to be FALSE it is replaced by 0 Hence 0 is assigned to i
.
Change the statement 6 to the one shown below and re-execute the program.
i = x <(y < z);
The following output is generated.
Employing brackets the associativity is changed from left to right to right to left. In this case, the
statement y < z evaluates to 0 (false) and the statement x < 0 evaluates to 1 (true) and hence 1 is
assigned to i.
Q. No. 5 Category : Arrays
What is the output of the following program?
#include<stdio.h>
int main()
{
int a[5] = {2, 3};
printf("%d, %d, %dn", a[2], a[3], a[4]);
return 0;
}
Output :
When array is partially initialized, the remaining elements are automatically initialized to zero.
Change the above program to the one shown below and re-execute the program.
15
#include<stdio.h>
int main()
{
int a[5];
printf("%d, %d, %dn", a[2], a[3], a[4]);
return 0;
}
The following output is generated.
Explanation :
As described in Q.3, when an array is uninitialized all the members will have garbage values and
when the array is partially initialized remaining elements are initialized to 0(zero).
Q. No. 6 Category : Abstract Data Types
What is the output of the following program?
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
printf("%d, %d, %dn", u.ch[0], u.ch[1], u.i);
return 0;
}
16
Output :
Explanation :
A union is a special abstract data type available in C which allows different data types to be
stored in the same memory location. A union can be defined with many members, but only one
member can contain a value at any given time. In union, different members share the same
memory location. Unions provide an efficient way of using the same memory location for
multiple-purpose.
Consider the following C program :
#include<stdio.h>
#include <conio.h>
int main()
{
struct s
{
int i;
char ch[2];
};
union u
{
int i;
char ch[2];
};
struct s a;
union u b;
clrscr();
printf("Size of struct - %d Size of union - %d", sizeof(a), sizeof(b));
getch();
return 0;
}
On execution of the above program the following output is generated.
17
Hence,
Size of struct=Sum{sizeof(x) / x is a member of struct}
Size of union=Max{sizeof(x) / x is a member of union}
The following points about the struct and union should be noted:
 In struct, the members are allocated memory in a contiguous memory locations whereas
in union different members share the same memory location.
 Size of a struct is sum of the sizes of its members whereas the size of a union is
maximum size of its members.
 In struct multiple members can be accessed simultaneously whereas in union only one
member can be accessed at a time.
 In struct any member can be altered without changing the values of remaining members
whereas in union, altering any member will change the values of remaining members as
the members share the same memory location.
 When initializing a union, the initializer list must have only one member, which
initializes the first member of the union and where as in struct the initializer list can
contain all or few members. If the structure is initialized partially, the remaining elements
default to zero.
Consider the following program:
1. #include<stdio.h>
2. #include <conio.h>
3. int main()
4. {
5. union u
6. {
7. int i;
8. int j;
9. };
10. union u b={10, 20};
18
11. clrscr();
12. printf("%d %d", b.i, b.j);
13. getch();
14. return 0;
15. }
On execution of the above program, the following error message is displayed.
Change the statement 10 to the one shown below and re-execute the program:
union u b={10};
The following output is generated.
Considering the above mentioned points, the above program prints the value of u.ch[0] = 3,
u.ch[1] = 2 and it prints the value of u.i means the value of entire union size.
19
Q. No. 7 Category : Operators
What is the output of the following program?
#include<stdio.h>
int main()
{
int a = 500, b = 100, c;
if(!a >= 400)
b = 300;
c = 200;
printf("b = %d c = %dn", b, c);
return 0;
}
Output :
Explanation :
a b c
500 100
! operator has higher precedence than >= operator.
!a > 400 i.e. 0 > 400 evaluates to false. Hence statement b=300; is not executed.
a b c
500 100 200
Q. No. 8 Category : Data Types
What will be the output produced on execution of the following program?
#include<stdio.h>
int main()
{
unsigned int i = 65535; /* Assume 2 byte integer*/
while(i++ != 0)
printf("%d",++i);
20
printf("n");
return 0;
}
Output :
Infinite Loop
Explanation :
Consider the following program:
#include<stdio.h>
#include <conio.h>
int main()
{
unsigned int i = 65535; /* Assume 2 byte integer*/
i++;
clrscr();
printf("%d",i);
getch();
return 0;
}
On execution of the above program, the following output is generated.
21
Hence the data type rotates on incrementing its value once it reaches a maximum value.
Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.
Step 1:unsigned int i = 65535;
Step 2: Inside loop, the condition is initially checked and then the value of i is incremented
twice. Hence i always attains odd values and never becomes equal to zero. Hence the condition is
always true, resulting into an infinite loop.
Consider the following variant of the above program:
#include<stdio.h>
#include <conio.h>
int main()
{
unsigned int i = 65535; /* Assume 2 byte integer*/
clrscr();
while(i++ != 0);
printf("%d",++i);
printf("n");
getch();
return 0;
}
On execution of the above program, the following output is generated.
Observe the semi-colon next to the while loop. The following two statements
 Comparison of value of i with 0 and
 Incrementing the value of i
22
are repeatedly executed till the value of i becomes zero. The value of i is then incremented by 1
(i becomes 1). Finally, the value of i is incremented again (i becomes 2), before printing its
value.
Q. No. 9 Category : Data Types
What will be the output produced on execution of the following program?
int main()
{
short int i = 0;
for(i<=5 && i>=-1; ++i; i>0)
printf("%u,", i);
return 0;
}
Output :1..65535
Explanation :
In the fop loop
for(i<=5 && i>=-1; ++i; i>0)
 the expression i<=5&& i>=-1 initializes for loop
 the expression ++i is the loop condition and
 the expression i>0 is the increment expression.
23
In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to 1.
Loop condition always gets evaluated to true. Also at this point it increases i by one.
An increment expression i>0 has no effect on value of i. So for loop get executed till the limit of
integer (i.e. 65535)
Q. No. 10 Category : Built-in Functions
What will be the output produced on execution of the following program?
#include<stdio.h>
int main()
{
char ch;
if(ch = printf(""))
printf("It mattersn");
else
printf("It doesn't mattern");
return 0;
}
Output :
Explanation :
printf() function returns the number of characters printed on the console. Hence, the statement
printf(“”) returns 0, which is assigned to ch and the if() condition evaluates to false and the
statement in the else part is executed.
Consider the following program:
#include<stdio.h>
int main()
{
clrscr();
printf("%dn",printf("It matters"));
getch();
24
return 0;
}
On execution of the above program, the following output is generated.
Hence printf() returns the number of characters printed on the console.
The program given in Q.No. 10 is executed in the steps given below:
Step 1: if(ch = printf("")) here printf() does not print anything, so it returns '0'(zero).
Step 2: if(ch = 0) here variable ch has the value '0'(zero).
Step 3: if(0) Hence the if condition is not satisfied. So it prints the else statements.
Hence the output is "It doesn't matters".
Q. No. 11 Category :Data Types
What will be the output produced on execution of the following program?
#include<stdio.h>
int main()
{
float a = 0.7;
if(0.7 > a)
printf("Hin");
else
printf("Hellon");
return 0;
}
Output :
Explanation :
if(0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is
25
greater than the float variable a. Hence if condition is satisfied and it prints 'Hi'
Example:
#include<stdio.h>
int main()
{
float a=0.7;
printf("%.10f %.10fn",0.7, a);
return 0;
}
On execution of the above program, the following output is generated.
Hence always double literal is greater than floating point literal of same value.
Q. No. 12 Category : Operators
What will be the output produced on execution of the following program?
#include<stdio.h>
int main()
{
int a=0, b=1, c=3;
*((a) ?&b :&a) = a ? b : c;
printf("%d, %d, %dn", a, b, c);
return 0;
}
Output :
Explanation :
Step 1: int a=0, b=1, c=3; here variable a, b, and c are declared as integer type and initialized to
0, 1, 3 respectively.
26
Step 2: *((a) ?&b :&a) = a ? b : c; The right side of the expression(a?b:c) becomes (0?1:3).
Hence it returns the value '3'.
The left side of the expression *((a) ?&b :&a) becomes *((0) ? &b :&a). Hence this contains the
address of the variable a *(&a).
Step 3: *((a) ?&b :&a) = a ? b : c; Finally this statement becomes *(&a)=3. Hence the variable a
has the value '3'.
Step 4: printf("%d, %d, %dn", a, b, c); It prints "3, 1, 3".
Q. No. 13 Category : Operators
What will be the output produced on execution of the following program?
#include<stdio.h>
int main()
{
int x = 10, y = 20;
if(!(!x) && x)
printf("x = %dn", x);
else
printf("y = %dn", y);
return 0;
}
Output :
Explanation :
The logical not operator takes precedence and evaluates to true if the expression is false and
evaluates to false if the expression is true. In other words it reverses the value of the expression.
Step 1: if(!(!x) && x)
27
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 4: if(1 && 10)
Step 5: if (TRUE)
hence the if condition is satisfied. Hence it prints x = 10.
Q. No. 14 Category :Control Statements
What will be the output produced on execution of the following program?
#include<stdio.h>
int main()
{
int i=4;
switch(i)
{
default:
printf("This is defaultn");
case 1:
printf("This is case 1n");
break;
case 2:
printf("This is case 2n");
break;
case 3:
printf("This is case 3n");
}
return 0;
}
Output :
Explanation :
In the very beginning of switch-case statement default statement is encountered. So, it prints
"This is default".
28
In default statement there is no break; statement is included. So it prints the case 1 statement.
"This is case 1".
Then the break; statement is encountered. Hence the program exits from the switch-case block.
Modify the above program as shown below and re-execute it.
#include<stdio.h>
int main()
{
int i=4;
switch(i)
{
default:
printf("This is defaultn");
case 1:
printf("This is case 1n");
break;
case 2:
printf("This is case 2n");
break;
case 3:
printf("This is case 3n");
case 4:
printf("This is case 4n");
}
return 0;
}
The following output is generated.
Hence the rule of thumb is the matching case statement is always executed till the break
statement is encountered, irrespective of where it is placed and if the matching case statement is
not found, then default block is executed till the break statement is encountered.
29
Q. No. 15 Category : Control Statements
How many times “SIBER" gets printed?
1. #include<stdio.h>
2. int main()
3. {
4. int x;
5. for(x=-1; x<=10; x++)
6. {
7. if(x < 5) continue;
8. else break;
9. printf(“SIBER");
10. }
11. return 0;
12. }
Output :
Explanation :
No Output.
In the execution of the above C program, statement 9 is never reached. In the fop loop, the value
of x is continuously incremented till x becomes 5, after which the control will break outside the
for loop and main returns with the exit code of 0.
Q. No. 16 Category :Functions
What will be the output produced on execution of the following program?
#include<stdio.h>
int main()
{
printf(“SIBER");
main();
return 0;
}
30
Output :
Explanation :
A call stack or function stack is used for several related purposes, but the main reason for having
one is to keep track of the point to which each active subroutine should return control when it
finishes executing.
A stack overflow occurs when too much memory is used on the call stack.
Here function main() is called repeatedly and its return address is stored in the stack. After stack
memory is full. It shows stack overflow error.
Q. No. 17 Category : Pointers
What would be the equivalent pointer expression for referring the array element a[i][j][k][l]
Output :
*(*(*(*(a+i)+j)+k)+l)
Explanation :
Consider the situation in the case of 1-D integer array with the following declaration.
int a[4]={1, 2, 3, 4};
int *p;
p=a;
31
The memory allocation for p and a is as depicted in Figure.
Using a, the different elements of the array can be accessed as shown below:
a Address of first element of array.
*a Value of first element of array.
a+1 Address of second element of array.
*(a+1) Value of second element of array.
. .
. .
Continuing in this manner,
(a+i) refers to the address of (i+1)th
element of an array and *(a+i) refers to the value of (i+1)th
element of an array.
Using p, the element at the position 3 (with index starting at 0) can be accessed as shown below:
*(a+3).
Consider the following C Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[4]={1, 2, 3, 4};
int *p;
p=a;
clrscr();
printf("%d",*(a+3));
getch();
}
On execution of the above C Program, the following output is generated.
32
In order to extend the logic to 2-D array, consider the 2 Dimensional integer array as shown
below:
int **a = {1, 2, 3, 4};
int **p;
p = new int*[4]; // dynamic `array (size 4) of pointers to int`
for (int i = 0; i < 4; ++i) {
p[i] = new int[10];
// each i-th pointer is now pointing to dynamic array (size 10) of actual int values
}
To free the memory
For one dimensional array, // need to use the delete[] operator because we used the new[]
operator delete[] p; //free memory pointed by p;
For 2d Array, // need to use the delete[] operator because we used the new[] operator
for(int i = 0; i < 4; ++i)
{
delete[] p[i];//deletes an inner array of integer;
}
delete[] p; //delete pointer holding array of pointers;
33
Variable Memory Content Description
a 3000 Base address of array a
(a+1) 3002 Incrementing integer pointer advances it by 2 bytes.
*(a+1) 2000 Content of the memory location addressed by a+1
*(a+1)+1 2002 Incrementing the pointer *(a+1) by 1.
*(*(a+1)+1) 2 Content of the memory location addressed by
*(a+1)+1
Consider the following C Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int **p=new int*[2];
for(int i=0;i<2;i++)
p[i]=new int[2];
for (i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
34
*(*(p+i)+j)=i+j;
}
}
clrscr();
printf("%d",*(*(p+1)+1));
getch();
}
Q. No. 18 Category : Abstract Data Types
What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>
union employee
{
char name[15];
int age;
float salary;
};
const union employee e1;
int main()
{
strcpy(e1.name, "K");
printf("%s %d %f", e1.name, e1.age, e1.salary);
return 0;
}
Output :
Explanation :
By definition, different members of union share the same memory location. The memory
allocation for different members of union employee is ash shown below:
e1.name
1B 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B
e1.age 1B 1B
e1.salary 1B 1B 1B 1B
The member name requires 15 bytes of storage, whereas age and salary require 2 bytes and 4
35
bytes, respectively.
The ASCII code corresponding to the letter „K‟ is 75 which is displayed by e1.age member of the
union variable.
Q. No. 19 Category : Program Execution
In which stage the following code
#include<stdio.h>
gets replaced by the contents of the file stdio.h
Output :
During preprocessing
Explanation :
The preprocessor replaces the line #include <stdio.h> with the system header file of that name.
More precisely, the entire text of the file 'stdio.h' replaces the #include directive.
Q. No. 20 Category : Variable Declaration Issues
What does the following declaration mean?
int (*ptr)[10];
Output :
ptr is a pointer to an array of 10 integers
Explanation :
Consider the following two declarations :
int (*ptr)[10]; and
int *ptr[10];
The former declaration implies ptr is a pointer to an array of 10 integers (stores the address of
base address of array) while in the second declaration ptr is an array of 10 integer pointers (stores
the addresses of 10 integers). For improving readability the second declaration can be re-written
36
as
int* ptr[10];
What is difference between variable ptr in the following two declarations?
int (*ptr)[10] and
int ptr[10];
In the former case ptr is pointer variable which can store the base address of array of ten integers,
while in the latter case ptr is a constant pointer to an array to 10 integers.
Consider the following program :
#include<stdio.h>
#include<conio.h>
int main()
{
int a[]={1,2,3,4};
int (*ptr1)[10];
int ptr2[10];
ptr1=&a;
clrscr();
printf("%p %p %pn",a,&a,&a[0]);
printf("%p",ptr1);
getch();
return 0;
}
On execution of the above program, the following output is generated:
In the above program, ptr1 is a pointer to an integer array. Ptr1 is initialized with array a. Hence
a and ptr1 refer to the same entity.
Q. No. 21 Category : Pointers
What will be output if you compile and execute the following c code?
37
#include<stdio.h>
#include<conio.h>
int main()
{
int num , *ptr1 ,*ptr2 ;
ptr1 = &num ;
ptr2 = ptr1 + 2 ;
clrscr();
printf("%d",ptr2 - ptr1);
getch();
return(0);
}
Output :
Explanation :
Subtraction of two pointers is equal to the number of elements between the two memory
locations. In the above program, both ptr1 and ptr2 are integer pointers. ptr1 is initialized with
the address of integer variable num. ptr1 is incremented by 2 which advances it by 4 bytes.
Assume that the value stored in ptr1 is 1000. Then, ptr2 = 1004.
By definition, ptr2 – ptr1 = (1004-1000)/sizeof(int)
= 4/2 = 2
Hence the program generates the output 2.
Q. No. 22 Category : Variable Declaration Issues
Declare the following statement?
"An array of three pointers to chars".
Output :
char *ptr[3];
38
Explanation :
In the above declaration, if *ptr is placed in a parentheses as shown below,
char (*ptr)[3];
then, ptr becomes pointer to the array of three characters.
Q. No. 23 Category : Variable Declaration Issues
What does the following declaration signify?
int *ptr[30];
Output :
ptr is a array of 30 pointers to integers.
Explanation :
In the above declaration, if *ptr is placed in a parentheses as shown below,
int (*ptr)[30];
then, ptr becomes pointer to the array of thirty integers.
Consider the following program:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[]={1,2,3,4};
int (*ptr1)[4];
clrscr();
printf("%p %pn",a,&a[0]);
ptr1=a;
printf("%pn",*ptr1);
printf("%d",**ptr1);
getch();
return 0;
}
On execution of the above program the following output is generated:
39
Q. No. 24 Category : Variable Declaration Issues
Declare the following statement?
"A pointer to an array of three chars".
Output :
char (*ptr)[3];
Explanation :
Apply chain rule to declare the given statement.
A pointer to an
(*ptr)
array of 3
(*ptr)[3]
chars
char (*ptr)[3]
Q. No. 25 Category : Variable Declaration Issues
What does the following declaration signify?
char *arr[10];
Output :
arr is a array of 10 character pointers
Explanation :
In order to improve readability, the given statement can be re-written as:
char*arr[10];
Clearly, now the data type of arr is array of char* of ten elements.
40
Q. No. 26 Category : Variable Declaration Issues
What does the following declaration signify?
int (*pf)();
Output :
pf is a pointer to a function which returns int and does not accept any arguments
Explanation :
Consider the following function:
int test()
{
int a=10;
a++;
return a;
}
test() is a function which does not accept any parameters and returns int and hence the address of
test() function can be stored in pf as shown below:
pf = &test;
Now, the test() function can be invoked as shown below:
int result = (*pf)();
Consider the following program:
#include<stdio.h>
#include<conio.h>
int test()
{
int a=10;
a++;
return a;
}
int main()
{
int (*pf)();
41
int result;
clrscr();
pf = &test;
result=(*pf)();
printf("Result : %d",result);
getch();
return 0;
}
On execution of the above program, the following output is generated:
Q. No. 27 Category : Variable Declaration Issues
Declare the following statement?
"A pointer to a function which receives an int pointer and returns float pointer".
Output :
float *(*ptr)(int*)
Explanation :
Consider the following function:
float* test(int* x)
{
int a=*x;
float b=(float)(a/2);
return *b;
}
test() is a function which accepts a single parameter of type integer pointer and returns a floating
point pointer and hence the address of test() function can be stored in pf as shown below:
pf = &test;
Now, the test() function can be invoked as shown below:
int x=10;
42
float* y = (*pf)(&x;);
Consider the following program:
#include<stdio.h>
#include<conio.h>
float* test(int* x)
{
int a=*x;
float* b=(float *)malloc(4);
*b=(float)(a/2);
printf("%f",*b);
return b;
}
int main()
{
float* (*pf)(int*);
int x=10;
float* result;
clrscr();
pf = &test;
result = (*pf)(&x);
printf("nResult : %f",*result);
getch();
return 0;
}
On execution of the above program the following output is generated:
Q. No. 28 Category : Variable Declaration Issues
Declare the following statement?
"A pointer to a function which receives nothing and returns nothing".
43
Output :
void (*ptr)()
Explanation :
Ptr is a pointer to a function which receives nothing and returns nothing.
Q. No. 29 Category : Variable Declaration Issues
What does the following declaration signify?
void (*cmp)();
Output :
cmp is a pointer to a function with no arguments which returns void
Explanation :
Apply chain rule, in order to extract the meaning of the above declaration.
3
void (*cmp)();
1 2
cmp is a pointer
to a function with no arguments
which returns void.
Q. No. 30 Category : Pointers
Predict the output generated on execution of the following program:
#include<stdio.h>
#include<conio.h>
void main()
{
int const * p=5;
printf("%d",++(*p)); Line 6
}
Output :
Compiler error: Cannot modify a constant value.
44
Explanation :
p is a pointer to a "constant integer". Any attempt to change the value of the "constant integer"
will generate a compiler error.
What is difference between constant pointer and pointer to a constant?
Consider the declaration of ptr as shown below:
int x=10;
int* const ptr=&x;
ptr, as declared above is a constant pointer. Hence the pointer cannot be dereferenced and made
to point another integer. The following statement would result in error.
int y=20;
ptr=&y;
However, x can be modified.
At present the value of (*ptr) is 10 and after the statement
x=x+10;
the value of (*ptr) becomes 20 as shown in the following program.
Example to illustrate Constant Pointer
#include<stdio.h>
#include<conio.h>
int main()
{
int x=10;
int y=20;
int* const ptr=&x;
clrscr();
ptr=&y; Line 10
45
getch();
return 0;
}
On execution of the above program, the following compiler error is generated.
Modify the above program as shown below and re-execute it.
#include<stdio.h>
#include<conio.h>
int main()
{
int x=10;
int y=20;
int* const ptr=&x;
clrscr();
//ptr=&y;
*ptr=*ptr+10;
printf("x = %d *ptr = %d",x,*ptr);
getch();
return 0;
}
On execution of the above program, the following output is generated.
Example to illustrate Pointer to a Constant
Consider the following program:
#include<stdio.h>
#include<conio.h>
int main()
{
const int x=10;
46
const int y=20;
const int* ptr=&x;
clrscr();
ptr=&y;
*ptr=*ptr+10; Line 11
printf("x = %d *ptr = %d",x,*ptr);
getch();
return 0;
}
On execution of the above program, the following compiler error is generated.
Comment the line shown in bold and re-execute the program. The following output is generated.
Difference Between Constant Pointer and Pointer to a Constant
Constant Pointer – Pointer when declared is initialized with the address of a variable, then
onwards it cannot be dereferenced and made to point another variable using address operator.
Pointer to a Constant – A pointer is initialized with the address of a constant. The value of a
referenced entity cannot be modified through indirection operator.
The following C program clearly demonstrates the difference between constant pointer and
pointer to a constant.
#include <stdio.h>
#include <conio.h>
void main()
{
int x=10;
int y=20;
47
//Pointer to a constant
const int* p1=&y;
//Constant Pointer
int* const p2=&x;
printf("%d",++(*p2));
getch();
}
Q. No. 31 Category : Pointers
Predict the output generated on execution of the following program:
void main()
{
char s[ ]="man";
int i;
for(i=0;s[ i];i++)
printf("n%c%c%c%c",s[i],*(s+i),*(i+s),i[s]);
}
Output :
mmmm
aaaa
nnnn
Explanation :
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name
is the base address for that array. Here s is the base address. i is the index number/displacement
from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the
case of C it is same as s[i].
Q. No. 32 Category : Data Types
Predict the output generated on execution of the following program:
main()
{
float me = 1.1;
double you = 1.1;
if (me==you)
printf (“Equal");
48
else
printf (“Not Equal");
}
Output :
Explanation :
For floating point numbers (float, double, long double)the values cannot be predicted exactly.
Depending on the number of bytes, the precession with of the value represented varies. Float
takes 4 bytes and long double takes 8 bytes. So float stores 0.9 with less precision than long
double.
Rule of Thumb:
Never compare or at-least be cautious when using floating point numbers with relational
operators (== ,>,<, <=, >=,!= ) .
Q. No. 33 Category : Storage Classes
Predict the output generated on execution of the following program:
void main()
{
static int var = 5;
printf ("%d ",var--);
if (var)
main();
}
Output :
49
Explanation :
When static storage class is given, it is initialized once. The change in the value of a static
variable is retained even between the function calls. main is also treated like any other ordinary
function, which can be called recursively.
Re-execute the application by removing static qualifier.
void main()
{
int var = 5;
printf ("%d ",var--);
if (var)
main();
}
Since the static qualifier is removed in the variable declaration var, each time main() function is
invoked, var is initialized to 5 and the condition shown in bold is never satisfied. Hence main()
function is continuously invoked till stack becomes full, after which the application terminates.
Q. No. 34 Category : Pointers
Predict the output generated on execution of the following program:
void main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++)
{
printf (" %d ",*c);
++q;
}
for(j=0;j<5;j++)
{
printf(" %d ",*p);
++p;
}
}
50
Output :
Explanation :
After the execution of first for loop
After the execution of second for loop
Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and
not c, and the value stored in the memory location pointed by c is printed, the value 2 will be
printed 5 times. In second for loop, p itself is incremented and the value stored in the memory
location pointed by p is printed. So the values 2 3 4 6 5 will be printed.
Q. No. 35 Category : Operators
Predict the output or error(s) for the following:
main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}
51
Output :
Explanation :
Logical operations always give a result of 1 or 0. And also the logical AND (&&) operator has
higher priority over the logical OR (||) operator. So the expression „i++&& j++ && k++‟ is
executed first. The result of this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 ||
2 which evaluates to 1 (because OR operator always gives 1 except for „0 || 0‟ combination for
which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.
Since all the operators in the expression are post increment operators, the expression m is
evaluated first and then the variables i, j, k and l are incremented.
Q. No. 36 Category :Arrays
What will be the output of the program ?
#include<stdio.h>
int main()
{
int arr[1]={10};
printf("%dn", 0[arr]);
return 0;
}
Output :
Explanation :
Step 1: int arr[1]={10}; The variable arr is declared as an integer array with size '2' and it's first
element is initialized to value '10' (i.e. arr[0]=10)
52
Step 2: printf("%dn", 0[arr]); It prints the first element value of the variable arr. 0[arr] is same
as arr[0].
Hence the output of the program is 10.
Q. No. 37 Category : Data Types
What will be the output of the program ?
#include<stdio.h>
#include <conio.h>
void main()
{
char *p;
clrscr();
printf("%d %d ",sizeof(*p),sizeof(p));
getch();
}
Output :
Explanation :
The sizeof() operator gives the number of bytes taken by its operand. p is a character pointer,
which needs two bytes for storing the address of a character it points. Hence sizeof (p) gives a
value of 2 whereas *p is of type char which needs one byte for storing the character assigned to
it. Hence sizeof (p) gives 2.
Q. No. 38 Category : Control Statements
What will be the output of the following program ?
#include<stdio.h>
#include <conio.h>
void main()
{
int i=3;
53
clrscr();
switch(i)
{
default:
printf ("zero");
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
}
getch();
}
Output :
Explanation :
The default case can be placed anywhere inside the loop. It is executed only when all other cases
doesn't match
Q. No. 39 Category : Functions
What will be the output of the following program?
#include<stdio.h>
#include <conio.h>
void main()
{
char string[]="Hello World";
clrscr();
display(string);
getch();
}
54
void display(char *string) Line 12
{
printf("%s",string);
}
Output :
Explanation :
In third line, when the function display is encountered, the compiler doesn't know anything about
the function display. It assumes the arguments and return types to be integers, (which is the
default type). When it sees the actual function display, the arguments and type contradicts with
what it has assumed previously. Hence a compile time error occurs.
Q. No. 40 Category : Operators
What will be the output of the following program?
#include<stdio.h>
#include <conio.h>
void main()
{
int c=- -2;
clrscr();
printf("c=%d",c);
getch();
}
Output :
55
Explanation :
Here unary minus (or negation) operator is used twice.
Same Math rules applies, i.e. .minus* minus= plus.
Note:
However you cannot give like --2. Because – operator can only be applied to variables as a
decrement operator (eg., i--). 2 is a constant and not a variable.
Q. No. 41 Category : Operators
What will be the output of the following program?
#include<stdio.h>
#include <conio.h>
#define int char
void main()
{
int i=65;
clrscr();
printf("sizeof(i)=%d",sizeof(i));
getch();
}
Output :
Explanation :
Since the #define macro replaces the string int by char, on preprocessing the source code
becomes
#include<stdio.h>
#include <conio.h>
void main()
56
{
char i=65;
clrscr();
printf("sizeof(i)=%d",sizeof(i));
getch();
}
and sizeof() char data type equal to 1 is displayed.
Q. No. 42 Category : Operators
What will be the output of the following program ?
#include<stdio.h>
#include <conio.h>
void main()
{
int i=10;
i=!i>14;
clrscr();
printf("i=%d",i);
getch();
}
Output :
Explanation :
In the expression !i>14 , NOT (!) operator has more precedence than „ >‟ symbol. ! is a unary
logical operator. !i(!10) is 0 (not of true is false).0>14 is false(zero).
Modify the above program as shown below (the statement modified is shown in bold) and re-
execute it.
#include<stdio.h>
#include <conio.h>
57
void main()
{
int i=10;
i=!(i>14);
clrscr();
printf("i=%d",i);
getch();
}
On execution of the above program, the following output is generated.
Now, the expression i=!(i>14); is evaluated as follow:
i = !(0) since 10 > 14 is false
= 1
Hence the program generates the output of 1.
Q. No. 43 Category : Operators
What will be the output of the following program ?
#include<stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf("%x",-1<<4);
getch();
}
Output :
58
Explanation :
-1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are
filled with0's.The %x format specifier specifies that the integer value be printed as a hexadecimal
value.
Q. No. 44 Category : Pointers
What will be the output of the following program ?
#include<stdio.h>
#include <conio.h>
void main()
{
char s[]={'a','b','c','n','c','0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
clrscr();
printf("%d",++*p + ++*str1-32);
getch();
}
Output :
Explanation :
As shown in the above Figure, p is pointing to character 'n'. str1 is pointing to character 'a' . p is
pointing to 'n' and is incremented by one. The ASCII value of 'n' is 10, which is then
incremented to 11. Hence the value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is
incremented by and it becomes 'b'. ASCII value of 'b' is 98.
59
Now performing (11 + 98 – 32), we get 77.
Hence the output generated is 77.
Q. No. 45 Category : Arrays and Pointers
What will be the output of the following program ?
#include<stdio.h>
#include <conio.h>
void main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
clrscr();
printf("%d----%d",*p,*q);
getch();
}
Output :
Explanation :
p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third2D(which
you have not declared) it will print garbage values. *q=***a starting address of a is assigned
integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first
element of 3D array.
Q. No. 46 Category : Abstract Data Types
What will be the output of the following program ?
#include<stdio.h>
#include <conio.h>
void main()
60
{
struct xx
{
int x=3;
char name[]="hello"; Line 9
};
struct xx *s;
printf("%d",s->x);
printf("%s",s->name);
}
Output :
Compiler Error as shown below:
Explanation :
Rule : In structures, you should not initialize variables in declaration.
Q. No. 47 Category : Abstract Data Types
What will be the output of the following program?
#include<stdio.h>
#include <conio.h>
void main()
{
struct xx
{
int x;
struct yy
{
char s;
struct xx *p; Error
};
struct yy *q;
};
}
61
Output :
Explanation :
The structure yy is nested within structure xx. Hence, the elements of yy are to be accessed
through the instance of structure xx, which needs an instance of yy to be known. If the instance is
created after defining the structure the compiler will not know about the instance relative to xx.
Hence for nested structure yy, you have to declare member.
Q. No. 48 Category : Control Characters
What will be the output of the following program?
#include<stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf("nab");
printf("bsi");
printf("rha");
getch();
}
Output :
62
Explanation :
n - newline
b - backspace
r – carriage return
After first printf() statement, the characters printed are shown below, where _ indicates the
position of the cursor.
a b _
After second printf() statement(backspace, followed by characters s and i), the characters printed
are shown below:
a s i _
Carriage return means to return to the beginning of the current line without advancing
downward.
After third printf() statement (carriage return, followed by characters h and a), the characters
printed are shown below:
h a i _
Hence the output generated is “hai”
Q. No. 49 Category : Operators
What will be the output of the following program ?
#include <stdio.h>
#include <conio.h>
void main()
{
int i=5;
clrscr();
printf("%d %d %d %d %d",i++,i--,++i,--i,i);
getch();
}
63
Output :
Explanation :
The arguments in a function call are pushed into the stack from left to right. The evaluation is by
popping out from the stack. and the evaluation is from right to left, hence the result.
Step 1: Pushing arguments onto the stack from left to right.
i
--i
++i
i--
i++
Step 2 : Evaluation of arguments
4
5
5
4
5
Step 3 : Popping out elements from stack
45545
Hence the output is 45545.
Alternatively, the expression
printf("%d %d %d %d %d",i++,i--,++i,--i,i);
is evaluated as follows :
The arguments of printf() function are evaluated from right to left
64
Evaluation
i++ i-- ++i --i i
4 5 5 4 5
Display
Hence the program outputs 45545
Q. No. 50 Category : Macros
What will be the output of the following program ?
#include <stdio.h>
#include <conio.h>
#define square(x) x*x
void main()
{
int i;
i = 64/square(4);
clrscr();
printf("%d",i);
getch();
}
Output :
Explanation :
The macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 .
Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 =64
On preprocessing step, the pseudo code generated is shown below:
65
void main()
{
int i;
i = 64/4*4;
clrscr();
printf("%d",i);
getch();
}
The expression shown in bold is evaluated as:
i = (64/4)*4 = 16*4 = 64
Hence the output generated is 64.
Modify the program as shown below ( the statement changed is shown in bold) and re-execute it.
#include <stdio.h>
#include <conio.h>
#define square(x) (x*x)
void main()
{
int i;
i = 64/square(4);
clrscr();
printf("%d",i);
getch();
}
On execution of the above program, the following output is generated.
Q. No. 51 Category : Operators
What will be the output of the following program?
66
#include <stdio.h>
#include <conio.h>
void main()
{
char *p=“siber",*p1;
p1=p;
clrscr();
while(*p!='0')
++*p++;
printf("%s %s",p,p1);
getch();
}
Output :
Explanation :
++*p++ will be parsed in the given order. The expression ++*p++ is evaluated as shown in the
following steps:
1. *p that is value at the location currently pointed by p will be taken. ++*p, the retrieved
value will be incremented.
2. The location will be incremented that is p++ will be executed
Hence, in the while loop initial value pointed by p is „s‟, which is changed to „t‟ by executing
++*p and pointer moves to point, „i‟ which is similarly changed to „j‟ and so on. Thus, we obtain
value in p becomes “tjcfs” and since p reaches „0‟ and p1 points to p thus p1doesnot print
67
anything.
Hence after the execution of while loop, the situation is as depicted below:
Q. No. 52 Category : Control Statements
What will be the output of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
int i=3;
clrscr();
switch(i)
{
default:
printf ("zero");
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
}
getch();
}
Output :
68
Explanation :
The default case can be placed anywhere inside the loop. It is executed only when all other cases
do not match.
Q. No. 53 Category : Preprocessors
What will be the output of the following program ?
#include <stdio.h>
#include <conio.h>
#define a 10
void main()
{
#define a 50
clrscr();
printf("%d",a);
getch();
}
Output :
Explanation :
The preprocessor directives can be redefined anywhere in the program. So the most recently
assigned value will be taken.
Consider the following program:
#include <stdio.h>
#include <conio.h>
#define a 10
69
void main()
{
clrscr();
printf("%dn",a);
#define a 50
printf("%d",a);
getch();
}
On preprocessing, the main function is modified as shown below:
void main()
{
clrscr();
printf("%dn",10);
printf("%d",50);
getch();
}
The lines affected during preprocessing step are shown in bold.
On execution of the above program, the following output is generated:
Q. No. 54 Category : Preprocesors
What will be the output of the following program?
#include <stdio.h>
#include <conio.h>
#define clrscr() 100
void main()
{
clrscr();
printf("%dn",clrscr());
}
70
Output :
Explanation :
Preprocessor executes as a separate pass before the execution of the compiler. So textual
replacement of clrscr() to 100 occurs. The input program to compiler looks like this :
void main()
{
100;
printf("%dn",100);
}
Note:100; is an executable statement but with no action. So it doesn't give any problem
Q. No. 55 Category : Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf("%p",main);
getch();
}
71
Output :
Explanation :
Function names are just addresses (just like array names are addresses). main() is also a function.
So the address of function main will be printed. %p in printf specifies that the argument is an
address. They are printed as hexadecimal numbers.
Q. No. 56 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=10;
i=!i>14;
clrscr();
printf("i=%d",i);
getch();
}
Output :
Explanation :
In the expression !i>14 , NOT (!) operator has more precedence than „ >‟ symbol. ! is a unary
72
logical operator. !i(!10) is 0 (not of true is false).0>14 is false (zero).
Q. No. 57 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
char far *farther,*farthest;
clrscr();
printf("%d..%d",sizeof(farther),sizeof(farthest));
getch();
}
Output :
Explanation :
The second pointer is of near type and not a far pointer.
Modify the above program as shown below and re-execute it.
#include <stdio.h>
#include <conio.h>
void main()
{
char far* farther,farthest;
clrscr();
printf("%d..%d",sizeof(farther),sizeof(farthest));
getch();
}
The statement altered is shown in bold. On execution of the above program, the following output
73
is generated.
Now, farther is of type far pointer to a char and farthest is of type char.
Q. No. 58 Category : Variable Declaration Issues
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=400,j=300;
clrscr();
printf("%d..%d");
getch();
}
Output :
Explanation :
printf takes the values of the first two assignments of the program. Any number of printf's may
be given. All of them take only the first two values. If more number of assignments are given in
the program, then printf will take garbage values.
The numbers printed are in reverse order which is attributed to the nature of printf() function
evaluation. The arguments of printf() function are pushed on to the stack and are then output by
74
popping the elements from the stack.
Modify the above program as shown below and re-execute it. The statement altered is shown in
bold.
#include <stdio.h>
#include <conio.h>
void main()
{
int i=400,j=300;
clrscr();
printf("%d..%d",i,j);
getch();
}
On execution of the above program, the following output is generated.
Q. No. 59 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
char *p;
p="Hello";
clrscr();
printf("%cn",*&*p);
getch();
}
75
Output :
Explanation :
* is a dereference operator & is a reference operator. They can be applied any number of times
provided it is meaningful. Here p points to the first character in the string "Hello". *p
dereferences it and so its value is H. Again & references it to an address and * dereferences it to
the value H.
Alternatively, *&*p = *p since *&=1
Q. No. 60 Category : Control Statements
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1;
while (i<=5)
{
printf("%d",i);
if (i>2)
goto here; Error
i++;
}
}
fun()
{
here:
printf("PP");
}
76
Output :
Explanation :
Labels have function scope, in other words the scope of the labels is limited to functions. The
label 'here' is available in function fun(). Hence it is not visible in function main.
Q. No. 61 Category : Arrays
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
int i;
char *t;
t=names[3];
names[3]=names[4];
names[4]=t; Error
for (i=0;i<=4;i++)
printf("%s",names[i]);
}
Output :
Explanation :
Array name is a constant pointer. So it cannot be modified. Hence array names cannot appear on
77
the left hand side of the expression.
Q. No. 62 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=5;
clrscr();
printf("%d",i++ + ++i);
getch();
}
Output :
Explanation :
The expression is evaluated from right to left. The value of i is pre-incremented and becomes 6.
Then the same value is used in the first operand, resulting in 12 as the value of the expression.
After the evaluation of the expression, the value of i is post-incremented and becomes 7. Modify
the above program to add the following statement at the end.
printf("n%d",i);
On re-execution of the program, the following output is generated.
78
Change the expression to the one shown below and re-execute the program :
printf("%d",i++ + i++ + ++i);
7 + 6 + 6 = 19
The following output is generated:
Change the expression to the one shown below and re-execute the program :
printf("%d",++i + i++ + ++i);
8 + 6 + 6 = 20
The following output is generated:
Q. No. 63 Category : Control Statements
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1,j=2;
switch(i)
{
case 1: printf("GOOD");
break;
case j: printf("BAD"); Line 11
break;
}
}
79
Output :
Explanation :
The case statement can have only constant expressions (this implies that we cannot use variable
names directly so an error).
Note : Enumerated types can be used in case statements.
Q. No. 64 Category : Basic Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
printf("%d",scanf("%d",&i)); // value 10 is given as input here
getch();
}
Output :
Explanation :
scanf returns number of items successfully read and not I/0. Here 10 is given as input which
80
should have been scanned successfully. So number of items read is 1.
Q. No. 65 Category : Preprocessors
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
#define f(g,g2) g##g2
void main()
{
int var12=100;
clrscr();
printf("%d",f(var,12));
getch();
}
Output :
Explanation :
## is string concatenation operator. Hence during the preprocessing phase, f(var,12) is evaluated
as var12 and printf() statement in the above program becomes,
printf(“%d”, var12);
which displays 100.
Q. No. 66 Category : Basic Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
81
void main()
{
int i=0;
clrscr();
for(;i++;printf("%d",i)) ;
printf("%d",i);
getch();
}
Output :
Explanation :
before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0
(false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).
Q. No. 67 Category : Variable Declaration Issues
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
printf("%d", out); Line 6
}
int out=100;
Output :
82
Explanation :
The rule is that a variable is available for use from the point of declaration. Even though a is a
global variable, it is not available for main. Hence an error
Q. No. 68 Category : Storage Classes
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
extern out;
clrscr();
printf("%d", out);
getch();
}
int out=100;
Output :
Explanation :
This is the correct way of writing the previous program. Since the global variables are not
available at the time of compilation, extern statement informs the compiler that „out‟ variable
will be available to the program at the time of execution.
Q. No. 69 Category : Functions
Predict the output or error(s) for the following:
83
#include <stdio.h>
#include <conio.h>
void main()
{
show();
}
void show() Line 9
{
printf("I'm the greatest");
}
Output :
Explanation :
When the compiler sees the function show() it doesn't know anything about it. So the default
return type (i.e., int) is assumed. But when compiler sees the actual definition of show()
mismatch occurs since it is declared as void. Hence the error.
The solutions are as follows:
1. declare void show() in main() .
2. define show() before main().
3. declare extern void show() before the use of show().
Add the following statement before main() function and re-execute the application.
void show();
On re-execution of the application, the following output is generated.
84
Q. No. 70 Category :Arrays and Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main( )
{
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
clrscr();
printf(“%u %u %u %d n”,a,*a,**a,***a);
printf(“%u %u %u %d n”,a+1,*a+1,**a+1,***a+1);
getch();
}
Output :
Explanation :
The 12 elements of the 3D array are organized in contiguous memory locations as shown below:
2 4 7 8 3 4 2 2 2 3 3 4
Consider the following program :
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][2]={11,22,33,44};
85
clrscr();
printf("a : %un",a);
printf("a[0] : %un",a[0]);
printf("a[1] : %un",a[1]);
printf("a[0][0] : %d t**a : %d tt*a[0] : %dn",a[0][0], **a, *a[0]);
printf("a[0][1] : %d t*(*(a+0)+1) : %d t*(a[0]+1) : %dn",a[0][1], *(*(a+0)+1), *(a[0]+1));
printf("a[1][0] : %d t*(*(a+1)+0) : %d t*a[1] : %dn",a[1][0], *(*(a+1)+0), *a[1]);
printf("a[1][1] : %d t*(*(a+1)+1) : %d t*(a[1]+1) : %dn",a[1][1], *(*(a+1)+1), *(a[1]+1));
getch();
}
On execution of the above program, the following output is generated.
Consider the following program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][2][2]={11,22,33,44,55,66,77,88};
clrscr();
printf("a[0][0][0] : %d t*(a[0][0]+0) : %d t*(*(a[0]+0)+0) ta[0] : %dt*(*(*(a+0)+0)+0 :
%dnn",a[0][0][0], *(a[0][0]+0), *(*(a[0]+0)+0),*(*(*(a+0)+0)+0));
printf("a[0][0][1] : %d t*(a[0][0]+1) : %d t*(*(a[0]+0)+1) ta[0] : %dt*(*(*(a+0)+0)+1 :
%dnn",a[0][0][1], *(a[0][0]+1), *(*(a[0]+0)+1), *(*(*(a+0)+0)+1));
printf("a[0][1][0] : %d t*(a[0][1]+0) : %d t*(*(a[0]+1)+0) ta[0] : %dt*(*(*(a+0)+1)+0 :
%dnn",a[0][1][0], *(a[0][1]+0), *(*(a[0]+1)+0), *(*(*(a+0)+1)+0));
printf("a[0][1][1] : %d t*(a[0][1]+1) : %d t*(*(a[0]+1)+1) ta[0] : %dt*(*(*(a+0)+1)+1 :
%dnn",a[0][1][1], *(a[0][1]+1), *(*(a[0]+1)+1), *(*(*(a+0)+1)+1));
86
printf("a[1][0][0] : %d t*(a[1][0]+0) : %d t*(*(a[1]+0)+0) ta[0] : %dt*(*(*(a+1)+0)+0 :
%dnn",a[1][0][0], *(a[1][0]+0), *(*(a[1]+0)+0), *(*(*(a+1)+0)+0));
printf("a[1][0][1] : %d t*(a[1][0]+1) : %d t*(*(a[1]+0)+1) ta[0] : %dt*(*(*(a+1)+0)+1 :
%dnn",a[1][0][1], *(a[1][0]+1), *(*(a[1]+0)+1), *(*(*(a+1)+0)+1));
printf("a[1][1][0] : %d t*(a[1][1]+0) : %d t*(*(a[1]+1)+0) ta[0] : %dt*(*(*(a+1)+1)+0 :
%dnn",a[1][1][0], *(a[1][1]+0), *(*(a[1]+1)+0), *(*(*(a+1)+1)+0));
printf("a[1][1][1] : %d t*(a[1][1]+1) : %d t*(*(a[1]+1)+1) ta[0] : %dt*(*(*(a+1)+1)+1 :
%dnn",a[1][1][1], *(a[1][1]+1), *(*(a[1]+1)+1), *(*(*(a+1)+1)+1));
getch();
}
On execution of the above program, the following output is generated.
Q. No. 71 Category :Arrays
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
87
void main( )
{
int a[ ] = {10,20,30,40,50},j,*p;
clrscr();
for(j=0; j<5; j++)
{
printf(“%d” ,*a);
a++; Line 11
}
p = a;
for(j=0; j<5; j++)
{
printf(“%d ” ,*p);
p++;
}
getch();
}
Output :
Explanation :
Error is in line with statement a++. The operand must be an lvalue and may be of any of scalar
type for the any operator, array name only when subscripted is an lvalue. Simply array name is a
non-modifiable lvalue.
Modify the above program as shown below (modified statement is shown in bold) and re-execute
the application.
#include <stdio.h>
#include <conio.h>
void main( )
{
88
int a[ ] = {10,20,30,40,50},j,*p;
clrscr();
for(j=0; j<5; j++)
{
printf("%d " ,*(a+j));
}
printf("n");
p = a;
for(j=0; j<5; j++)
{
printf("%d " ,*p);
p++;
}
getch();
}
On execution of the application, the following output is generated.
a cannot be incremented or decremented (since a is a constant pointer), the correct way of
accessing the elements of an array using pointer notation is
*(a+i)
Q. No. 72 Category :Arrays and Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main( )
{
static int a[ ] = {0,1,2,3,4};
int *p[ ] = {a,a+1,a+2,a+3,a+4};
int **ptr = p;
ptr++;
clrscr();
printf(“n %d %d %d”, ptr-p, *ptr-a, **ptr);
89
*ptr++;
printf(“n %d %d %d”, ptr-p, *ptr-a, **ptr);
*++ptr;
printf(“n %d %d %d”, ptr-p, *ptr-a, **ptr);
++*ptr;
printf(“n %d %d %d”, ptr-p, *ptr-a, **ptr);
getch();
}
Output :
Explanation :
Subtraction of pointers gives total number of objects between them. For computing the
difference between two pointers ptr1 and ptr2, employ the following formula:
(ptr2 - ptr1) / Size of Data Type
Numerically Subtraction ( ptr2-ptr1 ) differs by 4
As both are Integers they are numerically Differed by 4 and technically by 2 objects
Suppose both pointers of float the they will be differed numerically by 8 and technically by 2
objects
Consider the below statement and refer the following table –
int num = ptr2 - ptr1;
and
If Two Pointers are of
Following Data Type
Numerical Difference Technical Difference
Integer 2 1
Float 4 1
Character 1 1
90
Q. No. 73 Category :Arrays and Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main( )
{
static char *s[ ] = {“black”, “white”, “yellow”, “violet”};
char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
**++p;
clrscr();
printf(“%s”,*--*++p + 3);
getch();
}
Output :
Explanation :
In this problem, we have an array of char pointers pointing to start of 4 strings. Then we have ptr
which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a
pointer of type char. p holds the initial value of ptr, i.e. p = s+3. The next statement increments
value in p by 1, thus now value of p = s+2. In the printf statement the expression is evaluated
*++p gets value s+1 then the pre decrement is executed and we get s+1 – 1 = s the indirection
operator now gets the value from the array of s and adds 3 to the starting address. The string is
printed starting from this position. Thus, the output is „ck‟.
91
Hence, *p = 1012
**p = s[2].
The printf() statement, contains the following statement,
*--*++p + 3
which is evaluated as,
(*--*++p)+3
++p = 1006
*++p = s+1
--*++p =s
--*++p + 3 =
b l a c k 0
Hence printf() statement will print from c till 0 character is encountered i.e. ck.
Hence the program generates the output ck.
Q. No. 74 Category :Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i, n;
char *x = “girl”;
n = strlen(x);
92
*x = x[n];
clrscr();
for(i=0; i<4;i++)
{
printf(“%sn”,x);
x++;
}
getch();
}
Output :
Explanation :
Here a string (a pointer to char) is initialized with a value “girl”. The strlen() function returns the
length of the string, thus n has a value 4. The next statement assigns value at the nth location
(„0‟) to the first location. Now the string becomes “0irl” . Now the printf statement prints the
string after each iteration it increments it starting position. Loop starts from 0 to 4. The first time
x[0] = „0‟ hence it prints nothing and pointer value is incremented. The second time it prints
from x[1] i.e. “irl” and the third time it prints “rl” and the last time it prints “l” and the loop
terminates.
Q. No. 75 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=-1;
+i;
printf("i = %d, +i = %d n",i,+i);
getch();
}
93
Output :
Explanation :
Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just
because it has no effect in the expressions (hence the name dummy operator).
Q. No. 76 Category :Program Execution
What are the files which are automatically opened when a C program is executed?
Output :
stdin, stdout, stderr (standard input, standard output, standard error).
Q. No. 77 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
char *cptr,c;
void *vptr,v; Line 7
c=10; v=0; Line 8
cptr=&c; vptr=&v;
getch();
printf("%c %v",c,v);
clrscr();
}
94
Output :
Explanation :
You can create a variable of type void * but not of type void, since void is an empty type. In the
second line you are creating variable vptr of type void * and v of type void hence an error.
The correct way of writing the above program is :
#include <stdio.h>
#include <conio.h>
void main()
{
char *cptr,c;
void *vptr;
c=65;
cptr=&c; vptr=&c;
clrscr();
printf("%c %c",*cptr, *(char*)vptr);
getch();
}
On execution of the above program, the following output is generated.
In the above program, both *cptr and (char*)vptr point to character „A‟ with ASCII value of 65.
Since vptr is a void pointer it requires explicit type casting to char *.
Q. No. 78 Category : Operators
Predict the output or error(s) for the following:
95
#include <stdio.h>
#include <conio.h>
void main()
{
char *str1="abcd";
char str2[]="abcd";
clrscr();
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
getch();
}
Output :
Explanation :
In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second
sizeof the name str2 indicates the name of the array whose size is 5(including the '0' termination
character). The third sizeof is similar to the second one.
Q. No. 79 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int k=1;
clrscr();
printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE");
getch();
}
96
Output :
Explanation :
When two strings are placed together (or separated by white-space) they are concatenated (this is
called as "stringization" operation). So the string is as if it is given as "%d==1 is %s". The
conditional operator( ?: ) evaluates to "TRUE".
The expression
printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE");
is evaluated as follows:
printf(“%d==1 is %s,1,TRUE);, which displays
1==1 is TRUE, hence the output generated by the program.
Q. No. 80 Category : Preprocessors
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
#define max 5
#define int arr1[max]
void main()
{
typedef char arr2[max];
arr1 list={0,1,2,3,4}; Line 10
arr2 name="name";
clrscr();
printf("%d %s",list[0],name);
getch();
}
97
Output :
Explanation :
arr2 is declared of type array of size 5 of characters. So it can be used to declare the variable
name of the type arr2. But it is not the case of arr1. Hence an error.
Rule of Thumb:
#define directives are used for textual replacement whereas typedefs are used for declaring new
types.
Q. No. 81 Category : Storage Classes
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
int i=10;
void main()
{
extern int i;
{
int i=20;
{
const volatile unsigned i=30;
clrscr();
printf("%dn",i);
}
printf("%dn",i);
}
printf("%dn",i);
getch();
}
98
Output :
Explanation :
'{' introduces new block and thus new scope. In the inner most block i is declared as, const
volatile unsigned which is a valid declaration. i is assumed of type int. So printf prints 30. In the
next block, i has value 20 and so printf prints 20. In the outermost block, i is declared as extern,
so no storage space is allocated for it. After compilation is over the linker resolves it to global
variable i (since it is the only variable visible there). So it prints i's value as 10.
Remove extern keyword and re-execute the application. The following output is generated.
i becomes uninitialized local variable. Hence last printf statements prints garbage.
Remove the following statement
extern int i;
and re-execute the application. The following output is generated.
The last printf() statement now uses global variable i.
Q. No. 82 Category : Variab le Scope and Life Time
Predict the output or error(s) for the following:
99
#include <stdio.h>
#include <conio.h>
main()
{
int *j;
{
int i=10;
j=&i;
}
clrscr();
printf("%d",*j);
getch();
}
Output :
Explanation :
The variable i is a block level variable and the visibility is inside that block only. But the lifetime
of i is lifetime of the function so it lives up to the exit of main function. Since the variable i is
still allocated space, *j prints the value stored in i since j points i.
The rule of thumb is that i can be accessed through a pointer and not by its name. To demonstrate
this, change the printf() statement to the one shown below and re-execute the program.
printf("%d",i);
On execution of the program now, the following output is generated. i is not reachable to the
printf() statement but is accessible through pointer variable j.
100
Q. No. 83 Category : Arrays and Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
clrscr();
printf("%d..%d",*p,*q);
getch();
}
Output :
Explanation :
p=&a[2][2][2] you declare only two 2D arrays. but you are trying to access the third2D (which
you have not declared) it will print garbage values. In the statement, *q=***a starting address of
a is assigned integer pointer. Now q is pointing to starting address of a and if you print *q, it will
print first element of 3D array.
Q. No. 84 Category : Storage Classes
Predict the output or error(s) for the following:
101
#include <stdio.h>
#include <conio.h>
void main()
{
register i=5;
char j[]= "hello";
clrscr();
printf("%s %d",j,i);
getch();
}
Output :
Explanation :
If you declare i as register, compiler will treat it as ordinary integer and it can store an integer
value. i variable may be stored either in register or in memory.
Q. No. 85 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=5,j=6,z;
clrscr();
printf("%d",i+++j);
getch();
}
102
Output :
Explanation :
There are two possibilities of the expression
i+++j
being evaluated.
Case 1 : (i++) + j
In this case the value of the expression is 11.
Case 2 : i + (++j)
In this case the value of the expression is 12.
As seen from the output, Case 1 is the correct way of evaluating the expression. Hence, the
expression i+++j is treated as (i++ + j).
Q. No. 86 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=-1,j=-1,k=0,l=2,m,n;
m=i++&&j++&k++||l++;
clrscr();
printf("%d %d %d %d %dn",i,j,k,l,m);
n=++i&&++j&++k||++l;
printf("%d %d %d %d %d",i,j,k,l,n);
getch();
}
103
Output :
Explanation :
In the expression,
m=(i++&&j++&k++)||(l++);
In the above expression,& is a bitwise AND operator and&& is a logical AND operator. & has
higher precedence compared to &&. Hence the above expression is identical to
m=(i++&& (j++&k++) )||(l++);
since i and j are true, third condition is evaluated and result is false, since k is zero. Since the
result in the first parenthesis is false, the condition in the second parenthesis is also evaluated and
the result of the complex condition becomes true. Hence 1 is assigned to m. After the evaluation
of the expression, the values of i, j, k and l are incremented.
Hence, i=0, j=0, k=1, l=3 and m=1.
In the expression,
n=++i&&++j&++k||++l;
since i and j are true, third condition is evaluated and result is false, since k is zero. Since the
result in the first parenthesis is false, the condition in the second parenthesis is also evaluated and
the result of the complex condition becomes true. Hence 1 is assigned to m. After the evaluation
of the expression, the values of i, j, k and l are incremented.
Hence, i=0, j=0, k=1, l=3 and m=1.
104
i j k l m n
Initial Values -1 -1 0 2 - -
On execution of
m=i++&&j++&&k++||l++;
0 0 1 3 1 -
On execution of
n=++i&&++j&&++k||++l;
1 1 2 4 1 1
Replace the bitwise & operator with the logical && operators and re-execute the program.
m=i++&&j++&&k++||l++;
n=++i&&++j&&++k||++l;
On re-execution of the program, the following output is generated.
The following table depicts the values of different variables on execution of different statements.
i j k l m n
Initial Values -1 -1 0 2 - -
On execution of
m=i++&&j++&&k++||l++;
0 0 1 3 1 -
On execution of
n=++i&&++j&&++k||++l;
1 1 2 3 1 1
Q. No. 87 Category : Data Types
Predict the output or error(s) for the following:
105
#include <stdio.h>
#include <conio.h>
main()
{
signed char i=0;
for(;i>=0;i++);
clrscr();
printf("%d",i);
getch();
}
Output :
Explanation :
Signed char variable assumes the value from -128 to 127. In the for loop once the value of i
becomes 127, incrementing it further assigns the value -128 to i and the condition becomes false
(observe the semicolon at the end of for statement). Hence i=-128 gets printed in the subsequent
statement.
Q. No. 88 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
main()
{
char c='A';
c=c|32;
clrscr();
printf("%c",c);
getch();
}
106
Output :
Explanation :
Binary representation of A is 1 0 0 0 0 0 1 and binary representation of 32 is 1 0 0 0 0 0.
Performing the logical OR operation on the two operands, we get, 1 1 0 0 0 0 1 whose decimal
equivalent is 97 an ASCII code representation of a, hence the output generated.
Q. No. 89 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
main()
{
int i=0,j=1;
clrscr();
printf("%d %d %dn",i++ && ++j,i,j);
printf("%d",i);
getch();
}
Output :
Explanation :
The expression
printf("%d %d %dn",i++ && ++j,i,j);
is evaluated as follows:
107
The value of i is 0 (false). Since && operator employs short-circuit evaluation, the second
operand is not evaluated since the value of first operand is false. Hence j is not pre-incremented
and its value remains to be 1. After the first printf() statement, the value of i is incremented to 1
as is evident from second printf() statement.
Add the following printf() statement at the end and re-execute the program.
printf(“n%d”,j);
On execution of the program, the following output is generated.
Change the value of i to 1 in the first statement as shown below
int i=1,j=1;
and re-execute the program. The following output is generated.
Change the order of arguments in printf() statement as shown below
printf("%d %d %dn",i,j,i++ && ++j);
and re-execute the application. The following output is generated.
108
This reveals that in the printf() statement, the operands are evaluated from right to left.
Q. No. 90 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int x=011|0x10;
printf("x=%d",x);
getch();
}
Output :
Explanation :
The prefix 0 indicates octal representation while 0x indicates hexadecimal representation. Hence
binary representation of octal 011 is (decimal 9) 1 0 0 1 and the binary representation of
hexadecimal 0x10 (decimal 16) is 1 0 0 0 0. Performing the binary OR operation on them, we
get, 1 1 0 0 1 whose decimal equivalent is 25, hence the output generated.
Q. No. 91 Category : Operators
What will be output if you compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
109
void main()
{
int i,j=0,k=7;
i=j++&k++;
clrscr();
printf("j : %d k : %d i : %dn",j,k,i);
getch();
}
Output :
Explanation :
Short-circuit evaluation does not apply to bitwise AND operator (&). Hence in the expression
i=j++&k++;
the second statement, k++ is evaluated even if the first condition is false. The value assigned to i
is the bitwise AND operation between j and k, after that the values of j and k are incremented.
Since j is 0, the result of expression is 0 which is assigned to i, then j and k are incremented to 1
and 8, respectively.
Q. No. 92 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1;
clrscr();
i=(i<<=1%2);
printf("%d",i);
getch();
}
110
Output :
Explanation :
The expression
i=(i<<=1%2);
is evaluated as follows:
1 % 2 evaluates to 1.
Left shifting bits of an integer n times is equivalent to multiplying it by 2n
.
Hence, left shifting bits of i by 1 bit is equivalent to multiplying it by 2.
Hence 2 is assigned to i and the same is output in the succeeding printf() statement.
Q. No. 93 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1;
clrscr();
i=i+2*i++;
printf("%d",i);
getch();
}
Output :
111
Explanation :
The expression
i=i+2*i++;
is evaluated as follows :
Step 1 : The expression i + 2*i is evaluated (=3)
Step 2 : The value of 3 is assigned to i.
Step 3 : The value of i is incremented by 1. (=4)
Hence the output of 4 is generated.
Q. No. 94 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int x=20,y=35;
clrscr();
x=y+++x++;
y=++y+ ++x;
printf("%d %d",x,y);
getch();
}
Output :
Explanation :
The expression x=y+++x++; is evaluated as follows:
Step 1 : The expression is evaluated as follows:
112
x=(y++)+(x++);
x is assigned the value of (x + y) (=55)
Step 2 : x and y values are incremented to 56 and 36, respectively.
Step 3 : The expression y=++y+ ++x; is evaluated as follows:
y=(++y)+ (++x);
Step 4 : x and y variables are pre-incremented and assigned the values of 57 and 37,
respectively.
Step 5 : y is assigned the value of (x+y) (=94).
Step 6 : x and y get the values of 57 and 94, respectively.
Hence the output of 57 and 94 is generated.
Q. No. 95 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int a=0xff,b;
b=a<<4>>12;
clrscr();
b?printf("Sandeep"):printf("Ashok");
getch();
}
Output :
Explanation :
The expression b=a<<4>>12;is evaluated as follows:
113
Step 1 : The expression b=a<<4>>12; is evaluated as b=(a<<4)>>12;
Step 2 : Initial value of a is 0000ff, hence a << 4 = 000ff0 (left shift by 4-bits or one
hexadecimal digit)
Step 3 : The expression 000ff0 >> 12 bits evaluates to 0 (right shift by 12 bits or 3 hexadecimal
digits)
Step 4 : The value of b is 0 (false). Hence the expression
b?printf("Sandeep"):printf("Ashok");
prints “Ashok”. Hence the output generated.
Q. No. 96 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned int x=-10;
int y=10;
clrscr();
if (y <= x) printf("He is goodn");
if (y == (x=-10)) printf("He is bettern");
if ((int)x==y) printf("He is bestn");
getch();
}
Output :
Explanation :
Sign bit is 1 for –ve number and 0 for +ve number. Hence -10 is represented as
1000 0000 0000 1010
114
Q. No. 97 Category : Control Statements
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int x=0;
clrscr();
for(; ;)
{
if (x++ == 4) break;
continue;
}
printf("x=%dn",x);
getch();
}
Output :
Explanation :
In the for loop, the value of x is continuously incremented till it becomes 4. Once the condition x
== 4 is satisfied, the control breaks out of the for loop but before that the value of x is
incremented to 5. Hence the output x=5 is generated.
Q. No. 98 Category : Control Statements
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int score=4;
switch(score)
{
115
default :
;
case 3 :
score+=5;
if (score==8)
{
score++;
if (score==9) break;
score *= 2;
}
score -= 4;
break;
case 8 :
score += 5;
break;
}
printf("SCORE = %dn",score);
getch();
}
Output :
Explanation :
The steps in the execution of the application are given below:
Step 1 : Since case 4 is not present, the control enters the default case in switch statement.
Step 2 : Since default case does not contain a break statement, the next case, case 3 is entered.
Step 3 : score variable is incremented by 5 and its value becomes 9.
Step 4 : The if condition evaluates to false and value of score variable is decremented by 4 and
its value becomes 5.
Step 5 : The control breaks out of the switch statement and the value of score (equal to 5) is
printed.
Hence the output SCORE=5 is generated.
116
Q. No. 99 Category : Control Statements
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
i=2;
i++;
clrscr();
if (i=4)
{
printf("i=4");
}
else
{
printf("i=3");
}
getch();
}
Output :
Explanation :
= is an assignment operator while == is a comparison operator. In the if condition,
if (i=4)
4 is assigned to i and the condition evaluates to true and i=4 is displayed on console.
Q. No. 100 Category : Recursion
Predict the output or error(s) for the following:
117
#include <stdio.h>
#include <conio.h>
void foo(int nVal)
{
int i;
if (nVal)
{
nVal--;
foo(nVal);
for(int i=0;i<nVal;i++)
printf("*");
printf("n");
}
}
int main()
{
foo(4);
getch();
return 0;
}
Output :
Explanation :
The first statement in main() invokes a call foo(4). In the function foo(), the statements are
executed in the following order.
Step 1 : if condition returns true.
Step 2 : Value of nVal is decremented by 1, and the function is recursively invoked with
argument 3, as foo(3) on pushing the context onto the stack.
Step 3 : Recursively invoking the function foo() continues till the terminating condition
if (nval) returns false (till nval becomes 0).
118
Step 4 : Output is generated by popping the context from the stack.
The output generated in various method calls is depicted in the following table:
nVal foo(nVal) Statements printed
1 foo(1)
2 foo(2) *
3 foo(3) **
4 foo(4) ***
Q. No. 101 Category : Abstract Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
union A
{
char a;
char b;
};
int main()
{
union A a;
a.a=65;
clrscr();
printf("%c %c",a.a,a.b);
getch();
return 0;
}
Output :
Explanation :
In union, different members share the same memory location. 65, which is the ASCII code
corresponding to the alphabet „A‟ is stored in the member a of union variable a, which is also
119
shared by the variable b. Hence both a.a and a.b contain the value 65 (ASCII code of A). Hence
the output A A is generated by the preceding printf statement.
Q. No. 102 Category : Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
int fun(int i)
{
return(i++);
}
void main()
{
int i=fun(10);
clrscr();
printf("%dn",i);
getch();
}
Output :
Explanation :
return(i++) statement will first return the value of i and then increments i. i.e. 10 will be returned.
Q. No. 103 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
char *p;
int *q;
120
long *r;
p=q=r=0;
p++;
q++;
r++;
clrscr();
printf("%p...%p...%p",p,q,r);
getch();
}
Output :
Explanation :
++ operator, when applied to pointers increments address according to their corresponding data-
types. Thus, character pointer is incremented by 1, integer pointer is incremented by 2 and long
pointer is incremented by 4.
Q. No. 104 Category : Arrays and Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
int one_d[]={1,2,3};
void main()
{
int *ptr;
ptr=one_d;
ptr+=3;
clrscr();
printf("%d",*ptr);
getch();
}
121
Output :
Explanation :
ptr points to the first element 1 of one_d array as shown below:
++ operator, when applied to pointers increments address according to their corresponding data-
types. Thus, integer pointer is incremented by 3.
After ptr+=3,
ptr pointer is pointing to out of the array range of one_d.
Change the statement ptr+=3 to ptr+=2 (so that ptr points to the last element of the array) and re-
execute the application. The following output is generated.
Q. No. 105 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i =0;
122
j=0;
if(i && j++)
printf("%d..%d",i++,j);
printf("%d..%d,i,j);
}
Output :
Explanation :
The value of i is 0. Since this information is enough to determine the truth value of the boolean
expression. So the statement following the if statement is not executed. The values of i and j
remain unchanged and get printed.
Q. No. 106 Category : Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
i = abc();
printf("%d",i);
}
abc()
{
_AX = 1000;
}
123
Output :
Explanation :
Normally the return value from the function is through the information from the accumulator.
Here _AH is the pseudo global variable denoting the accumulator. Hence, the value of the
accumulator is set 1000 so the function returns value 1000.
Q. No. 107 Category : Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
int i;
void main()
{
int t;
clrscr();
for ( t=4;scanf("%d",&i)-t;printf("%dn",i))
printf("%d--",t--);
getch();
}
// If the inputs are 0,1,2,3 find the o/p
124
Output :
Explanation :
In the given program, scanf() statement is present in the test condition and the output generated
is the concatenation of body of the for loop and the increment condition of the for loop.
The following table depicts the statements executed during the different iterations of fop loop.
Iteration Input t Test Condition
scanf("%d",&i)-t
o/p
printf("%d--",t--);
Increment condition
printf("%dn",i)
+ o/p
1 0 4 -4 (true) 4-- 4--0
2 1 3 -2(true) 3-- 3--1
3 2 2 0 (false) 2-- 2--2
Hence the given program generates the output.
4-0
3-1
2-2
In the third iteration the condition becomes false and the for loop terminates.
Q. No. 108 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
125
void main()
{
int a= 0;
int b = 20;
char x =1;
char y =10;
clrscr();
if(a,b,x,y)
printf("hello");
getch();
}
Output :
Explanation :
The comma operator has associativity from left to right. Only the rightmost value is returned and
the other values are evaluated and ignored. Thus the value of last variable y is returned to check
in if. Since it is a non zero value if becomes true so, "hello" will be printed.
Change the statement
char y =10;
to
char y =0;
re-execute the application. The if condition evaluates to false and no output is generated as
shown below:
Q. No. 109 Category : Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
126
#include <conio.h>
void main()
{
unsigned int i;
clrscr();
for(i=1;i>-2;i--)
printf("c aptitude");
getch();
}
Output :
No Output.
Explanation :
i is an unsigned integer. It is compared with a signed value. Since the both types doesn't match,
signed is promoted to unsigned value. The unsigned equivalent of -2 is a huge value so condition
becomes false and control comes out of the loop.
Q. No. 110 Category : Storage Classes
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
static int i=5;
if(--i)
{
main();
printf("%d ",i);
}
}
127
Output :
Explanation :
The variable "i" is declared as static, hence memory for i will be allocated for only once, as it
encounters the statement. The function main() will be called recursively unless i becomes equal
to 0, and since main() is recursively called, so the value of static i i.e., 0 will be printed every
time the control is returned.
In the above program, if the static keyword is removed the program will enter into an infinite
loop since each time main() function is invoked the value of I is initialized to 5.
Q. No. 111 Category : Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned giveit=-1;
int gotit;
clrscr();
printf("%u ",++giveit);
printf("%u n",gotit=--giveit);
getch();
}
Output :
128
Explanation :
The range of unsigned int is 0 to 65535.
Hence decrementing the unsigned int variable initialized to 0 (minimum value) will result in
rollback to maximum value of 65535.
Similarly, incrementing the unsigned int variable initialized to 65535 (maximum value) will
result in rollback to minimum value of 0 as illustrated in the following program:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned x=0;
clrscr();
printf("%un",--x);
getch();
}
On execution of the above program, the following output is generated.
Change the above program to the one shown below and re-execute it.
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned x=65535;
clrscr();
printf("%un",++x);
getch();
}
On execution of the above program, the following output is generated.
129
Q. No. 112 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
void *v;
int integer=2;
int *i=&integer;
v=i; Error
printf("%d",(int*)*v);
}
Output :
Explanation :
void pointer is a generic pointer type. No pointer arithmetic can be done on it. void pointers are
normally used for,
1. Passing generic pointers to functions and returning such pointers.
2. As an intermediate pointer type.
3. Used when the exact pointer type will be known at a later point of time.
130
Q. No. 113 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=i++, j=j++, k=k++;
printf(“%d%d%d”,i,j,k);
}
Output :
Explanation :
An identifier is available to use in program code from the point of its declaration.
So expressions such as i = i++ are valid statements. The i, j and k are automatic variables and so
they contain some garbage value. Garbage in is garbage out (GIGO).
Q. No. 114 Category : Storage Classes
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
static int i=++i, j=++j, k=++k;
clrscr();
printf(“%d %d %d”,i,j,k);
getch();
}
131
Output :
Explanation :
Since static variables are initialized to zero by default.
Remove the static qualifier and re-execute the program, the following output is generated.
int i=++i, j=++j, k=++k;
As seen from the output, the variables i, j and k are initialized to garbage values.
Q. No. 115 Category : Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
while(1)
{
if(printf("%d",printf("%d")))
break;
else
continue;
}
getch();
}
132
Output :
Explanation :
The inner printf executes first to print some garbage value. The printf returns no of characters
printed and this value also cannot be predicted. Still the outer printf prints something and so
returns a non-zero value. So it encounters the break statement and comes out of the while
statement.
Q. No. 116 Category : Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned int i=10;
clrscr();
while(i-->=0)
printf("%u ",i);
getch();
}
Output :
133
Explanation :
Since i is an unsigned integer it can never become negative. So the expression i-- >=0 will
always be true, resulting in an infinite loop.
Q. No. 117 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int x,y=2,z,a;
if(x=y%2) z=2;
a=2;
clrscr();
printf("%d %d ",z,x);
getch();
}
output :
Explanation :
The value of y%2 is 0. This value is assigned to x. The condition reduces to if (x) or in other
words if(0) and so z goes uninitialized.
Thumb Rule: Check all control paths to write bug free code.
Q. No. 118 Category : Arrays and Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
134
void main()
{
int a[10];
clrscr();
printf("%d",*a+1-*a+3);
getch();
}
Output :
Explanation :
*a and -*a cancels out. The result is as simple as 1 + 3 = 4 !
Q. No. 119 Category : Macros
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
#define prod(a,b) a*b
void main()
{
int x=3,y=4;
clrscr();
printf("%d",prod(x+2,y-1));
getch();
}
Output :
135
Explanation :
The macro expands and evaluates to as:
x+2*y-1
= x+(2*y)-1
= 10
Q. No. 120 Category : Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned int i=65000;
while(i++!=0);
printf("%d",i);
}
Output :
Explanation :
Note the semicolon after the while statement. When the value of i becomes 0 it comes out of
while loop. Due to post-increment on i the value of i while printing is 1.
Q. No. 121 Category : Operators
Predict the output or error(s) for the following:
#include <stdlib.h>
#include <stdio.h>
136
void main()
{
int i=1;
+i;
clrscr();
while (i--!=0);
printf("%d",i);
getch();
}
Output :
Explanation :
Unary + is the only dummy operator in C. So it has no effect on the expression and now the
while loop is, while (--i!=0) which is false and so breaks out of while loop. The value –1 is
printed due to the post-decrement operator.
The condition while (i--!=0); is evaluated in the following steps:
Step 1 : The expression i != 0 is evaluated.
Step 2 : i is decremented.
The condition is first evaluated and then I is decremented.
Observe the semicolon next to while loop.
In the first iteration the condition (1 != 0) evaluates to true and in the second iteration the
condition evaluates to false (0 != 0). Hence the control comes out of the loop, before that the
value of i is decremented to -1 and the same is printed by the next printf() statement.
Hence the program generates the output -1.
Change the post-decrement operator to pre-decrement operator as shown in the following
program and re-execute the program.
#include <stdlib.h>
#include <stdio.h>
137
void main()
{
int i=1;
+i;
clrscr();
while (--i!=0);
printf("%d",i);
getch();
}
On execution of the above program, the following output is generated.
The condition while (--i!=0); is evaluated in the following steps:
Step 1 : i is decremented.
Step 2 : The expression i != 0 is evaluated.
i is decremented first and then the condition is evaluated.
In the first iteration of the while loop the condition (0!=0) becomes false. Hence the control
comes out of the for loop and prints 0.
Hence the program now generates the output 0.
Q. No. 122 Category : Abstract Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
float f=5,g=10;
enum{i=10,j=20,k=50};
138
clrscr();
printf("%dn",++k); Line 9
printf("%fn",f<<2); Line 10
printf("%lfn",f%g); Line 11
printf("%lfn",fmod(f,g));
getch();
}
Output :
Explanation :
Enumeration constants cannot be modified, so you cannot apply ++.
Bit-wise operators and % operators cannot be applied on float values.
fmod() is to find the modulus values for floats as % operator is for ints.
Q. No. 123 Category : Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void pascal f1(int i,int j,int k)
{
printf("%d %d %d",i, j, k);
}
void cdecl f2(int i,int j,int k)
{ printf("%d %d %d",i, j, k);
}
void main()
{
int i=10;
clrscr();
f1(i++,i++,i++);
139
printf(" %dn",i);
i=10;
f2(i++,i++,i++);
printf(" %d",i);
getch();
}
Output :
Explanation :
Pascal argument passing mechanism forces the arguments to be called from left to right. cdecl is
the normal C argument passing mechanism where the arguments are passed from right to left.
The function f1 uses pascal mechanism of parameter invocation. Hence the function call
f1(i++, i++,i++) is evaluated as follows:
printf() statement in function f1() becomes
printf(“%d %d %d”,10 ,11 ,12) which displays 10 11 12 on the console.
The function f2 uses cdecl mechanism of parameter invocation. Hence the function call
f2(i++, i++,i++) is evaluated as follows:
printf() statement in function f2() becomes
printf(“%d %d %d”,12 ,11 ,10) which displays 12 11 10 on the console.
Q. No. 124 Category : Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
signed char i=0;
140
clrscr();
for(;i>=0;i++) ;
printf("%dn",i);
getch();
}
Output :
Explanation :
Notice the semicolon at the end of the for loop. The initial value of the i is set to 0. The inner
loop executes to increment the value from 0 to 127 (the positive range of char) and then it rotates
to the negative value of -128. The condition in the for loop fails and so comes out of the for loop.
It prints the current value of i that is -128.
Q. No. 125 Category : Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
unsigned char i=0;
clrscr();
for(;i>=0;i++) ;
printf("%dn",i);
getch();
}
Output :
Infinite Loop
141
Explanation :
The difference between the previous question and this one is that the char is declared to be
unsigned. So the i++ can never yield negative value and i>=0 never becomes false so that it can
come out of the for loop.
Q. No. 126 Category : Variable Declaration Issues
Is the following statement a declaration/definition. Find what does it mean?
int (*x)[10];
Output :
x is a pointer to array of(size 10) integers.
Explanation :
Apply clock-wise rule to find the meaning of this definition.
There is a technique known as the ``Clockwise/Spiral Rule'' which enables any C programmer to
parse any C declaration.
For example consider the following declaration,
int *x[10]
Step 1 :x is a ….
Step 2 : We move in a spiral clockwise direction starting with `x' and the first character we see is
x[10]
Hence, x is an array of 10 ….
Step 3: Continue in a spiral clockwise direction, and the next thing we encounter is the `*' so,
we have pointers
Hence, x is an array 10 of pointers to...
Step 4 :Continue in a spiral direction and we see the end of the line i.e., `;', so keep going and
we get to the type `int',
Hence, ”x is an array 10 of pointers to int''
In the declaration,
142
int (*x)[10];
(*x) is a single entity,
Hence, x is a pointer ….
Move in the spiral clockwise direction, to an array of 10
Continue to move in the spiral clockwise direction, integers.
Hence, “x is a pointer to an array of 10 integers”.
Q. No. 127 Category : Abstract Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
typedef struct error{int warning, error, exception;}error;
void main()
{
error g1;
g1.error =1;
printf("%d",g1.error);
}
Output :
Explanation :
The three usages of name errors can be distinguishable by the compiler at any instance, so valid
(they are in different namespaces).
typedef struct error
{
int warning, error, exception;
143
}error;
This error can be used only by preceding the error by struct keyword as in:
struct error someError;
typedef struct error
{
int warning, error, exception;
}error;
This can be used only after . (dot) or -> (arrow) operator preceded by the variable name as in :
g1.error =1;
printf("%d",g1.error);
typedef struct error{int warning, error, exception;}error;
This can be used to define variables without using the preceding struct keyword as in:
error g1;
Since the compiler can perfectly distinguish between these three usages, it is perfectly legal and
valid.
Q. No. 128 Category : Preprocessors
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
#ifdef something
int some=0;
#endif
void main()
{
int thing = 0;
printf("%d %dn", some ,thing); Error
}
144
Output :
Explanation :
This is a very simple example for conditional compilation. The name „something‟ is not already
known to the compiler making the declaration
int some = 0;
effectively removed from the source code.
Q. No. 129 Category : Preprocessors
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
#if something == 0
int some=0;
#endif
void main()
{
int thing = 0;
clrscr();
printf("%d %dn", some ,thing);
getch();
}
Output :
145
Explanation :
This code is to show that preprocessor expressions are not the same as the ordinary expressions.
If a name is not known the preprocessor treats it to be equal to zero.
Q. No. 130 Category : Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
if(~0 == (unsigned int)-1)
printf(“You can answer this if you know how values are represented in memory”);
getch();
}
Output :
Explanation :
~ (tilde operator or bit-wise negation operator) operates on 0 to produce all ones to fill the space
for an integer. –1 is represented in unsigned value as all 1‟s and so both are equal.
Q. No. 131 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
char *p = “SIBER”;
printf(“%c”,++*(p++));
}
146
Output :
Explanation :
The expression ++*(p++)) is evaluated as follows:
Step 1 : p is declared as a character pointer pointing to the character S of a string “SIBER”.
Step 2 : The pointer is dereferenced and then incremented by 1, to a value T (next to S in
alphabetic sequence).
Step 3 : After the printf statement is executed, the pointer p is incremented by 1 and will point to
the next character of the string “SIBER”, I.
Add the following statement at the end of the program and re-execute it.
printf("%c",*p);
On execution of the program, the following output is generated.
Q. No. 132 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=5;
clrscr();
printf("%d",++i++); Error
getch();
}
147
Output :
Explanation :
++i yields an rvalue. For postfix ++ to operate an lvalue is required.
Q. No. 133 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
char *p = “ayqm”;
char c;
c = ++*p++;
clrscr();
printf(“%c”,c);
getch();
}
Output :
Explanation :
There is no difference between the expression ++*(p++) and ++*p++. Parenthesis just works as a
visual clue for the reader to see which expression is first evaluated.
148
The expression ++*(p++)) is evaluated as follows:
Step 1 : p is declared as a character pointer pointing to the character S of a string “ayqm”.
Step 2 : The pointer is dereferenced and then incremented by 1, to a value b (next to a in
alphabetic sequence).
Step 3 : After the printf statement is executed, the pointer p is incremented by 1 and will point to
the next character of the string “ayqm”, y.
Add the following statement at the end of the program and re-execute it.
printf("%c",*p);
On execution of the program, the following output is generated.
Q. No. 134 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=5;
clrscr();
printf(“%d”,i=++i==6);
getch();
}
Output :
149
Explanation :
The expression can be treated as i = (++i==6), because == is of higher precedence than =
operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result.
Q. No. 135 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
char p[ ]="%dn";
p[1] = 'c';
clrscr();
printf(p,65);
getch();
}
Output :
Explanation :
Due to the assignment p[1] = „c‟ the string becomes, “%cn”. Since this string becomes the
format string for printf and ASCII value of 65 is „A‟, the same gets printed.
Q. No. 136 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i = 3;
150
clrscr();
for (;i++=0;) Error
printf(“%d”,i);
getch();
}
Output :
Explanation :
As we know that increment operators return rvalues and hence it cannot appear on the left hand
side of an assignment operation.
Q. No. 137 Category : Control Statements
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
static int i;
clrscr()
while(i<=10)
(i>2)?i++:i--;
printf(“%d”, i);
}
Output :
151
Explanation :
Since i is static it is initialized to 0. Inside the while loop the conditional operator evaluates to
false, executing i--. This continues till the integer value rotates to positive value (32767). The
while condition becomes false and hence, comes out of the while loop, printing the i value.
Q. No. 138 Category : Variable Declaration Issues
1. const char *a;
2. char* const a;
3. char const *a;
Differentiate the above declarations.
Output :
Explanation :
1. 'const' applies to char * rather than 'a' (pointer to a constant char)
*a='F' : illegal
a="Hi" : legal
2. 'const' applies to 'a' rather than to the value of a (constant pointer to char )
*a='F' : legal
a="Hi" : illegal
3. Same as 1.
Q. No. 139 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=5,j=10;
i=i&=j&&10;
clrscr();
printf("%d %d",i,j);
getch();
}
152
Output :
Explanation :
The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1
because j==10. i is 5. i = 5&1 is 1. Hence the result.
Q. No. 140 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=4,j=7;
j = j || i++ && printf("YOU CAN");
clrscr();
printf("%d %d", i, j);
getch();
}
Output :
Explanation :
The boolean expression needs to be evaluated only till the truth value of the expression is not
known. j is not equal to zero itself means that the expression‟s truth value is 1. Because it is
followed by || and true || (condition2) => true where (condition2) will not be evaluated. So the
remaining expression is not evaluated and so the value of i remains the same.
Similarly when && operator is involved in an expression, when any of the operands become
153
false, the whole expression‟s truth value becomes false and hence the remaining expression will
not be evaluated.
false&& (condition2) => false where (condition2) will not be evaluated.
Hence 1 is assigned to j and i continues to remain at 4.
Hence the given program generates the output 4 1.
Q. No. 141 Category : Storage Classes
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
register int a=2;
clrscr();
printf("Address of a = %d",&a); Line 8
printf("Value of a = %d",a);
getch();
}
Output :
Explanation :
& (address of ) operator cannot be applied on register variables.
Q. No. 142 Category : Control Statements
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
154
void main()
{
float i=1.5;
switch(i) Line 7
{
case 1: printf("1");
case 2: printf("2");
default : printf("0");
}
}
Output :
Explanation :
Switch statements can be applied only to integral types.
Q. No. 143 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int a=2,*f1,*f2;
f1 = f2 = &a;
*f2 += *f2 += a += 2.5;
clrscr();
printf("n%d %d %d",a,*f1,*f2);
getch();
}
Output :
155
Explanation :
f1 and f2 both refer to the same memory location a. So changes through f1 and f2 ultimately
affects only the value of a.
The statement *f2 += *f2 += a += 2.5;is evaluated as follows:
Step 1 : f1 and f2 both refer to the same memory location a as shown below:
After f2+=8,
Hence a, f1 and f2 point to the same memory location containing the 16. Hence the output.
Q. No. 144 Category : Arrays and Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
156
void main()
{
char *p="GOOD";
char a[ ]="GOOD";
clrscr();
printf("n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p),strlen(p));
printf("n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a));
getch();
}
Output :
Explanation :
sizeof(p) => sizeof(char*) => 2
sizeof(*p) => sizeof(char) => 1
Similarly,
sizeof(a) => size of the character array => 5
When sizeof operator is applied to an array it returns the sizeof the array and it is not the same as
the sizeof the pointer variable. Here the sizeof(a) where a is the character array and the size of
the array is 5 because the space necessary for the terminating NULL character should also be
taken into account.
Q. No. 145 Category : Preprocessors
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
#define DIM( array, type) sizeof(array)/sizeof(type)
void main()
{
int arr[10];
printf(“The dimension of the array is %d”, DIM(arr, int));
}
157
Output :
Explanation :
The size of integer array of 10 elements is 10 * sizeof(int). The macro expands to
sizeof(arr)/sizeof(int) => 10 * sizeof(int) / sizeof(int) => 10.
Modify the above program as shown below and re-execute it.
#include <stdio.h>
#include <conio.h>
#define DIM( array, type) sizeof(array)/sizeof(type)
void main()
{
int arr[10];
float arr1[50];
clrscr();
printf("The dimension of the array arr is %d", DIM(arr, int));
printf("nThe dimension of the array arr1 is %d", DIM(arr1, float));
getch();
}
On re-execution of the program, the following output is generated.
Q. No. 146 Category : Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
158
int DIM(int array[])
{
return sizeof(array)/sizeof(int );
}
void main()
{
int arr[10];
clrscr();
printf(“The dimension of the array is %d”, DIM(arr));
getch();
}
Output :
Explanation :
Arrays cannot be passed to functions as arguments and only the pointers can be passed. So the
argument is equivalent to int * array (this is one of the very few places where [] and * usage are
equivalent). The return statement becomes, sizeof(int *)/ sizeof(int) that happens to be equal in
this case.
Q. No. 147 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i = 257;
int *iPtr = &i;
clrscr();
printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
getch();
}
159
Output :
Explanation :
The integer value 257 is stored in the memory as, 00000001 00000001, so the individual bytes
are taken by casting it to char * and get printed.
Q. No. 148 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=300;
char *ptr = &i;
*++ptr=2;
clrscr();
printf("%d",i);
getch();
}
Output :
Explanation :
The integer value 300 in binary notation is: 00000001 00101100. It is stored in memory (small-
endian) as: 00101100 00000001. Result of the expression *++ptr = 2 makes the memory
representation as: 00101100 00000010. So the integer corresponding to it is 00000010
160
00101100 => 556.
Q. No. 149 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h
void main()
{
char * str = "hello";
char * ptr = str;
char least = 127;
while (*ptr++)
least = *ptr;
clrscr();
printf("%d",least);
getch();
}
Output :
Explanation :
After „ptr‟ reaches the end of the string the value pointed by „str‟ is „0‟. So the value of „str‟ is
less than that of „least‟. So the value of „least‟ finally is 0.
Q. No. 150 Category : Abstract Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
struct student
{
161
char name[30];
struct date dob; Line 9
}stud;
struct date
{
int day,month,year;
};
scanf("%s%d%d%d", stud.name, &stud.dob.day, &stud.dob.month, &stud.dob.year);
}
Output :
Explanation :
Inside the struct definition of „student‟ the member of type struct date is given. The compiler
doesn‟t have the definition of date structure (forward reference is not allowed in C in this case)
so it issues an error.
To remove the error, re-write the program as shown below and re-execute it. Place struct date
before struct student.
#include <stdio.h>
#include <conio.h>
void main()
{
struct date
{
int day,month,year;
};
struct student
{
char name[30];
struct date dob;
162
}stud;
scanf("%s%d%d%d", stud.name, &stud.dob.day, &stud.dob.month, &stud.dob.year);
}
Q. No. 151 Category : Functions
Is there any difference between the two declarations,
1. int foo(int *arr[])
2. int foo(int *arr[2])
Output :
No
Explanation :
Functions can only pass pointers and not arrays. The numbers that are allowed inside the [] is just
for more readability. So there is no difference between the two declarations.
Q. No. 152 Category : Functions
What is wrong with the following code?
int *foo()
{
int *s = malloc(sizeof(int)100);
assert(s != NULL);
return s;
}
Output :
Explanation :
assert macro should be used for debugging and finding out bugs. The check s != NULL is for
error/exception handling and for that assert shouldn‟t be used. A plain if and the corresponding
remedy statement has to be given.
163
Q. No. 153 Category : Macros
What is the hidden bug with the following statement?
assert(val++ != 0);
Output :
Explanation :
Assert macro is used for debugging and removed in release version. In assert, the expression
involves side-effects. So the behavior of the code becomes different in case of debug version and
the release version thus leading to a subtle bug.
Rule to Remember:
Don‟t use expressions that have side-effects in assert statements.
Q. No. 154 Category : Memory Allocation
Predict the output or error(s) for the following:
void main()
{
int *i = 0x400; // i points to the address 400
*i = 0; // set the value of memory location pointed by i;
}
Output :
Undefined Behaviour
Explanation :
The second statement results in undefined behavior because it points to some location whose
value may not be available for modification. This type of pointer in which the non-availability
of the implementation of the referenced location is known as 'incomplete type'.
Q. No. 155 Category : Abstract Data Types
Is the following code legal?
164
struct a
{
int x;
struct a b;
}
Output :
No
Explanation :
Is it not legal for a structure to contain a member that is of the same type as in this case. Because
this will cause the structure declaration to be recursive without end.
However, a structure can contain a pointer to a structure as one of its members, as shown below:
struct a
{
int x;
struct* a b;
}
Q. No. 156 Category : Abstract Data Types
Is the following code legal?
struct a
{
int x;
struct a *b;
}
Output :
Yes
Explanation :
*b is a pointer to type struct a and so is legal. The compiler knows, the size of the pointer to a
structure even before the size of the structure is determined(as you know the pointer to any type
is of same size). This type of structures is known as „self-referencing‟ structure.
165
Q. No. 157 Category : Abstract Data Types
Is the following code legal?
typedef struct a
{
int x;
aType *b;
}aType
Output :
No
Explanation :
The typename aType is not known at the point of declaring the structure (forward references are
not made for typedefs).
Re-write the above code as shown below:
struct a
{
int x;
aType *b;
};
The typename aType is known at the point of declaring the structure, because it is already type
defined.
Q. No. 158 Category : Abstract Data Types
Is the following code legal?
void main()
{
typedef struct a aType;
aType someVariable;
struct a
{
int x;
aType *b;
};
}
166
Output :
No
Explanation :
When the declaration,
typedef struct a aType;
is encountered body of struct a is not known. This is known as „incomplete types‟.
Q. No. 159 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf(“sizeof (void *) = %d n“, sizeof( void *));
printf(“sizeof (int *) = %d n”, sizeof(int *));
printf(“sizeof (double *) = %d n”,sizeof (double *));
printf(“sizeof(struct unknown *) = %d n”, sizeof(struct unknown *));
getch();
}
Output :
Explanation :
The pointer to any type is of same size, equal to 2 bytes.
167
Q. No. 160 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=10, j=2;
int *ip= &i, *jp = &j;
int k = *ip/*jp; Error
clrscr();
printf(“%d”,k);
getch();
}
Output :
Explanation :
The programmer intended to divide two integers, but by the “maximum munch” rule, the
compiler treats the operator sequence / and * as /* which happens to be the starting of comment.
To force what is intended by the programmer,
int k = *ip/ *jp;
there are two methods available.
Method 1 :
// give space explicitly separating / and *
Method 2 :
int k = *ip/(*jp);
168
// put braces to force the intention
will solve the problem.
Incorporate the above change and re-execute the program, the following output is generated.
Q. No. 161 Category : Control Statements
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
char ch;
clrscr();
for(ch=0;ch<=127;ch++)
printf(“%c %d n“, ch, ch);
getch();
}
Output :
169
Explanation :
The char type may be signed or unsigned by default. If it is signed then ch++ is executed after ch
reaches 127 and rotates back to -128. Thus ch is always smaller than 127.
Q. No. 162 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main(){
int *ptr;
ptr = (int *) 0x400;
clrscr();
printf("%d",*ptr);
getch();
}
Output :
Explanation :
The pointer ptr will point at the integer in the memory location 0x400.
Q. No. 164 Category : Arrays
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[4]="HELLO"; Line 6
clrscr();
printf("%s",a);
getch();
}
170
Output :
Explanation :
The array a is of size 4 but the string constant “HELLO”, requires 6 bytes to get stored.
Q. No. 165 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int a=10,*j;
void *k;
j=k=&a;
j++;
k++; Line 10
printf("n %u %u ",j,k);
}
Output :
Explanation :
void pointers are generic pointers and they can be used only when the type is not known and as
171
an intermediate address storage type. No pointer arithmetic can be done on it and you cannot
apply indirection operator (*) on void pointers.
The pointer k can be incremented as follows:
Cast void pointer to integer pointer as shown below:
(int *)k
Increment the pointer now,
((int *)k)++
Predict the output
#include <stdio.h>
#include <conio.h>
void main()
{
int a=10,*j;
void *k;
j=k=&a;
(*j)++;
(*(int*)k)++;
clrscr();
printf("n %d %d",*j,*(int*)(k));
getch();
}
On execution of the above program, the following output is generated:
Q. No. 166 Category : Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
172
char *someFun()
{
char *temp = “string constant";
return temp;
}
void main()
{
clrscr();
puts(someFun());
getch();
}
Output :
Explanation :
The program suffers no problem and gives the output correctly because the character constants
are stored in code/data area and not allocated in stack, so this doesn‟t lead to dangling pointers.
Q. No. 167 Category : Functions
#include <stdio.h>
#include <conio.h>
char *someFun1()
{
char temp[ ] = “string";
return temp;
}
char *someFun2()
{
char temp[ ] = {„s‟, „t‟,‟r‟,‟i‟,‟n‟,‟g‟};
return temp;
}
173
void main()
{
clrscr();
puts(someFun1());
puts(someFun2());
getch();
}
Output :
Explanation :
The above program demonstrates the situation of dangling pointers. The memory for the
character array temp is allocated in function someFun1() which is deallocated when the function
returns. someFun1() returns the character pointer where the string “string” was previously
present. The same situation arises in someFun2().
The correct version of the program is shown below:
#include <stdio.h>
#include <conio.h>
char *someFun1()
{
char* temp= "string";
return temp;
}
char *someFun2()
{
char* temp = (char*)malloc(7*sizeof(char));
//char* temp[]= {'s', 't','r','i','n','g'};
temp[0]='s';
temp[1]='t';
temp[2]='r';
temp[3]='i';
temp[4]='n';
temp[5]='g';
temp[6]='0';
return temp;
}
174
void main()
{
clrscr();
puts(someFun1());
puts(someFun2());
free(someFun2());
getch();
}
On execution of the above program, the following output is generated.
Q. No. 168 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j;
j = 10;
i = j++ - j++;
clrscr();
printf("%d %d", i,j);
getch();
}
Output :
175
Explanation :
In the expression,
i = j++ - j++
the expression i = j – j is first evaluated and then the value of j is incremented twice. Hence i gets
the value 0 while j becomes 12, hence the output.
Q. No. 169 Category : Functions
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int j;
clrscr();
for(j=0;j<3;j++)
foo();
getch();
}
foo()
{
static int i = 10;
i+=10;
printf(%dn,i);
}
Output :
Explanation :
In the main() function, the function foo() is invoked 3 times in the for loop. In the function foo()
176
since the variable i is declared as static, it is initialized only once and retains the value between
function calls. Hence for every subsequent function call, the value of i is incremented by 10 and
its value is printed in the next statement, generating the output
20
30
40
Q. No. 170 Category : Abstract Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
union test
{
int a;
int b;
int c;
};
void main()
{
union test u;
u.a = 10;
u.b = 20;
printf(“%d %d n”,u.a,u.b);
}
Output :
Explanation :
Since test is declared as a union with three members a, b and c of type int, by definition of union,
a, b and c share the same memory location of size 2 bytes. Any change to a single member is
reflected in other members of the union. Latest assignment overwrites the values of other
177
members.
Hence the statement
u.b = 20;
assigns the value of 20 to all the members of the union.
Modify the printf() statement as shown below and re-execute the program.
printf(%d %d %dn,u.a,u.b,u.c);
On re-execution of the program, the following output is generated.
Q. No. 171 Category : Operators
Predict the output or error(s) for the following:
#include <stdlib.h>
#include <stdio.h>
void f()
{
static int a[3] = {1, 2, 3};
a[1]++;
printf("%dn", a[1]);
}
main()
{
int i;
clrscr();
for (i = 0; i < 5; i++)
{
f();
}
getch();
}
178
Output :
Explanation :
The function f() is invoked 5 times from the main() method. Since a is declared as a static array
in f() its elements are initialized only once and the elements of a retain their values between
function calls.. In the function f(), the second element of the array is incremented and the same
is printed in the next statement. Hence the program generates the output
3
4
5
6
7
Q. No. 172 Category : Control Statements
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=0;
clrscr();
if(i=0)
printf("statement 1");
else
printf("statement 2");
getch();
}
179
Output :
Explanation :
= is an assignment operator and not a comparison operator. Hence in the expression,
if(i=0)
0 is assigned to i and the condition becomes if (0), hence the condition evaluates to false and the
statement in the else part is evaluated which displays „statement 2‟, hence the output.
Q. No. 173 Category : Operators
What do you mean by associativity and precedence of C operators?
Answer:
Operator precedence determines which operator is performed first in an expression with multiple
operators. Associativity is used when two operators of same precedence appear in an expression.
Associativity can be either Left to Right or Right to Left. Hence, associativity is used when there
are two or more operators of same precedence.
Explanation :
180
Associativity of Function Invocations
Consider the following program :
#include <stdio.h>
#include <conio.h>
int x = 1;
int f1()
{
x += 5;
return x;
}
int f2()
{
x *= 3;
return x;
}
int main()
{
int p = f1() + f2();
clrscr();
printf("%d ", x);
getch();
return 0;
}
The value of p in the following expression
int p = f1() + f2();
depends on the order in which the functions are invoked i.e. associativity of function invocations.
If f1() is invoked first, followed by f2(), then the output generated is 18, on the other hand if it is
reversed, i.e. f2() is invoked first followed by f1(), the output generated is 8. To test this, execute
the above program.
On execution of the above program, the following output is generated.
181
Hence the associativity of function invocations is from left to right.
Q. No. 174 Category : Operators
What is the output generated on execution of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
int a=(1 & 3) + (3 & 4);
clrscr();
printf("%d",a);
getch();
}
Output : 1
Explanation :
The expression
a=(1 & 3) + (3 & 4);
is evaluated as follows:
Step 1 : 1 & 3 is evaluated as
00000000 00000001 & 00000000 00000011 = 00000000 00000001
182
Step 2 : 3 & 4 is evaluated as
00000000 00000011 & 00000000 00000100 = 00000000 00000000
Step 3 : The results obtained in Step 1 and Step 2 are added and the result is assigned to a.
Hence a gets the value 1, hence the output.
if int a=1&3 + 3&4 then, o/p is 0
Q. No. 175 Category : Functions
What is the output generated on execution of the following program?
#include <stdio.h>
#include <conio.h>
void f(int n)
{
if (n!=0)
{
printf("%d",n);
f(n-1);
printf("%d",n);
}
}
void main()
{
clrscr();
f(5);
getch();
}
Output :
5432112345
183
Explanation :
The main() method invokes the function f with argument 5 passed to it. In the function f(), if
condition evaluates to true. Hence 5 is printed and context is pushed on to the stack. Hence
successively, 5, 4, 3, 2 and 1 are printed and the if condition evaluates to false. Finally, stack is
popped up in the reverse order to generate the output
5432112345
f(5) = 5f(4)5
f(4) = 4f(3)4
f(3) = 3f(2)3
f(2) = 2f(1)2
f(1) = 11
Substituting the values in the reverse order, we get
f(2)=2112
f(3)=321123
f(4)=43211234
f(5)=5432112345
Hence the output.
Q. No. 176 Category : Abstract Data Types
What is the output generated on execution of the following program?
#include<stdio.h>
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
184
struct emp e = {"Tiger"};
printf("%d, %fn", e.age, e.sal);
return 0;
}
Output :
0, 0.000000
Explanation :
When an automatic structure is partially initialized remaining elements are initialized to 0(zero).
Q. No. 177 Category : Operators
What is the output generated on execution of the following program?
#include<stdio.h>
int main()
{
int x = 10, y = 20;
if(!(!x) && x)
printf("x = %dn", x);
else
printf("y = %dn", y);
return 0;
}
Output :
x=10
Explanation :
The logical not operator takes expression and evaluates to true if the expression is false and
evaluates to false if the expression is true. In other words it reverses the value of the expression.
Step 1: if(!(!x) && x)
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 3: if(1 && 10)
185
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10.
Q. No. 178 Category : Arrays and Functions
In C, if you pass an array as an argument to a function, what actually gets passed?
 A. Value of elements in array
 B. First element of the array
 C. Base address of the array
 D. Address of the last element of array
Output :
Option C
Explanation :
The statement 'C' is correct. When we pass an array as a function argument, the base address of
the array will be passed.
Q. No. 179 Category : Arrays and Operators
What is the output generated on execution of the following program?
#include<stdio.h>
#include <conio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
clrscr();
printf("%d, %d, %d", i, j, m);
getch();
return 0;
}
186
Output :
Explanation :
Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a
size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means
2++ so i=3)
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15
Q. No. 180 Category : Functions
What is the output generated on execution of the following program?
#include<stdio.h>
#include <conio.h>
int main()
{
void fun(int, int[]);
int arr[] = {1, 2, 3, 4};
int i;
fun(4, arr);
clrscr();
for(i=0; i<4; i++)
printf("%d,", arr[i]);
getch();
187
return 0;
}
void fun(int n, int arr[])
{
int *p=0;
int i=0;
while(i++ < n)
p = &arr[i];
*p=0;
}
Output :
Explanation :
Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts
one integer value and one array as an arguments and does not return anything.
Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is
initialized to
a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4
Step 3: int i; The variable i is declared as an integer type.
Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this
function.
Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs until the variable i is
less than '4' and it prints the each value of array a.
Hence the output of the program is 1,2,3,4
Q. No. 181 Category : Arrays and Functions
What is the output generated on execution of the following program?
188
#include<stdio.h>
#include <conio.h>
void fun(int **p);
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
int *ptr;
ptr = &a[0][0];
clrscr();
fun(&ptr);
getch();
return 0;
}
void fun(int **p)
{
printf("%dn", **p);
}
Output :
Explanation :
Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0}; The variable a is declared as an
multidimensional integer array with size of 3 rows 4 columns.
Step 2: int *ptr; The *ptr is a integer pointer variable.
Step 3: ptr = &a[0][0]; Here we are assigning the base address of the array a to the pointer
variable*ptr.
Step 4: fun(&ptr); Now, the &ptr contains the base address of array a.
Step 5: Inside the function fun(&ptr); The printf("%dn", **p); prints the value '1'.
because the *p contains the base address or the first element memory address of the
array a (i.e. a[0])
**p contains the value of *p memory location (i.e. a[0]=1).
Hence the output of the program is '1'
189
Q. No. 182 Category : Pointers and Arrays
What is the output generated on execution of the following program?
#include<stdio.h>
#include <conio.h>
int main()
{
static int arr[] = {0, 10, 20, 30, 40};
int *p[] = {arr+4, arr+3, arr+2, arr+1, arr};
int **ptr=p;
clrscr();
ptr++;
printf("%dn", ptr-p);
*ptr++;
printf("%dn", ptr-p);
*++ptr;
printf("%dn", ptr-p);
getch();
return 0;
}
Output :
Explanation :
In the above program, arr is an integer array and p is an integer pointer pointing to address of the
first element of an array in the reverse order as shown below:
0 10 20 30 40
1000 1002 1004 1006 1008
190
1008 1006 1004 1002 1000
2000
p ptr
Hence both p and ptr are pointer of pointers.
After the execution of the statements ptr++, we have
1008 1006 1004 1002 1000
2000
p ptr
Hence ptr-p =1 (no. of objects between ptr and p)
After the execution of the statement *ptr++
1008 1006 1004 1002 1000
2000
p ptr
Hence ptr-p = 2 (no. of objects between ptr and p)
Similarly, after the execution of the statement *++ptr;
ptr-p = 3.
Hence the given program generates the output
1
2
3
Q. No. 183 Category : Arrays
What will be the output of the program if the array begins at 65472 and each integer occupies
2 bytes?
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
191
clrscr();
printf("%u, %un", a+1, &a+1);
getch();
return 0;
}
Output :
65480, 65496
Modify the printf() statement as shown below and execute the program.
printf("%u %u, %un", a, a+1, &a+1);
The following output is generated.
Explanation :
Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an
integer array having the 3 rows and 4 columns dimensions.
Step 2: printf("%u, %un", a+1, &a+1);
The base address(also the address of the first element) of array is 65472.
For a two-dimensional array like a reference to array has type "pointer to array of 4 ints".
Therefore, a+1 is pointing to the memory location of first element of the second row in array a.
Hence 65472 + (4 ints * 2 bytes) = 65480
Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1
denotes "12 ints * 2 bytes * 1 = 24 bytes".
192
Hence, beginning address 65472 + 24 = 65496. So, &a+1 = 65496
Hence the output of the program is 65480, 65496
Q. No. 184 Category : Arrays
What will be the output of the program if the array begins 1200 in memory?
#include<stdio.h>
#include <conio.h>
int main()
{
int arr[]={2, 3, 4, 1, 6};
clrscr();
printf("%u, %u, %un", arr, &arr[0], &arr);
getch();
return 0;
}
Output :
1200, 1200, 1200
Explanation :
Step 1: int arr[]={2, 3, 4, 1, 6}; The variable arr is declared as an integer array and
initialized.
Step 2: printf("%u, %u, %un", arr, &arr[0], &arr);
Here, The base address of the array is 1200.
=>arr, &arr is pointing to the base address of the array arr.
=>&arr[0] is pointing to the address of the first element array arr. (ie. base address)
Hence the output of the program is 1200, 1200, 1200
On execution of the program, the following output is generated.
193
Q. No. 185 Category : Operators
What is the output generated on execution of the following program?
#include<stdio.h>
#include <conio.h>
int main()
{
int i=3, *j;
j = &i;
clrscr();
printf("%dn", i**j*i+*j);
getch();
return 0;
}
Output :
Explanation :
The expression
i**j*i+*j
is evaluated as i*(*j)*i+(*j)
i and *j refer to the same entity. Hence the above expression is i * i * i + i = 30.
Hence the program generates the output 30.
194
Q. No. 186 Category : Operators
What is the output generated on execution of the following program?
#include <stdio.h>
#include <conio.h>
int main()
{
int x=30, *y, *z;
y=&x;
/* Assume address of x is 500 and
integer is 4 byte size */
z=y;
clrscr();
printf("x=%d, y=%d, z=%dn", x, y, z);
*y++=*z++;
x++;
printf("x=%d, y=%d, z=%dn", x, y, z);
getch();
return 0;
}
Output :
x=31, y=504, z=504
On execution on the real system, the following output is generated.
Explanation :
The expression
*y++=*z++;
is equivalent to
(*y)++=(*z)++;
195
Post increment has higher priority than dereference operator.
The above expression is evaluated in the following steps:
Step 1 : Pointer z is dereferenced
Step 2 : Pointer z is incremented.
Step 3 : Value in the memory location pointed by z is assigned to variable pointed by y.
Step 4 :Pointer y is incremented.
To clearly understand the working of the statement
*y++=*z++;
Consider the following program:
#include <stdio.h>
#include <conio.h>
void main()
{
char str[] = "0123456789";
char *x = str;
char *y = "abcdefghij";
printf("x : %s y : %sn",x,y);
*x++ = *y++;
printf("x : %s y : %s",x,y);
x--;
printf("x : %s",x);
getch();
clrscr();
}
On execution of the above program, the following output is generated.
196
x and y are character pointers pointing to first character of strings "0123456789" and
“abcdefghij”, respectively, as shown below:
0 1 2 3 4 5 6 7 8 9
x
a b c d e f g h i j
y
The statement
*x++ = *y++;
is executed as follows:
i. y is dereferenced, so the right hand side of the expression becomes a.
ii. y is incremented so that it now points to b.
a b c d e f g h i j
y
iii. a is assigned to memory location pointed by x as shown below:
a 1 2 3 4 5 6 7 8 9
x
iv. x is incremented.
a 1 2 3 4 5 6 7 8 9
x
Hence the statement
197
printf("x : %s y : %s",x,y);
generates the output.
x: 123456789 y : bcdefghij
Next x is decremented as shown below:
a 1 2 3 4 5 6 7 8 9
x
Next, the value of x is printed as a123456789.
Q. No. 187 Category : Pointers
What is the output generated on execution of the following program?
#include<stdio.h>
#include <conio.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='M';
clrscr();
printf("%sn", str);
getch();
return 0;
}
Output :
Explanation :
str points to the first character „H‟ of a string “Hello” as shown below:
198
H e l l o 0
str
p is declared as a constant pointer initialized to str. Hence, p also points to the first character „H‟
of a string “Hello” as shown below:
H e l L o 0
p str
Since p is a constant pointer, it cannot be modified. However, it can modify the content of the
memory location it is pointing to.
The statement *p='M'; changes „H‟ to „M‟ as shown below:
M e l L o 0
p str
Hence the program generates the output “Mello”.
Q. No. 188 Category : Pointers
What will be the output of the program if the integer is 4 bytes long?
#include<stdio.h>
#include<conio.h>
int main()
{
int ***r, **q, *p, i=8;
p = &i;
q = &p;
r = &q;
clrscr();
printf("%d, %d, %dn", *p, **q, ***r);
getch();
return 0;
}
199
Output :
Explanation :
The snapshot of the memory storing variables is as shown below:
i p q r
1000 2000 3000
The expression ***r is evaluated as follows:
***r = ***&q
= **q (since *& =1 )
= **&p
= *p
= *&i
= i
Hence, ***r = i. On the same arguments,
**q = *p = i
Hence, *p, **q and ***r all refer to the same variable i.
Hence the program generated the output 8, 8, 8.
Q. No. 189 Category : Functions
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int *check(static int, static int);
int main()
{
int *c; c = check(10, 20);
8 2000
1000
000
3000
200
clrscr();
printf("%dn", c);
getch();
return 0;
}
int *check(static int i, static int j) Line 14
{
int *p, *q;
p = &i;
q = &j;
if (i >= 45)
return (p);
else
return (q);
}
Output :
Compiler Error
Explanation :
The static keyword means that a variable may have only and exactly one instance in its scope,
and that instance is invisible out of its scope. Neither of these requirements make sense for a
function argument
Q. No. 190 Category : Operators
What will be the output of the program ?
#include<stdio.h>
#include <conio.h>
int main()
{
clrscr();
201
printf("%d, %dn", sizeof(NULL), sizeof(""));
getch();
return 0;
}
Output :
Explanation :
The literal "" is an array that consists of one char with the value 0. The type is char[1], and
sizeof(char) is always one; therefore sizeof(char[1]) is always one.
NULL in C id declared as
((void*)0)
which is a void pointer, hence its size is 2 bytes.
Q. No. 191 Category : Pointers and Arrays
What will be the output of the program?
#include<stdio.h>
#include <conio.h>
int main()
{
int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
int *p, *q;
p = &arr[1][1][1];
q = (int*) arr;
clrscr();
printf("%d, %dn", *p, *q);
getch();
return 0;
}
202
Output :
Explanation :
In the above program, arr is a 3D array of dimension 2 x 2 x 2. The elements are stored as
follows:
i j k a[i][j][k]
0 0 0 10
0 0 1 2
0 1 0 3
0 1 1 4
1 0 0 5
1 0 1 6
1 1 0 7
1 1 1 8
Hence a[1][1][1] = 8
In the expression,
p = &arr[1][1][1];
the address of the element a[1][1][1] is assigned to p. Hence, *p is 8. q is initialized with arr, the
address of the first element of the array. Hence *q is equal to 10. Hence the program generates
the output 8, 10.
Q. No. 192 Category : Arrays
What will be the output of the program assuming that the array begins at the location 1002
and size of an integer is 4 bytes?
203
#include<stdio.h>
#include <conio.h>
int main()
{
int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
clrscr();
printf("%u, %u, %un", a[0]+1, *(a[0]+1), *(*(a+0)+1));
getch();
return 0;
}
Output :
1006, 2, 2
Explanation :
The elements of the array are stored in th contiguous memory locations in row major order as
shown below:
1 2 3 4 5 6 7 8 9 10 11 12
a[0] a[1] a[2]
1000 1008 1016
If the base address of the first array is 1002, then
a[0] = 1002. Since the size of an integer is 4 bytes,
a[0]+1 = 1006
a[0] + 1 points to the memory location, next to a[0] and *( a[0] + 1) dereferences it to 2.
Hence *( a[0] + 1) = 2
The expression, *(*(a+0)+1)is same as *(a[0]+1), and hence is equal to 2.
204
In general, *(*(a+i)+j)refers to ijth
element of a 2D array.
Alternatively,
The addresses stored in a[0], a[1] and a[2] are as shown below:
a[0] a[1] a[2]
1000 1008 1016
2000
A stores the address of a[0] as shown below:
a
2000
3000
Hence , *(*(a+0)+1) = *(1000 + 1) = *(1002)=2
Q. No. 193 Category : Pointers
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = arr;
p = (char*)((int*)(p));
clrscr();
printf("%d, ", *p);
p = (int*)(p+1);
printf("%d", *p);
getch();
return 0;
}
205
Output :
Explanation :
In the above program, arr is an array of three integers as shown below:
2 3 4
arr
p is a character pointer. p points to first byte of integer 2 as shown below:
0000000000000010
Hence *p is 2.
On incrementing p, it points to the next byte of integer 2. Now the value of *p is 0.
On incrementing the pointer p, again and re-executing the program, the following output is
generated.
p now points to first byte of integer 3.
Q. No. 194 Category : Operators
What will be output if you compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
206
void main()
{
int i,j=1,k=1;
i=j++&k++;
clrscr();
printf("j : %d k : %d i : %dn",j,k,i);
getch();
}
Output :
Explanation :
In the expression
i=j++&k++;
first condition evaluates to true. The value assigned to i is the bitwise AND operation between j
and k, after that the values of j and k are incremented. Since both j and k are 1, the result of
expression is 1 which is assigned to i, then j and k are incremented to 2.
Q. No. 195 Category : Operators
What will be the output of the program?
#include<stdio.h>
#include <conio.h>
int main()
{
char *str;
str = "%dn";
str++;
str++;
clrscr();
printf(str-2, 300);
getch();
return 0;
}
207
Output :
Explanation :
In the above program, str is a character string pointing to the first character of the string “%dn”
as shown below:
% d n
str
The pointer str is incremented twice as shown below:
% d n
str
The statement str-2 then relocates the pointer to the original location. Hence the statement
printf(str-2,300); becomes
printf(“%d”,300) which displays 300.
Q. No. 196 Category : Arrays
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("%cn", 7["SIBERINDIA"]);
getch();
return 0;
}
208
Output :
print "D" of SIBERINDIA
Explanation :
7["SIBERINDIA"] refers to the character at the index position 7 (index starts at 0). The character
at the index position 7 is „D‟, the same is printed.
In C, s[i] and i[s] are two different ways of referring to the ith
character of string s.
Q. No. 197 Category : Operators
What will be output if you compile and execute the following c code?
#include<stdio.h>
#include <conio.h>
int main()
{
int x=258;
char* p;
clrscr();
p=&x;
printf("*p = %un",*p);
p++;
printf("*p = %u",*p);
getch();
return 0;
}
209
Output :
Explanation :
The variable x occupies 16 bits arranged as shown below:
0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0
1 Byte 1 Byte
p initially points to the least significant byte of 2 byte storage space of x with a bit 1 at position 2
as shown in the Figure. Hence *p is equal to 2. On incrementing p, it will point to next byte of
integer with a bit 1 at position 1. Hence *p now equals 1 as evident from the above Figure.
Q. No. 198 Category : Operators
If the size of integer is 4bytes, What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[] = {12, 13, 14, 15, 16};
clrscr();
printf("%d, %d, %dn", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
getch();
return 0;
}
Output :
20, 4, 4
210
Explanation :
In the above program, arr refers to the integer array of 5 elements. Since the size of integer is 2
bytes,
Size of arr is 5 * 2 = 10
*arr and arr[0] both refer to the same entity, i.e. first element of the array, hence the size of *arr
and arr[0] is 2.
Q. No. 199 Category : Pointers and Arrays
What is the output generated on execution of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
char s1[]="SIBER";
char s2[]="MCA";
char *p;
s1=s2; Line 9
p=s2;
clrscr();
printf("%s",p);
getch();
}
Output :
Compiler Error
s1 is constant pointer, hence its value cannot be changed.
211
Explanation :
Array name is a constant pointer to the first element of the array. Hence array name cannot be
modified or it cannot appear on the left hand side of the assignment statement.
Q. No. 200 Category : Pointers and Arrays
What is the output generated on execution of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
char s[]={'a','b','c','n','c','0'};
char *p, *str, *strl;
p=&s[3];
str=p;
strl=s;
clrscr();
printf("%dn",++*p);
printf("%d",++*p + ++*strl - 32);
getch();
}
Output :
Explanation :
In the above program, s is a pointer to the character array and p points to the character at the
position 3, as shown below:
a b c n C 0
s p
str and str1 are initialized to p and s, respectively, as shown below:
212
a b c n c 0
s str1 p str
Now, *p is 10, hence ++*p is 11.
++*str1 is 99.
Hence, ++*p + ++*strl – 32 = 11 + 99 – 32 = 78
Q. No. 201 Category : Operators
What is the output generated on execution of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
int a=10,b=-1,x,y,z;
clrscr();
x=(a!=10)&&(b=1);
printf("x=%d",x);
printf("b=%dn",b);
y=(a==10)&&(b=0);
printf("y=%d",y);
printf("b=%dn",b);
z=(a==10)&&(b=1);
printf("z=%d",z);
printf("b=%dn",b);
getch();
}
Output :
Explanation :
In C, logical operators employ short-circuit evaluation, i.e. in the evaluation of logical AND
213
operation shown below
Condition1 && Condition2
Condition2 is evaluated if and only if Condition1 is true since then the output of the complex
condition depends on Condition2.
Similarly, for the similar reason, in the evaluation of logical OR operation shown below
Condition1 || Condition2
Condition2 is evaluated if and only if Condition1 is false since then the output of the complex
condition depends on Condition2.
In the above program, a = 10 and b = -1.
Step 1 :
The condition x=(a!=10)&&(b=1);is evaluated as follows:
The first condition a!=10 evaluates to false. Hence second condition is not evaluated. Hence the
value of b is not modified and is still -1. Since the complex condition evaluates to false, 0 is
assigned to x.
Hence the first printf statement displays x=0 and b=-1
Step 2 :
The condition y=(a==10)&&(b=0);is evaluated as follows:
The first condition a==10 evaluates to true. Hence second condition is evaluated which assigns
0 to b. Hence the value of b is modified to 0 and the condition becomes false. Since the complex
condition evaluates to false, 0 is assigned to y.
Hence the second printf statement displays y=0 and b=0.
Step 3 :
The condition z=(a==10)&&(b=1);is evaluated as follows:
The first condition a==10 evaluates to true. Hence second condition is evaluated which assigns
214
1 to b. Hence the value of b is modified to 1 and the condition becomes true. Since the complex
condition evaluates to true, 1 is assigned to z.
Hence the third printf statement displays z=1 and b=1.
Q. No. 202 Category : Operators
What is the output generated on execution of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
int x=2,y=4;
clrscr();
printf("%d %dn",x,y);
x^=y;
printf("%dn",x);
y^=x;
printf("%dn",y);
x^=y;
printf("%d %d",x,y);
getch();
}
Output :
Explanation :
^ is a bitwise XOR operator in C.
The bitwise representation of x and y is as follows:
0 0 0 0 0 0 1 0 and
0 0 0 0 0 1 0 0
Hence x ^ y = 0 0 0 0 0 1 1 0, whose decimal equivalent is 6 (=x).
215
Similarly, (0 0 0 0 0 1 1 0) ^ (0 0 0 0 0 1 0 0 ) = 0 0 0 0 0 0 1 0, whose decimal equivalent is 2
(=y).
and(0 0 0 0 0 1 1 0) ^ (0 0 0 0 0 0 1 0 ) = 0 0 0 0 0 0 1 0, whose decimal equivalent is 4 (=x).
Hence, at this point x=4 and y=2.
This example illustrates swapping two values without any intermediate variable.
Q. No. 203 Category : Operators
What is the output generated on execution of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
int x=20,y=35;
clrscr();
x=y++ + x++;
y=++y + ++x;
printf("%d %d",x,y);
getch();
}
Output :
Explanation :
The contents of the different memory locations storing x and y get updated as shown below:
x y
Initial Values 20 35
After the execution of the statement
x=y++ + x++;
56 36
After the execution of the statement
y=++y + ++x;
57 94 (57+37)
216
Q. No. 204 Category : Data Types
Why reference is not same as a pointer?
A. A reference can never be null.
B. A reference once established cannot be changed.
C. Reference doesn't need an explicit dereferencing mechanism.
D. All of the above.
Output :
Option D
Explanation :
A reference can be viewed as an alternative identifier for a value stored in computer memory
which enables the data to be indirectly accessed.
The following example creates a reference to a variable x with the name xref.
int x=10;
int& xref=x;
Once defined, a reference cannot be reassigned a new value. A reference always refers to the
object with which it is initialized.
Consider the following C program:
#include<stdio.h>
#include<conio.h>
int main()
{
int x=10;
int y=20;
int& xref=x;
xref=y;
clrscr();
printf("x : %d",x);
getch();
return 0;
}
In the above example, xref is a reference to the memory location where x is stored. Hence the
217
same memory location is referenced by two variables x and xref as shown below:
x
xref
After the execution of the statement
xref=y;
x
xref
The content of the memory location pointed by xref will be updated by y (=20). Hence indirectly
the value of x also gets modified to 20 as seen from the following output generated on execution
of the program.
Hence xref is an alias to x. The contents of the memory location can be modified using either x
or xref.
Some salient features of a reference which differentiate it from pointer are listed below:
A pointer is memory address of an object stored in computing memory whereas an alternative
identifier for a value stored in computer memory. In this context it plays the role of an alias for a
variable stored in memory.
A reference cannot be dereferenced and make point another memory location.
A pointer may not be initialized at the time of declaration or can be null indicating invalid
reference. In contrast to this a reference must be initialized at the time of declaration and once
10
20
218
initialized it cannot be dereferenced and point to another memory location. It continues to
reference the memory location with which it is initialized at the time of declaration. The contents
of the memory location can be modified using either a variable or its reference.
Q. No. 205 Category : Control Statements
What is the output generated on execution of the following program?
#include<stdio.h>
#include<conio.h>
int main()
{
int x=1, y=1;
clrscr();
for(; y; printf("%d %dn", x, y))
{
y = x++ <= 5;
}
printf("n");
getch();
return 0;
}
utput :
Explanation :
In the above program, x =1 and y=1.
In the for loop, the condition specified is y. Hence initially the condition becomes true and the
body of the for loop is executed. The body of the for loop contains the statement
y = x++ <= 5;
which is evaluated as follows:
219
Step 1 : The condition x <= 5 is evaluated.
Step 2 :The output of the condition in Step 1 is a assigned to y.
Step 3 : The value of x is incremented.
Hence after the first iteration, x=2 and y=1.
After second iteration, x=3 and y=1.
After third iteration, x=4 and y=1.
After fourth iteration, x=5 and y=1.
After sixth iteration, x=6 and y=1.
After sixth iteration, the condition becomes false and 0 is assigned to y and x is incremented to 7.
Since y=0, the condition in for loop evaluates to false and control comes out of the for loop.
Q. No. 206 Category : Control Statements
What is the output generated on execution of the following program?
#include<stdio.h>
#include <conio.h>
int main()
{
int i = 5;
clrscr();
while(i-- >= 0)
printf("%d,", i);
i = 5;
printf("n");
while(i-- >= 0)
printf("%i,", i);
while(i-- >= 0)
printf("%d,", i);
getch();
return 0;
}
220
Output :
Explanation :
Step 1: Initially the value of variable i is '5'.
Loop 1:while(i-- >= 0) here i = 5, this statement becomes while(5-- >= 0) Hence the while
condition is satisfied and it prints '4'. (variable 'i' is decremented by '1'(one) in previous while
condition)
Loop 2: while(i-- >= 0) here i = 4, this statement becomes while(4-- >= 0) Hence the while
condition is satisfied and it prints '3'. (variable 'i' is decremented by '1'(one) in previous while
condition)
Loop 3: while(i-- >= 0) here i = 3, this statement becomes while(3-- >= 0) Hence the while
condition is satisfied and it prints '2'. (variable 'i' is decremented by '1'(one) in previous while
condition)
Loop 4: while(i-- >= 0) here i = 2, this statement becomes while(2-- >= 0) Hence the while
condition is satisfied and it prints '1'. (variable 'i' is decremented by '1'(one) in previous while
condition)
Loop 5: while(i-- >= 0) here i = 1, this statement becomes while(1-- >= 0) Hence the while
condition is satisfied and it prints '0'. (variable 'i' is decremented by '1'(one) in previous while
condition)
Loop 6: while(i-- >= 0) here i = 0, this statement becomes while(0-- >= 0) Hence the while
221
condition is satisfied and it prints '-1'. (variable 'i' is decremented by '1'(one) in previous while
condition)
Loop 7:while(i-- >= 0) here i = -1, this statement becomes while(-1-- >= 0) Hence the while
condition is not satisfied and loop exits.
The output of first while loop is 4,3,2,1,0,-1
Step 2: Then the value of variable i is initialized to '5' Then it prints a new line character(n).
See the above Loop 1 to Loop 7 .
The output of second while loop is 4,3,2,1,0,-1
Step 3: The third while loop, while(i-- >= 0) here i = -1(because the variable 'i' is decremented to
'-1' by previous while loop and it never initialized.). This statement becomes while(-1-- >= 0)
Hence the while condition is not satisfied and loop exits.
Hence the output of the program is
4,3,2,1,0,-1
4,3,2,1,0,-1
Q. No. 207 Category : Control Statements
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
char j=1;
clrscr();
while(j < 5)
{
printf("%d, ", j);
j = j+1;
}
printf("n");
getch();
return 0;
}
222
Output :
Explanation :
Internally, char data type stores ASCII code of a character assigned to it. Hence any arithmetic
operation is allowed on it. Thus, incrementing the char variable makes it point to the next
alphabet in the character sequence. printf () statement displays the ASCII code of the character j.
Q. No. 208 Category : Operators
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
int x, y, z;
x=y=z=1;
z = ++x || ++y && ++z;
clrscr();
printf("x=%d, y=%d, z=%dn", x, y, z);
getch();
return 0;
}
Output :
Explanation :
Step 1: x=y=z=1; here the variables x ,y, z are initialized to value '1'.
223
Step 2: z = ++x || ++y && ++z; becomes z = ( (++x) || (++y && ++z) ). Here ++x becomes 2. So
there is no need to check the other side because ||(Logical OR) condition is satisfied.
(z = (2 || ++y && ++z)). There is no need to process ++y && ++z. Hence it returns '1'. So
the value of variable z is '1'
Step 3: printf("x=%d, y=%d, z=%dn", x, y, z); It prints "x=2, y=1, z=1". here x is incremented
in previous step. y and z are not incremented.
Q. No. 209 Category : Arrays
What will be output if you compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][2]={100,2,3,4};
clrscr();
printf("%d %d", a[3], *(*(a+1)+1));
getch();
}
Output :
Explanation :
a is declared to be a 2D array of size 2 x 2. There are two ways in which the array a can be
initialized.
Method 1 :
int a[2][2]={100,2,3,4};
as shown above.
224
Method 2 :
int a[2][2]={{100,2},{3,4}};
In either case, four contiguous memory locations are allocated for storing the elements of a.
Irrespective of how the array is initialized, its elements can only be accessed using two
dimensions. Using single dimension for retrieving the element will generate garbage as shown
above.
The correct version of the program is: (modified statement is shown in bold)
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][2]={100,2,3,4};
clrscr();
printf("%d %d", a[1][1], *(*(a+1)+1));
getch();
}
On execution of the above program, the following output is generated.
Q. No. 210 Category : Data Types
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
char *str;
225
str = "%s";
clrscr();
printf(str, "Kn");
getch();
return 0;
}
Output :
Explanation :
The first argument, the format string of a printf() statement can be a variable in C. str is assigned
a format string for printing a string. Hence the printf() statement becomes,
printf(“%s”, “Kn”);
and displays the string K.
Q. No. 211 Category : Pointers
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
void *vp;
char ch=74, *cp="SIBER";
int j=65;
vp=&ch;
clrscr();
printf("%c", *(char*)vp);
vp=&j;
printf("%c", *(int*)vp);
vp=cp;
printf("%s", (char*)vp+2);
getch();
return0;
}
226
Output :
Explanation :
vp is a void pointer pointing to the character whose ASCII code is 74 (J). Hence the first printf()
statement prints the character J.
In the next statement, the pointer vp is dereferenced and starts pointing integer j. The next
printf() statement prints the character with the ASCII value „A‟.
In the subsequent statement, the pointer vp is dereferenced again and now starts pointing to the
string cp. The last printf() statement prints the characters of the string “SIBER” starting with
third character up to the end of string, i.e. BER.
The concatenation of three printf() statements results in a string, JABER, being output by the
program.
Q. No. 212 Category : Arrays
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("%cn", 3["SIBER"]);
getch();
return 0;
}
227
Output :
Explanation :
3[“SIBER”] retrieves the element at the index 3 (index starts from 0), i.e. character E.
Consider the declaration:
char[] s=”SIBER”;
In the above declaration, s[3], 3[s] refer to the same element at the index position 3. 3[“SIBER”]
is also a valid statement in C.
Q. No. 213 Category : Operators
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
char str[] = "peace";
char *s = str;
clrscr();
printf("%sn", s++ +3);
getch();
return0;
}
Output :
228
Explanation :
In the above program, the expression
s++ +3
is evaluated as follows
(s++) +3
Since ++ is a post-increment operator, the expression s+3 is evaluated first and then s is
incremented. (s+3) points to the character „c‟ of a string “peace”. Hence the printf() statement
displays c up to the end of the string i.e. ce is displayed as output.
Q. No. 214 Category : Operators
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
char *p;
p="hello";
clrscr();
printf("%sn", *&*&p);
getch();
return 0;
}
Output :
Explanation :
In the printf() statement
229
printf("%sn", *&*&p);
*& cancel out and the expression becomes,
printf("%sn", p);
which simply displays string s.
Q. No. 215 Category : Pointers and Arrays
What will be the output of the program assuming that the array begins at location 1002?
#include<stdio.h>
#include<conio.h>
int main()
{
int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2},
{2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
clrscr();
printf("%u, %u, %u, %dn", a, *a, **a, ***a);
getch();
return 0;
}
Output :
1002, 1002, 1002, 1
Explanation :
In the above program, a is a 3D array of dimensions 2 x 3 x 4. The 24 elements of array a are
stored in contiguous memory locations. ***a is an array of integer pointers of size 24. **a is an
array of integer pointers of size 12, *a is an array of integer pointers of size 4. All three store the
base address of the array a.
Using pointer notation, the ijkth
element of an array can be accessed as
230
* (*(*(a + i)+j)+k)
Hence the syntax to access the first element of the array is ***a. Hence the output 1 is generated
by the program.
Q. No. 216 Category : Functions
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
power(int**);
int main()
{
int a=5, *aa; /* Address of 'a' is 1000 */
aa = &a;
a = power(&aa);
clrscr();
printf("%dn", a);
getch();
return 0;
}
power(int **ptr)
{
int b;
b = **ptr***ptr;
return (b);
}
Output :
Explanation :
The snapshot of memory after storing variables is as follows:
a aa
1000 2000
5 1000
231
The function power() is invoked with actual argument &aa.
Hence, int** ptr = &aa
Thus, *ptr=aa and **ptr=*aa = a.
The expression, b = **ptr***ptr; is evaluated as follows:
b = (**ptr) * (**ptr) = 5 * 5 = 25, the same is returned and printed by the printf() statement in the
main() function.
Q. No. 217 Category : Pointers
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
int main()
{
char str1[] = "siber";
char str2[] = "MCA";
char *s1 = str1, *s2=str2;
clrscr();
while(*s1++ = *s2++)
printf("%s", str1);
printf("n");
getch();
return 0;
}
Output :
Explanation :
In the above program, str1 and str2 point to the string “siber” and “MCA”, respectively and the
character pointers s1 and s2 are initialized with str1 and str2, respectively as shown below:
232
S i b e r 0
str1 s1
M C A 0
str2 s2
In the first iteration of the loop, the content of the memory location pointed by s1 is updated by
the content of the memory location pointed by s2. After this, the string s1 becomes,
M i b e r 0
str1 s1
Hence the string Miber gets displayed and both the pointers s1 and s2 get incremented.
In the second iteration of the loop, the content of the memory location pointed by s1 is updated
by the content of the memory location pointed by s2. After this, the string s1 becomes,
M C b e r 0
str1 s1
M C A 0
str2 s2
Hence the string MCber gets displayed and both the pointers s1 and s2 get incremented.
Repeating the same argument, MCAer gets displayed in the third iteration of the loop and
pointer s2 points to „0‟ and *s2 („0‟) assigned to *s1 and the while loop terminates.
Hence the program generates the output,
MiberMCberMCAer
Q. No. 218 Category : I/O
What will be the output of the program?
233
#include<stdio.h>
#include<string.h>
#include <conio.h>
int main()
{
int x=10;
clrscr();
printf("n",x);
getch();
return 0;
}
Output : No Output
Explanation :
Since the printf() statement contains only n in the format string and since %d is missing, the
value of x will not be displayed.
Q. No. 219 Category : Pointers
What will be the output of the program?
#include<stdio.h>
#include<string.h>
#include <conio.h>
int main()
{
int i, n;
char *x="SIBER";
n = strlen(x);
*x = x[n];
clrscr();
for(i=0; i<=n; i++)
{
234
printf("%s ", x);
x++;
}
printf("n", x);
getch();
return 0;
}
Output :
Explanation :
In the above program, x is a pointer to a string “SIBER” as shown below:
S I B E R 0
x
The statement
*x = x[n];
places „0‟ in the memory location pointed by x as shown below:
0 I B E R 0
x
In the first iteration of for loop, the string x is displayed. The characters of x are displayed till
„0‟ character is encountered. Since x points to „0‟ character, in the first iteration nothing is
displayed.
Then, the pointer x gets incremented by 1 as shown below:
0 I B E R 0
235
x
Hence in the second iteration of the for loop, x is pointing to the character „I‟. The characters
starting with „I‟ are displayed till „0‟ character is encountered. Hence IBER gets displayed in the
second iteration of the for loop.
Again the pointer x gets incremented by 1 as shown below:
0 I B E R 0
x
Third iteration of for loop displays, BER and increments the pointer so that it points to the next
memory location.
Fourth iteration of for loop displays, ER and increments the pointer so that it points to the next
memory location and finally,
fifth iteration of for loop displays, R and increments the pointer so that it points to the next
memory location.
The last printf() statement only displays a new line since no format string is specified.
Q. No. 220 Category : Operators
What will be output if you compile and execute the following c code?
#include<stdio.h>
#include <conio.h>
int main()
{
char *str;
str = "%dn";
str++;
*str='c';
str--;
clrscr();
printf(str,65);
236
getch();
return 0;
}
Output :
Explanation :
In the above program, str is a character array pointing to the first element of a string “%dn” as
shown below:
% d n
str
str is incremented and the content of the memory location pointed by it is replaced with „c‟ as
shown below:
% c n
str
Next, the pointer str is decremented, so that it now points to the first character of the string
“%cn” and the
printf(str,65);
statement becomes
printf(“%cn”,65);
which displays character „A‟ on the console.
Q. No. 221 Category : Functions
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
237
void change(int*, int);
int main()
{
int i, a[] = {2, 4, 6, 8, 10};
change(a, 5);
clrscr();
for(i=0; i<=4; i++)
printf("%d, ", a[i]);
getch();
return 0;
}
void change(int *b, int n)
{
int i;
for(i=0; i<n; i++)
*(b+1) = *(b+i)+5;
}
Output :
Explanation :
The base address of the array a is passed as the first argument to the change() method. The
change() method iterates through every element of the array and modifies the second element
with the sum of 10 (last element of the array) and 5. Hence the array after invoking change()
method is
2 15 6 8 10
The same is printed in the main() method.
Q. No. 222 Category : Pointers and Arrays
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
238
int main()
{
static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
clrscr();
printf("%s", **p+1);
getch();
return 0;
}
Output :
Explanation :
In the above program, s points to the array of strings {"black", "white", "pink", "violet"}.
ptr points to the array of strings in the reverse order.
p is initialized with ptr, hence points to the first element of ptr. In the next statement, p is
incremented so that now it points to the string “pink”. The next printf() statement advances the
pointer by one character and prints the characters of the string “pink” from second character to
the end of character. Hence the program generates “ink” as output.
Q. No. 223 Category : Operators
What will be the output of the program?
#include<stdio.h>
#include<conio.h>
void main()
{
int i=5;
clrscr();
printf("%d%d%d%d%d",--i,i--,++i,i++,i);
getch();
}
239
Output :
Explanation :
The arguments in a function call are pushed into the stack from left to right. The evaluation is by
popping out from the stack. and the evaluation is from right to left, hence the result. The
expression
printf("%d%d%d%d%d%d",--i,i--,++i,i++,i);
is evaluated as follows:
Evaluation
--i i-- ++i i++ i
5 7 7 5 5
Display
Q. No. 224 Category : Operators
What will be the output of the program?
#include <stdio.h>
#include <conio.h>
void main()
{
char *p="hai friends",*p1;
p1=p;
clrscr();
while(*p!='0')
++*p++;
printf("%s %s",p,p1);
getch();
}
240
Output :
Explanation :
In the given program, p is a character pointer pointing to the first character of the string “hai
friends”. p1 is initialized with p. Hence both p and p1 point to the same memory location.
The statement ++*p++ is parsed as follows:
i. *p is computed
ii. ++*p, *p is incremented
iii. p++ is computed, p advances by 1 byte and points to the next memory location
The above three statements constitute a while loop. The loop is executed till the null character,
„0‟ is encountered.
In the first iteration of the loop,
i. *p = h
ii. ++*p = i
iii. p++
i a i f r i e n d S 0
p1 p
After second iteration
i b i f r i e n d S 0
p1 p
241
and so on.
After the last iteration,
i b j ! g s j f o e t 0
p1 p
Now p1=”ibj!gsjfoet” and p=”0”
Thus, p does not print anything while p1 prints ibj!gsjfoet
Hence the given program generates the following output.
ibj!gsjfoet
Q. No. 225 Category : Built-in Functions
What will be the output of the program?
#include <conio.h>
void main()
{
clrscr();
}
clrscr();
Output :
No output/error
Explanation :
The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is
a function declaration (because it is not inside any function).
Q. No. 226 Category : Abstract Data Types
What will be the output of the program?
#include <stdio.h>
#include <conio.h>
enum colors {BLACK,BLUE,GREEN}
242
void main()
{
clrscr();
printf("%d..%d..%d",BLACK,BLUE,GREEN);
getch();
return(1);
}
Output :
0..1..2
Explanation :
enum assigns numbers starting from 0, if not explicitly defined.
Q. No. 227 Category : Operators
What will be the output of the program?
#include <stdio.h>
#include<conio.h>
void main()
{
int i=5;
clrscr();
printf("%d",i+++++i); Line 8
getch();
}
Output :
Compiler Error
Explanation :
The expression i+++++i is parsed as i ++ ++ + i which is an illegal combination of operators.
243
Q. No. 228 Category : Control Statements
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j;
for(i=0;i<=10;i++)
{
j+=5;
assert(i<5);
}
}
Output :
Runtime error: Abnormal program termination.
assert failed (i<5), ,
Explanation :
asserts are used during debugging to make sure that certain conditions are satisfied. If assertion
fails, the program will terminate reporting the same. After debugging use,
#undef NDEBUG
and this will disable all the assertions from the source code. Assertion is a good debugging tool
to make use of.
Q. No. 229 Category : Operators
Predict the output or error(s) for the following:
244
#include <stdio.h>
#include <conio.h>
void main()
{
char not;
not=!2;
clrscr();
printf("%d",not);
getch();
}
Output :
Explanation :
! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any
non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so
TRUE. !TRUE is FALSE (0) so it prints 0.
Q. No. 230 Category : Preprocessors
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
#define TRUE 1
#define NULL 0
#define FALSE -1
void main()
{
clrscr();
if(NULL)
puts("NULL");
else if(FALSE)
puts("TRUE");
245
else
puts("FALSE");
getch();
}
Output :
Explanation :
The input program to the compiler after processing by the preprocessor is,
void main()
{
if(0)
puts("NULL");
else if(-1)
puts("TRUE");
else
puts("FALSE");
}
Preprocessor doesn't replace the values given inside the double quotes. The check by if condition
is boolean value false so it goes to else. In second if -1 is boolean value true, hence "TRUE" is
printed.
Q. No. 231 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int y;
scanf("%d",&y); // input given is 2000
clrscr();
246
if( (y%4==0 && y%100 != 0) || y%100 == 0 )
printf("%d is a leap year");
else
printf("%d is not a leap year");
getch();
}
Output :
Explanation :
An ordinary program to check if leap year or not.
Q. No. 232 Category : Operators
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=-1;
-i;
clrscr();
printf("i = %d, -i = %d n",i,-i);
getch();
}
Output :
247
Explanation :
-i is executed and this execution doesn't affect the value of i. In printf first you just print the
value of i. After that the value of the expression -i = -(-1) is printed.
Q. No. 233 Category : Variable Declaration Issues
In the following code, the p2 is Integer Pointer or Integer?
typedef int *ptr;
ptr p1, p2;
A. Integer
B. Integer pointer
C. Error in declaration
D. None of above
Explanation :
The statement
ptr p1,p2;
is equivalent to
int* p1,p2;
Hence p2 is an integer pointer. To clarify the issue consider the following program:
#include <stdio.h>
#include <conio.h>
void main()
{
typedef int *ptr;
ptr p1, p2;
int x=10;
p2=&x;
clrscr();
printf("%d",*p2);
getch();
}
On execution of the above program, the following output is generated.
248
In the above program, both p1 and p2 are integer pointers. The address of x is assigned to p2, p2
is dereferenced and printed in the next printf() statement. Hence the program displays 10 as
output.
Q. No. 234 Category : Macros
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
#define x 5+2
void main()
{
int i;
i=x*x*x;
clrscr();
printf("%d",i);
getch();
}
Output :
Explanation :
The input program to the compiler after processing by the preprocessor is,
249
void main()
{
int i;
i=5+2*5+2*5+2;
clrscr();
printf("%d",i);
getch();
}
The expression
i=5+2*5+2*5+2;
evaluates to 27.
Change the #define macro to
#define x (5+2)
and re-execute the program. The following output is generated:
The expression now becomes,
i = (5+2)*(5+2)*(5+2)
= 343, hence the output.
Q. No. 235 Category : Pointers
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=320;
char *ptr=(char *)&i;
250
clrscr();
printf("%d",*ptr);
getch();
}
Output :
Explanation :
Since the size of the int data type is 2 bytes while the char pointer can point 1 bytes at a time,
initially a char pointer points to only the first byte of a 2 byte integer as shown in the following
Figure.
Memory Representation of int i = 320 (256+64)
First Bye Second Byte
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1
*ptr is the content of the first byte i.e. 01000000 whose decimal equivalent is 64. Hence the
program generates the output 64.
Increment the pointer ptr as shown in the following program and re-execute it. (The new
statement added is shown in bold).
#include <stdio.h>
#include <conio.h>
ptr Sign bit
251
void main()
{
int i=320;
char *ptr=(char *)&i;
ptr++;
clrscr();
printf("%d",*ptr);
getch();
}
On execution of the above program, the following output is generated.
The pointer, ptr, now points to the second byte of the integer 320, 00000001, whose decimal
equivalent is 1.
Q. No. 236 Category : Data Types
Predict the output or error(s) for the following:
#include <stdio.h>
#include <conio.h>
void main()
{
char c=125;
c=c+10;
clrscr();
printf("%d",c);
getch();
}
252
Output :
Explanation :
The range of the char data type is 127 to -128. The char data type shows cyclic properties, i.e. if
the char variable is increased beyond its maximum value, then it attains the minimum value of -
128. Similarly, if the char variable is decreased beyond its minimum value, then it attains a
maximum value of 127, in a cyclic order as shown below:
Hence the variable c is incremented as shown in the following table:
125+1=126
125 + 2 =127
125 + 3 = -128
125 + 4 = -127
125 + 5 = -126
125 + 6 = -125
125 + 7 = -124
125 + 8 = -123
125 + 9 = -122
125 + 10 = -121
Hence the given program generates the output -121.
Consider the following program:
#include <stdio.h>
#include <conio.h>
Char
127
126
125
-128
-126
-127
0
-1
1 2
253
void main()
{
char c=127;
c++;
clrscr();
printf("%d",c);
getch();
}
On execution of the above program, the following output is generated.
Modify the above program as shown below and re-execute it.
#include <stdio.h>
#include <conio.h>
void main()
{
char c=-128;
c--;
clrscr();
printf("%d",c);
getch();
}
On execution of the above program, the following output is generated.
Q. No. 237 Category : Data Types
What will be output if you will compile and execute the following ccode?
254
#include <stdio.h>
#include <conio.h>
void main()
{
float a=5.2;
clrscr();
if(a==5.2)
printf("Equal");
else if(a<5.2)
printf("Less than");
else
printf("Greater than");
getch();
}
Output :
Explanation :
In C, floating point literal is of type double and the size of a double data type is 8 bytes while the
size of a float variable is 4 bytes. In memory, float and double data types are stored completely in
a different manner. In the given program, the variable a is of type float while 5.2 is a double
constant. The double constant , 5.2 is stored in memory as
101.00 11001100 11001100 1100110011001100 11001100 11001100
while the variable a is stored in memory as
101. 00110 01100110 01100110
It is clear that the variable a is less than the double constant 5.2. Since 5.2 is a recurring float
number its value of different for float and double. Since a is less than 5.2, the given program
generates the output “Less than”.
255
Consider the following program:
include <stdio.h>
#include <conio.h>
void main()
{
float f=5.2;
clrscr();
printf("%.10f %.10f",f,5.2);
getch();
}
On execution of the above program, the following output is generated.
As seen from the output generated, floating point 5.2 is always less that double 5.2.
Q. No. 238 Category : Operators
What will be output if you will compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
void main()
{
int i=4,x;
x=++i + ++i + ++i;
clrscr();
printf("%d",x);
getch();
}
256
Output :
Explanation :
In the expression ++a, ++ is the pre-increment operator. In any mathematical expression, pre-
increment operator is evaluated first up to the break point which then starts assigning the final
value to all the variables. The expression
x=++i + ++i + ++i;
is evaluated in two steps given below:
Step 1 : Increment the variable i up to the break point.
x=++i + ++i + ++i;
5 6 7
Step 2 : Start assigning the final value of i (=7) to all the variables i in the expression.
Hence x = 7 + 7 + 7 =21
Hence the program generates the output 21.
Change the expression to the one shown below and re-execute the program.
x=++i + i++ + ++i;
On execution of the program, following output is generated.
257
The expression is now evaluated as
Step 1:
x=++i + i++ + ++i;
5 5 6
Step 2 : x = 6 + 6 + 6 = 18.
Hence the program generates the output 18.
After the evaluation of the statement the value of i becomes 7.
Q. No. 239 Category : Operators
What will be output if you will compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
void main()
{
int a=10;
clrscr();
printf("%d %d %d",a,a++,++a);
getch();
}
Output :
Explanation :
In C, printf function follows cdecl parameter passing scheme in which parameters are passed
from right to left. Hence the expression
printf("%d %d %d",a,a++,++a);
is evaluated as follows:
258
Before the function call, the value of a is 10.
The right most parameter is ++a, hence a is incremented to 11. Hence the value of a is 11. The
next parameter is a++, hence the second parameter is 11 and after that the value of a is
incremented to 12. As a result third parameter (from right) becomes 12.
On evaluation of parameters, printf() statement becomes,
printf("%d %d %d",12,11,11);
Hence the program generates the output 12, 11, 11.
Change the printf() statement to
printf("%d %d %d",a,++a,a++);
and re-execute the program. The following output is generated.
In the modified printf() statement, the rightmost parameter is 10, then a is incremented to 11, in
the next parameter a is incremented to 12, then the same is used in the third parameter
The modified printf statement is equivalent to
printf("%d %d %d",12,12,10);
Hence the program generates the output 12 12 10.
Q. No. 240 Category : Operators
What will be output if you will compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
259
void main()
{
clrscr();
printf("%d",sizeof(5.2));
getch();
}
Output :
Explanation :
The default type of the floating point constant is double, i.e. the floating point literal is of type
double. Hence 5.2 is double constant and hence its size is 8 bytes.
In order to render the type of floating point constant as float, suffix either „f‟ or „F‟ to the
constant as shown below:
5.2f
To illustrate this modify the program as shown below (change is shown in bold) and re-execute
it.
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf("%d",sizeof(5.2f));
getch();
}
On execution of the above program, the following output is generated.
260
As evident from the output generated, the type of 5.2f is float and hence the size is 4 bytes.
Q. No. 241 Category : Macros
What will be output if you will compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
#define call(x,y) x##y
void main()
{
int x=5,y=10,xy=20;
clrscr();
printf("%d",xy+call(x,y));
getch();
}
Output :
Explanation :
## is a concatenation operator in preprocessor directive. It concatenates its two operands as
shown below:
x##y = xy
On preprocessing, the following pseudo code is generated before the actual compilation. call(x,y)
is replaced with xy.
261
void main()
{
int x=5,y=10,xy=20;
clrscr();
printf("%d",xy+xy);
getch();
}
Hence the above program generates the output 40.
Q. No. 242 Category : Arrays
What will be output if you will compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
void main()
{
int array[]={10,20,30,40};
clrscr();
printf("%d",-2[array]);
getch();
}
Output :
Explanation :
In C, the different ways of accessing the ith
element of an array, a are,
a[i] = *(a+i) = *(i+a) = i[a].
Hence the second element of array, array can be accessed as 2[array] and – sign is a unary –
operator.
Hence -2[array] = -30, which is generated as an output of a program.
262
Q. No. 243 Category : Storage Classes
What will be output if you will compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
void main()
{
int i=10;
static int x=i; Line 7
clrscr();
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");
else
printf("Less than");
getch();
}
Output :
Compiler Error
Explanation :
Since static variables are load time entities, i.e. the memory for the static variables is allocated
when the program is loaded into the memory while auto variables are run time entities, i.e. the
memory for the auto variables is allocated at the program execution time. Hence, the runtime
variables are not available to the load time variables. Hence, we cannot initialize any load time
variable with the runtime variable.
In the above program, i is a run time variable while x is a load time variable. Hence we
arrive at the following rules:
263
Rule 1 : A runtime variable can be initialized with another runtime variable.
Rule 2 : A load time variable can not be initialized with another load time variable.
Rule 3 : A runtime variable can be initialized with load time variable.
Rule 4 : A load time variable can be initialized with runtime variable.
Consider the following program.
#include <stdio.h>
#include <conio.h>
void main()
{
static int i=10;
int x=i;
clrscr();
printf("x : %d",x);
getch();
}
On execution of the above program, the following output is generated.
Since s is a runtime variable, it can be initialized with the load time variable i.
Q. No. 244 Category : Operators
What will be output if you will compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
#define max 5;
264
void main()
{
int i=0;
i=max++; Line 8
clrscr();
printf("%d",i++);
getch();
}
Output :
Compiler Error
Explanation :
On preprocessing the macro, the following pseudo code is generated. The variable max is
replaced with 5.
void main()
{
int i=0;
i=5++;
clrscr();
printf("%d",i++);
getch();
}
As seen, the macro constant Max has been replaced with 5. Since it is illegal to increment the
constant number, the compiler will generate an error message, „Expression Syntax‟.
Q. No. 245 Category : Pointers and Arrays
What will be output if you compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
265
int main(void)
{
int arr[] = {10, 20};
int *p = arr;
++*p;
clrscr();
printf("*p = %d", *p);
getch();
return 0;
}
Output :
Explanation :
Precedence of prefix ++ and * is same. Associativity of both is right to left. Hence the expression
++*p is equivalent to
++(*p)
Initially p is pointing to the element 10 or array, arr. Hence *p is equal to 10 and ++(*p) is equal
to 11.
Q. No. 246 Category : Pointers and Arrays
What will be output if you compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
int main(void)
{
int arr[] = {10, 20};
int *p = arr;
*p++;
clrscr();
266
printf("*p = %d", *p);
getch();
return 0;
}
Output :
Explanation :
Precedence of postfix ++ is higher than both * and prefix ++. Hence the expression
*p++ is equivalent to
*(p++)
Initially p is pointing to the element 10 or array, arr. After the execution of p++, p will point to
element 20 of array, arr. Hence dereferencing it, the content of the memory location, 20 is
returned.
Hence *(p++) is equal to 20.
Q. No. 247 Category : Pointers and Arrays
What will be output if you compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
int main(void)
{
int arr[] = {10, 20};
int *p = arr;
*++p;
clrscr();
printf("*p = %d", *p);
getch();
return 0;
}
267
Output :
Explanation :
The expression *++p has two operators of same precedence, so compiler looks for associativity.
Associativity of operators is right to left. Therefore the expression is treated as *(++p). Therefore
the output of second program is “arr[0] = 10, arr[1] = 20, *p = 20.
Q. No. 248 Category : Variable Declaration Issues
What will be output if you compile and execute the following c code?
#include<stdio.h>
#include <conio.h>
int main()
{
int a = 1, 2, 3; Line 6
clrscr();
printf("%d", a);
getch();
return 0;
}
Output :
268
Explanation :
In a C/C++ program, comma is used in two contexts: (1) A separator (2) An Operator. In the
above program, comma works just as a separator, hence the program generates a compiler error.
Q. No. 249 Category : Operators
What will be output if you compile and execute the following c code?
#include<stdio.h>
#include <conio.h>
int main()
{
int a;
a = 1, 2, 3;
clrscr();
printf("%d", a);
getch();
return 0;
}
Output :
Explanation :
In the above program, comma works as an operator. Precedence of comma operator is least in
operator precedence table. So the assignment operator takes precedence over comma and the
expression “a = 1, 2, 3” becomes equivalent to “(a = 1), 2, 3”
Q. No. 250 Category : Operators
What will be output if you compile and execute the following c code?
#include<stdio.h>
269
#include <conio.h>
int main()
{
int a;
a = (1, 2, 3);
clrscr();
printf("%d", a);
getch();
return 0;
}
Output :
Explanation :
In the above program, brackets are used so comma operator is executed first and we get the
output as 3.
Q. No. 251 Category : Operators
What will be output if you compile and execute the following c code?
#include <stdio.h>
#include <conio.h>
int main()
{
int a = 10, b = 20, c = 30;
clrscr();
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
getch();
return 0;
}
270
Output :
Explanation :
(c > b > a) is treated as ((c > b) > a), associativity of '>' is left to right. Therefore the value
becomes ((30 > 20) > 10) which becomes (1 > 20)
References:
1. https://www.indiabix.com/c-programming
2. http://www.c4learn.com/c-programming
3. http://www.includehelp.com/c-programming-aptitude-questions-and-answers.aspx
4. http://www.sanfoundry.com/c-programming-examples/
5. http://www.c-program-example.com/p/c-aptitude.html
6. http://www.cquestions.com/2012/05/aptitude-questions-and-answers-in-c.html
7. http://blog.oureducation.in/c-c-aptitude-questions/
8. http://aptitude.students3k.com/technical-aptitude-questions-in-c-programming-pdf/
9. http://www.aucev.edu.in/placementcell/Materials/App1.pdf
10. http://techpreparation.com/aptitute-questions/c-cpp-aptitude-
question1.htm#.WTt3mDe1u2w
11. http://www.geeksforgeeks.org/c/
12. https://www.2braces.com/c-programming
View publication stats
View publication stats

More Related Content

PDF
Understanding a kernel oops and a kernel panic
PPTX
Linux Ethernet device driver
PDF
Let's talk about neovim
PPTX
MVNO契約でInitial Attachを活用しよう!
PDF
Diving into SWUpdate: adding new platform support in 30minutes with Yocto/OE !
PPTX
Berkeley Packet Filters
PDF
BeagleBone Black Booting Process
PDF
ACPI Debugging from Linux Kernel
Understanding a kernel oops and a kernel panic
Linux Ethernet device driver
Let's talk about neovim
MVNO契約でInitial Attachを活用しよう!
Diving into SWUpdate: adding new platform support in 30minutes with Yocto/OE !
Berkeley Packet Filters
BeagleBone Black Booting Process
ACPI Debugging from Linux Kernel

What's hot (20)

PDF
Video Drivers
PDF
マルチスレッド問題の特定と再現に頑張った話
PPTX
Yocto Project Open Source Build System and Collaboration Initiative
PDF
ARM Trusted FirmwareのBL31を単体で使う!
PDF
Basic unix commands
PDF
Programacion PRG Capitulo I
PPTX
Employee Management Process (Engineering Model)
PPTX
Linux Initialization Process (2)
PDF
Physical Memory Models.pdf
PDF
Linux Preempt-RT Internals
PDF
Linux field-update-2015
PDF
Angelo Compagnucci - Upgrading buildroot based devices with swupdate
PDF
Building your own embedded system with Yocto
PDF
writing self-modifying code and utilizing advanced assembly techniques
PDF
Wido den Hollander - 10 ways to break your Ceph cluster
PPTX
Debugging Modern C++ Application with Gdb
PDF
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
PDF
BeagleBone Black Bootloaders
PDF
ZynqMPのブートとパワーマネージメント : (ZynqMP Boot and Power Management)
PDF
Making Linux do Hard Real-time
Video Drivers
マルチスレッド問題の特定と再現に頑張った話
Yocto Project Open Source Build System and Collaboration Initiative
ARM Trusted FirmwareのBL31を単体で使う!
Basic unix commands
Programacion PRG Capitulo I
Employee Management Process (Engineering Model)
Linux Initialization Process (2)
Physical Memory Models.pdf
Linux Preempt-RT Internals
Linux field-update-2015
Angelo Compagnucci - Upgrading buildroot based devices with swupdate
Building your own embedded system with Yocto
writing self-modifying code and utilizing advanced assembly techniques
Wido den Hollander - 10 ways to break your Ceph cluster
Debugging Modern C++ Application with Gdb
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
BeagleBone Black Bootloaders
ZynqMPのブートとパワーマネージメント : (ZynqMP Boot and Power Management)
Making Linux do Hard Real-time
Ad

Similar to C aptitude book (20)

PPTX
About the c project in the detailed explaination
PDF
Memory as a Programming Concept in C and C Frantisek Franek
PDF
Fluent C: Principles, Practices, and Patterns 1st Edition Christopher Preschern
PDF
Memory as a Programming Concept in C and C Frantisek Franek
PDF
Computer Programming With C Kunal Pimparkhede
PDF
Memory as a Programming Concept in C and C Frantisek Franek
PDF
(Ebook) C Primer Plus Sixth Edition by Stephen Prata
PDF
C Programming_ Step By Step Beginner's To Experts _ Nho Vĩnh Share
PDF
Raspberry pi: Aprende raspberry pi con Linux por peter membrey y david hows.
PDF
Essential c
PDF
Essential c
PDF
Essential c notes singh projects
PDF
Essential c
PDF
Essential c
PDF
Memperf
PDF
C programming from problem analysis to program design 4th ed Edition D S Malik
PDF
Starting Out With C Early Objects 7th Edition 7th Edition Tony Gaddis
PDF
What Every Programmer Should Know About Memory
PDF
Let us c yashwant kanetkar(1)
PDF
INTRODUCTION TO C BOOK FOR REFERENCE.pdf
About the c project in the detailed explaination
Memory as a Programming Concept in C and C Frantisek Franek
Fluent C: Principles, Practices, and Patterns 1st Edition Christopher Preschern
Memory as a Programming Concept in C and C Frantisek Franek
Computer Programming With C Kunal Pimparkhede
Memory as a Programming Concept in C and C Frantisek Franek
(Ebook) C Primer Plus Sixth Edition by Stephen Prata
C Programming_ Step By Step Beginner's To Experts _ Nho Vĩnh Share
Raspberry pi: Aprende raspberry pi con Linux por peter membrey y david hows.
Essential c
Essential c
Essential c notes singh projects
Essential c
Essential c
Memperf
C programming from problem analysis to program design 4th ed Edition D S Malik
Starting Out With C Early Objects 7th Edition 7th Edition Tony Gaddis
What Every Programmer Should Know About Memory
Let us c yashwant kanetkar(1)
INTRODUCTION TO C BOOK FOR REFERENCE.pdf
Ad

Recently uploaded (20)

DOCX
How to Balance Clinical and Emotional Skills in Healthcare Assistant Courses....
PDF
Understanding Indicators TA Means technical indicators
PDF
VIT Accelerating Growth - September 2023.pdf
PDF
AAO Generalist notification 2025-2026-2027
PPTX
NURS1108_Lecture_7_-_Msuscular_2[1].pptx
PPT
3. Aggregate.ppt he is the main things of
PPTX
diabetes.pptxgtgthtgtgtgthyjiulp'][pp0ppp
PPTX
Q1 Review Spoke Centre _ Project समर्थ (1) (1).pptx
PPTX
OJT-Narrative-Presentation-Entrep-group.pptx_20250808_102837_0000.pptx
PPT
A-Guide-to-Developing-a-First-Course-in-the-NATEF-Model-NACAT-2017.ppt
PDF
How To Use Aged Linkedin Accounts To Grow Your Business.pdf
PDF
Lesson 1-IOM-Introduction to Management and Organizations.pdf
PDF
IMPORTANT QUES CLASS 12.pdf english by Tania sharma
PPTX
KAMAL HASSAN A VERY FAMOUS SOUTH INDIAN STAR.pptx
PPTX
obstetric instruments for final year mbbs students
PPTX
CDI 2.pptx special crime investigation with legal medicine
PPTX
MTVED - Trends in Food and Innovation.pptx
PPTX
Teaching Presentation on web Technology.
PPTX
FUTURE_VISIONS of me and my friends and dreams
PPTX
Creative-Nonfiction-Demystified.pptxhhhh
How to Balance Clinical and Emotional Skills in Healthcare Assistant Courses....
Understanding Indicators TA Means technical indicators
VIT Accelerating Growth - September 2023.pdf
AAO Generalist notification 2025-2026-2027
NURS1108_Lecture_7_-_Msuscular_2[1].pptx
3. Aggregate.ppt he is the main things of
diabetes.pptxgtgthtgtgtgthyjiulp'][pp0ppp
Q1 Review Spoke Centre _ Project समर्थ (1) (1).pptx
OJT-Narrative-Presentation-Entrep-group.pptx_20250808_102837_0000.pptx
A-Guide-to-Developing-a-First-Course-in-the-NATEF-Model-NACAT-2017.ppt
How To Use Aged Linkedin Accounts To Grow Your Business.pdf
Lesson 1-IOM-Introduction to Management and Organizations.pdf
IMPORTANT QUES CLASS 12.pdf english by Tania sharma
KAMAL HASSAN A VERY FAMOUS SOUTH INDIAN STAR.pptx
obstetric instruments for final year mbbs students
CDI 2.pptx special crime investigation with legal medicine
MTVED - Trends in Food and Innovation.pptx
Teaching Presentation on web Technology.
FUTURE_VISIONS of me and my friends and dreams
Creative-Nonfiction-Demystified.pptxhhhh

C aptitude book

  • 1. See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/318283734 A Comprehensive Guide to Crack C Aptitude Book · July 2017 CITATIONS 0 READS 3,044 2 authors: Some of the authors of this publication are also working on these related projects: Big Data : A text mining appraoch View project Soft Computing Model for Secure Auto Program Evaluator View project Poornima Naik CHHATRAPATI SHAHU INSTITUTE OF BUSINESS EDUCATION AND RESEARCH 108 PUBLICATIONS   92 CITATIONS    SEE PROFILE Kavita Oza Shivaji University, Kolhapur 65 PUBLICATIONS   60 CITATIONS    SEE PROFILE All content following this page was uploaded by Poornima Naik on 06 August 2018. The user has requested enhancement of the downloaded file.
  • 2. 1 A Comprehensive Guide to Crack C Aptitude By Dr.Poornima G. Naik Dr. Kavita S. Oza
  • 3. 2 Acknowledgements Many individuals share credit for this book‟s preparation. We extend our sincere thanks to Late Prof. A.D.Shinde, the Founder Director and Managing Trustee who has been a constant source of inspiration for us throughout our career. His support is really a driving force for us. Also, we would like to thank Dr.R.A.Shinde, Hon‟ble Secretary, CSIBER for his whole hearted support and continuous encouragement. We are grateful to Dr. M.M.Ali, Director CSIBER for his invaluable guidance. We take this opportunity to thank Dr.V.M.Hilage, Trustee Member, CSIBER, Dr. R.V.Kulkarni, H.O.D., Department of Computer Studies, Dr. R.R.Mudholkar, H.O.D., Department of Computer Science, Shivaji University for showing a keen interest in the matter of this book and extending all support facilities for the in-timely completion of this book. The material covered in this book is a systematic effort taken towards solving the various queries which we received from our students time to time, during our 15+ years tenure of teaching C at PG level. Last but not the least we thank all faculty members and non-teaching staff of department of computer studies, CSIBER, Kolhapur and department of Computer Science, Shivaji University, Kolhapur who have made contribution to this book either directly or indirectly. Dr. Poornima.G.Naik Dr. Kavita.S.Oza
  • 4. 3 Preface It gives us an immense pleasure to bring out a book on C concepts entitled „A Comprehensive Guide to Crack C Aptitude‟. This book is intended to serve those who have just started their tour of C. In this book, we have tried to stick to how, why and what of C rather than what a typical book of C offers. It serves as a reference material for those who have just started learning C and want to learn more of C internals. This book will help you to fill in the “gaps” by answering several „why‟ and „how to‟ questions in C. To understand the contents of this book a working knowledge of C and some basic concepts of C are required. This is not a complete reference to C, so some familiarity with C is essential. C is one of the oldest languages still having its prominent position in IT industry as it forms the basis for employment. It is learnt at School level to Postgraduate Level. In the boom of new languages emerging everyday still there are system level projects which are developed using C. In short C is become heart of computer science curricula and basic need for employability. In view of this, the proposed book aims at providing understanding of C language in a very simple way in the form of short aptitude questions and answers. C is one of the most popular language due to its versatile nature. C forms the basis for developing other programming languages like Java, it is also main source for developing Operating Systems like Unix/Linux. Every Company while recruiting the students first check their C language compatibility in the form of technical aptitude. So „C‟ becomes one of the stepping stone for the career of any student. The proposed book aims at providing the essentials of C language needed for employability. There are plenty of books available in the market on C language. These books are conservative in their approach in putting the core concepts. The proposed book is collection of technical questions collected from various IT companies used by them for recruitment. Authors through their experience of teaching C for more than 20 years found that there is no book in market which has a pin point focus only on technical concepts with needed theory for employability. There is a need for book which can give core C concepts in simple and interesting way with compact number of lines. Proposed book is handy reference to the candidates to
  • 5. 4 prepare for their technical aptitudes as well as interview. It will help students during their academics and even during their job hunting. Authors have used two approaches while writing the proposed book one is the questions which arises in students mind while studying C and second one is what an expert might expect from a new comer in the IT industry through their interactions with students and alumni working in IT industries. It‟s a collection of simple and trick questions with practically tested answers. Here 251 questions covering all core concepts of C language are discussed along with output and explanation for output. Language used is very simple with necessary comments wherever required. The book is the result of the questions collected from the various resources available on the internet whose references are provided at the end of the book. The authors have tried to provide more conceptual insight in to the c constructs unleashing the C internals both textually and visually on many occasions. The most threatening concepts of C such as pointers, unions can be easily comprehended from their memory representations. The same methodology is adopted whereas needed. As per authors knowledge there is no book in which C aptitude questions are made interesting and simple to motivate student learning. The current book is fully dedicated to hands on approach which will instill and create interest among students. As students are not much interested in long theory and find it uninteresting. Proposed book will make students active user as they have to execute the programs given and check the output. Book provides required pin point knowledge without any unnecessary theory associated to it so it helps in quick learning. Complex topics like pointers , abstract data types, variable declaration issues have been explained in very simple language in the form of questions and answers which makes it more interesting. C is interdisciplinary in the sense it is not only part of Computer science but is also part of other faculty curricula like MBA, Electronics, Mathematics, Bioinformatics, Biotechnology, Nano- science, Physics etc. so proposed book will prove to be a good reference book. It stimulates interactive learning due coverage of all concepts in the form of questions and answers. Dr. Poornima.G.Naik Dr. Kavita.S.Oza
  • 6. 5 Q. No. 1 Category : Storage Class What is the output generated on execution of the following C Program? #include<stdio.h> int main() { extern int i; Linker Error i = 100; printf("%dn", sizeof(i)); return 0; } Output : Explanation : The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred. Read More A storage class in C is used as a qualifier to modify data type declaration and to define the scope and life time of variables in a function. C defines four storage classes as shown below:  auto  register  static  extern The default storage class is auto.
  • 7. 6 Rules for auto storage class 1. It can only be used to define the variables with local scope. 2. Auto variables are stored on stack. 3. Memory management for auto variables is automatic. When the auto variable goes out of scope it is automatically deleted from memory. Consider the following C program #include <stdio.h> void main() { int month; auto int month; Line 6 } On execution of the above C program, the following error message is displayed. In the first declaration, the storage class by default is auto. Hence both declarations are identical. Rules for register storage class 1. The variables are stored in register instead of main memory. 2. The address operator cannot be used for accessing the location where register variable is stored. 3. The maximum size of the register storage class variable depends on the register size. 4. Depending on the implementation details and if the registers are used for storing some other data, register storage class may default to auto. Consider the following C program #include <stdio.h> #include <conio.h>
  • 8. 7 void main() { register int x=10; clrscr(); printf("%p",&x); Line 8 getch(); } On execution of the above program the following error message is displayed. Since x is a register variable, it does not conform to the memory location and hence & operator cannot be used for retrieving the address of a memory location. Re-execute the above program by change register storage class to auto storage class. The following output is generated. Rules for static storage class 1. Scope of a static variable is equal to the life time of a program. 2. Static variables retain their values between function calls. Rules for extern storage class 1. It is used for sharing data between different program files. 2. extern variable cannot be initialized. 3. When auto variables with the same name as extern variables exist, auto variables are given the preference over extern variables.
  • 9. 8 Consider the following C program #include <stdio.h> #include <conio.h> void main() { extern int x=10; Line 6 clrscr(); printf("%p",&x); getch(); } On execution of the above program the following error message is displayed. Create the following files. extern.c #include <stdio.h> int count=5; prg1.c #include <stdio.h> #include “extern.c” void main() { extern int count ; printf("%d",count); } On execution of prg1.c the following output is generated.
  • 10. 9 Change the prg1.c program as shown below: #include <stdio.h> #include “extern.c” void main() { int count=10 ; printf("%d",count); } On execution of prg1.c now, the following output is generated. Q. No. 2 Category : Pointer What is the output generated on execution of the following C Program? #include<stdio.h> int main() { char *s1; char far *s2; char huge *s3; printf("%d, %d, %dn", sizeof(s1), sizeof(s2), sizeof(s3)); return 0; } Output :
  • 11. 10 Explanation : Any pointer size is 2 bytes which includes only 16-bit offset. Offset address varies from 0000 to FFFF (in hexadecimal). Hence, char *c = 2 bytes. int *i = 2 bytes float *f = 2 bytes However, far and huge pointers consist of two parts 16-bit segment value + 16-bit offset value Hence, int | char | float far *s2; = 4 bytes. Similarly, int | char | float huge *s2; = 4 bytes. Read More There are three types of pointers:  Near  Far and  Huge If a pointer variable is not qualified with a type, then it defaults to near pointer. A near pointer can only point a 64-bit data segment or a segment no 8. Hence the limitation of near pointer is that we can only access 64kb of data at a time using near pointer and That is near pointer cannot access beyond the data segment like graphics video memory, text video memory etc. A far pointer can access memory outside current segment and is 32-bit in size. In the case of a far pointer, compiler allocates a segment register to store segment address, then another register to store offset within current segment.
  • 12. 11 Difference between huge and a far pointer. In case of far pointers, a segment is fixed whereas huge pointer can access multiple segments. In far pointer, the segment part cannot be modified, but in the case of huge pointer it can be. How to access segment and offset addresses of far/huge pointers? The header file dos.h, declares three macros which return the offset address and Segment address of a far pointer. The macro functions are: FP_OFF(): To get offset address of far pointer. FP_SEG(): To get segment address of far pointer. MK_FP(): To construct far pointer address from segment and offset addresses. Q. No. 3 Category : Abstract Data Types What is the output generated on execution of the following C Program? #include<stdio.h> int main() { struct emp { char name[20]; int age; float sal; }; struct emp e = {"Ashok"}; printf("%d, %fn", e.age, e.sal); return 0; } Output :
  • 13. 12 Explanation : Rule 1: When an automatic structure is uninitialized all the members will have garbage values. Rule 2: When an automatic structure is partially initialized remaining elements are initialized to 0(zero). Re-write the program as shown below: #include<stdio.h> #include <conio.h> int main() { struct emp { char name[20]; int age; float sal; }; struct emp e = {"Ashok"}; clrscr(); printf("%d, %fn", e.age, e.sal); getch(); return 0; } Execute the program, the following output is generated. Q. No. 4 Category : Operators What is the output of the following program? 1. #include<stdio.h> 2. int main() 3. { 4. int x = 10, y = 20, z = 5, i; 5. i = x < y < z;
  • 14. 13 6. printf("%dn", i); 7. return 0; 8. } Output : Explanation : The operator < has left associativity. Hence RHS of statement in line 5 is evaluated as shown below: Step 1 : Since x < y turns to be TRUE it is replaced by 1. Step 2 : 1 < z is evaluated and found to be TRUE. Hence 1 is assigned to i. Consider the following C program. 1. #include<stdio.h> 2. #include <conio.h> 3. int main() 4. { 5. int x = -1, y = 20, z = 0, i; 6. i = x < y < z; 7. clrscr(); 8. printf("%dn", i); 9. getch(); 10. return 0; 11. } On execution of the above program, the following output is generated. The statement i = x < y < z; is now evaluated as follows: Step 1 : Since x < y turns to be TRUE it is replaced by 1.
  • 15. 14 Step 2 : Since 1 < z turns to be FALSE it is replaced by 0 Hence 0 is assigned to i . Change the statement 6 to the one shown below and re-execute the program. i = x <(y < z); The following output is generated. Employing brackets the associativity is changed from left to right to right to left. In this case, the statement y < z evaluates to 0 (false) and the statement x < 0 evaluates to 1 (true) and hence 1 is assigned to i. Q. No. 5 Category : Arrays What is the output of the following program? #include<stdio.h> int main() { int a[5] = {2, 3}; printf("%d, %d, %dn", a[2], a[3], a[4]); return 0; } Output : When array is partially initialized, the remaining elements are automatically initialized to zero. Change the above program to the one shown below and re-execute the program.
  • 16. 15 #include<stdio.h> int main() { int a[5]; printf("%d, %d, %dn", a[2], a[3], a[4]); return 0; } The following output is generated. Explanation : As described in Q.3, when an array is uninitialized all the members will have garbage values and when the array is partially initialized remaining elements are initialized to 0(zero). Q. No. 6 Category : Abstract Data Types What is the output of the following program? #include<stdio.h> int main() { union a { int i; char ch[2]; }; union a u; u.ch[0] = 3; u.ch[1] = 2; printf("%d, %d, %dn", u.ch[0], u.ch[1], u.i); return 0; }
  • 17. 16 Output : Explanation : A union is a special abstract data type available in C which allows different data types to be stored in the same memory location. A union can be defined with many members, but only one member can contain a value at any given time. In union, different members share the same memory location. Unions provide an efficient way of using the same memory location for multiple-purpose. Consider the following C program : #include<stdio.h> #include <conio.h> int main() { struct s { int i; char ch[2]; }; union u { int i; char ch[2]; }; struct s a; union u b; clrscr(); printf("Size of struct - %d Size of union - %d", sizeof(a), sizeof(b)); getch(); return 0; } On execution of the above program the following output is generated.
  • 18. 17 Hence, Size of struct=Sum{sizeof(x) / x is a member of struct} Size of union=Max{sizeof(x) / x is a member of union} The following points about the struct and union should be noted:  In struct, the members are allocated memory in a contiguous memory locations whereas in union different members share the same memory location.  Size of a struct is sum of the sizes of its members whereas the size of a union is maximum size of its members.  In struct multiple members can be accessed simultaneously whereas in union only one member can be accessed at a time.  In struct any member can be altered without changing the values of remaining members whereas in union, altering any member will change the values of remaining members as the members share the same memory location.  When initializing a union, the initializer list must have only one member, which initializes the first member of the union and where as in struct the initializer list can contain all or few members. If the structure is initialized partially, the remaining elements default to zero. Consider the following program: 1. #include<stdio.h> 2. #include <conio.h> 3. int main() 4. { 5. union u 6. { 7. int i; 8. int j; 9. }; 10. union u b={10, 20};
  • 19. 18 11. clrscr(); 12. printf("%d %d", b.i, b.j); 13. getch(); 14. return 0; 15. } On execution of the above program, the following error message is displayed. Change the statement 10 to the one shown below and re-execute the program: union u b={10}; The following output is generated. Considering the above mentioned points, the above program prints the value of u.ch[0] = 3, u.ch[1] = 2 and it prints the value of u.i means the value of entire union size.
  • 20. 19 Q. No. 7 Category : Operators What is the output of the following program? #include<stdio.h> int main() { int a = 500, b = 100, c; if(!a >= 400) b = 300; c = 200; printf("b = %d c = %dn", b, c); return 0; } Output : Explanation : a b c 500 100 ! operator has higher precedence than >= operator. !a > 400 i.e. 0 > 400 evaluates to false. Hence statement b=300; is not executed. a b c 500 100 200 Q. No. 8 Category : Data Types What will be the output produced on execution of the following program? #include<stdio.h> int main() { unsigned int i = 65535; /* Assume 2 byte integer*/ while(i++ != 0) printf("%d",++i);
  • 21. 20 printf("n"); return 0; } Output : Infinite Loop Explanation : Consider the following program: #include<stdio.h> #include <conio.h> int main() { unsigned int i = 65535; /* Assume 2 byte integer*/ i++; clrscr(); printf("%d",i); getch(); return 0; } On execution of the above program, the following output is generated.
  • 22. 21 Hence the data type rotates on incrementing its value once it reaches a maximum value. Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535. Step 1:unsigned int i = 65535; Step 2: Inside loop, the condition is initially checked and then the value of i is incremented twice. Hence i always attains odd values and never becomes equal to zero. Hence the condition is always true, resulting into an infinite loop. Consider the following variant of the above program: #include<stdio.h> #include <conio.h> int main() { unsigned int i = 65535; /* Assume 2 byte integer*/ clrscr(); while(i++ != 0); printf("%d",++i); printf("n"); getch(); return 0; } On execution of the above program, the following output is generated. Observe the semi-colon next to the while loop. The following two statements  Comparison of value of i with 0 and  Incrementing the value of i
  • 23. 22 are repeatedly executed till the value of i becomes zero. The value of i is then incremented by 1 (i becomes 1). Finally, the value of i is incremented again (i becomes 2), before printing its value. Q. No. 9 Category : Data Types What will be the output produced on execution of the following program? int main() { short int i = 0; for(i<=5 && i>=-1; ++i; i>0) printf("%u,", i); return 0; } Output :1..65535 Explanation : In the fop loop for(i<=5 && i>=-1; ++i; i>0)  the expression i<=5&& i>=-1 initializes for loop  the expression ++i is the loop condition and  the expression i>0 is the increment expression.
  • 24. 23 In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to 1. Loop condition always gets evaluated to true. Also at this point it increases i by one. An increment expression i>0 has no effect on value of i. So for loop get executed till the limit of integer (i.e. 65535) Q. No. 10 Category : Built-in Functions What will be the output produced on execution of the following program? #include<stdio.h> int main() { char ch; if(ch = printf("")) printf("It mattersn"); else printf("It doesn't mattern"); return 0; } Output : Explanation : printf() function returns the number of characters printed on the console. Hence, the statement printf(“”) returns 0, which is assigned to ch and the if() condition evaluates to false and the statement in the else part is executed. Consider the following program: #include<stdio.h> int main() { clrscr(); printf("%dn",printf("It matters")); getch();
  • 25. 24 return 0; } On execution of the above program, the following output is generated. Hence printf() returns the number of characters printed on the console. The program given in Q.No. 10 is executed in the steps given below: Step 1: if(ch = printf("")) here printf() does not print anything, so it returns '0'(zero). Step 2: if(ch = 0) here variable ch has the value '0'(zero). Step 3: if(0) Hence the if condition is not satisfied. So it prints the else statements. Hence the output is "It doesn't matters". Q. No. 11 Category :Data Types What will be the output produced on execution of the following program? #include<stdio.h> int main() { float a = 0.7; if(0.7 > a) printf("Hin"); else printf("Hellon"); return 0; } Output : Explanation : if(0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is
  • 26. 25 greater than the float variable a. Hence if condition is satisfied and it prints 'Hi' Example: #include<stdio.h> int main() { float a=0.7; printf("%.10f %.10fn",0.7, a); return 0; } On execution of the above program, the following output is generated. Hence always double literal is greater than floating point literal of same value. Q. No. 12 Category : Operators What will be the output produced on execution of the following program? #include<stdio.h> int main() { int a=0, b=1, c=3; *((a) ?&b :&a) = a ? b : c; printf("%d, %d, %dn", a, b, c); return 0; } Output : Explanation : Step 1: int a=0, b=1, c=3; here variable a, b, and c are declared as integer type and initialized to 0, 1, 3 respectively.
  • 27. 26 Step 2: *((a) ?&b :&a) = a ? b : c; The right side of the expression(a?b:c) becomes (0?1:3). Hence it returns the value '3'. The left side of the expression *((a) ?&b :&a) becomes *((0) ? &b :&a). Hence this contains the address of the variable a *(&a). Step 3: *((a) ?&b :&a) = a ? b : c; Finally this statement becomes *(&a)=3. Hence the variable a has the value '3'. Step 4: printf("%d, %d, %dn", a, b, c); It prints "3, 1, 3". Q. No. 13 Category : Operators What will be the output produced on execution of the following program? #include<stdio.h> int main() { int x = 10, y = 20; if(!(!x) && x) printf("x = %dn", x); else printf("y = %dn", y); return 0; } Output : Explanation : The logical not operator takes precedence and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it reverses the value of the expression. Step 1: if(!(!x) && x)
  • 28. 27 Step 2: if(!(!10) && 10) Step 3: if(!(0) && 10) Step 4: if(1 && 10) Step 5: if (TRUE) hence the if condition is satisfied. Hence it prints x = 10. Q. No. 14 Category :Control Statements What will be the output produced on execution of the following program? #include<stdio.h> int main() { int i=4; switch(i) { default: printf("This is defaultn"); case 1: printf("This is case 1n"); break; case 2: printf("This is case 2n"); break; case 3: printf("This is case 3n"); } return 0; } Output : Explanation : In the very beginning of switch-case statement default statement is encountered. So, it prints "This is default".
  • 29. 28 In default statement there is no break; statement is included. So it prints the case 1 statement. "This is case 1". Then the break; statement is encountered. Hence the program exits from the switch-case block. Modify the above program as shown below and re-execute it. #include<stdio.h> int main() { int i=4; switch(i) { default: printf("This is defaultn"); case 1: printf("This is case 1n"); break; case 2: printf("This is case 2n"); break; case 3: printf("This is case 3n"); case 4: printf("This is case 4n"); } return 0; } The following output is generated. Hence the rule of thumb is the matching case statement is always executed till the break statement is encountered, irrespective of where it is placed and if the matching case statement is not found, then default block is executed till the break statement is encountered.
  • 30. 29 Q. No. 15 Category : Control Statements How many times “SIBER" gets printed? 1. #include<stdio.h> 2. int main() 3. { 4. int x; 5. for(x=-1; x<=10; x++) 6. { 7. if(x < 5) continue; 8. else break; 9. printf(“SIBER"); 10. } 11. return 0; 12. } Output : Explanation : No Output. In the execution of the above C program, statement 9 is never reached. In the fop loop, the value of x is continuously incremented till x becomes 5, after which the control will break outside the for loop and main returns with the exit code of 0. Q. No. 16 Category :Functions What will be the output produced on execution of the following program? #include<stdio.h> int main() { printf(“SIBER"); main(); return 0; }
  • 31. 30 Output : Explanation : A call stack or function stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing. A stack overflow occurs when too much memory is used on the call stack. Here function main() is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error. Q. No. 17 Category : Pointers What would be the equivalent pointer expression for referring the array element a[i][j][k][l] Output : *(*(*(*(a+i)+j)+k)+l) Explanation : Consider the situation in the case of 1-D integer array with the following declaration. int a[4]={1, 2, 3, 4}; int *p; p=a;
  • 32. 31 The memory allocation for p and a is as depicted in Figure. Using a, the different elements of the array can be accessed as shown below: a Address of first element of array. *a Value of first element of array. a+1 Address of second element of array. *(a+1) Value of second element of array. . . . . Continuing in this manner, (a+i) refers to the address of (i+1)th element of an array and *(a+i) refers to the value of (i+1)th element of an array. Using p, the element at the position 3 (with index starting at 0) can be accessed as shown below: *(a+3). Consider the following C Program: #include <stdio.h> #include <conio.h> void main() { int a[4]={1, 2, 3, 4}; int *p; p=a; clrscr(); printf("%d",*(a+3)); getch(); } On execution of the above C Program, the following output is generated.
  • 33. 32 In order to extend the logic to 2-D array, consider the 2 Dimensional integer array as shown below: int **a = {1, 2, 3, 4}; int **p; p = new int*[4]; // dynamic `array (size 4) of pointers to int` for (int i = 0; i < 4; ++i) { p[i] = new int[10]; // each i-th pointer is now pointing to dynamic array (size 10) of actual int values } To free the memory For one dimensional array, // need to use the delete[] operator because we used the new[] operator delete[] p; //free memory pointed by p; For 2d Array, // need to use the delete[] operator because we used the new[] operator for(int i = 0; i < 4; ++i) { delete[] p[i];//deletes an inner array of integer; } delete[] p; //delete pointer holding array of pointers;
  • 34. 33 Variable Memory Content Description a 3000 Base address of array a (a+1) 3002 Incrementing integer pointer advances it by 2 bytes. *(a+1) 2000 Content of the memory location addressed by a+1 *(a+1)+1 2002 Incrementing the pointer *(a+1) by 1. *(*(a+1)+1) 2 Content of the memory location addressed by *(a+1)+1 Consider the following C Program: #include <stdio.h> #include <conio.h> void main() { int **p=new int*[2]; for(int i=0;i<2;i++) p[i]=new int[2]; for (i=0;i<2;i++) { for (int j=0;j<2;j++) {
  • 35. 34 *(*(p+i)+j)=i+j; } } clrscr(); printf("%d",*(*(p+1)+1)); getch(); } Q. No. 18 Category : Abstract Data Types What will be the output of the program? #include<stdio.h> #include<stdlib.h> union employee { char name[15]; int age; float salary; }; const union employee e1; int main() { strcpy(e1.name, "K"); printf("%s %d %f", e1.name, e1.age, e1.salary); return 0; } Output : Explanation : By definition, different members of union share the same memory location. The memory allocation for different members of union employee is ash shown below: e1.name 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B 1B e1.age 1B 1B e1.salary 1B 1B 1B 1B The member name requires 15 bytes of storage, whereas age and salary require 2 bytes and 4
  • 36. 35 bytes, respectively. The ASCII code corresponding to the letter „K‟ is 75 which is displayed by e1.age member of the union variable. Q. No. 19 Category : Program Execution In which stage the following code #include<stdio.h> gets replaced by the contents of the file stdio.h Output : During preprocessing Explanation : The preprocessor replaces the line #include <stdio.h> with the system header file of that name. More precisely, the entire text of the file 'stdio.h' replaces the #include directive. Q. No. 20 Category : Variable Declaration Issues What does the following declaration mean? int (*ptr)[10]; Output : ptr is a pointer to an array of 10 integers Explanation : Consider the following two declarations : int (*ptr)[10]; and int *ptr[10]; The former declaration implies ptr is a pointer to an array of 10 integers (stores the address of base address of array) while in the second declaration ptr is an array of 10 integer pointers (stores the addresses of 10 integers). For improving readability the second declaration can be re-written
  • 37. 36 as int* ptr[10]; What is difference between variable ptr in the following two declarations? int (*ptr)[10] and int ptr[10]; In the former case ptr is pointer variable which can store the base address of array of ten integers, while in the latter case ptr is a constant pointer to an array to 10 integers. Consider the following program : #include<stdio.h> #include<conio.h> int main() { int a[]={1,2,3,4}; int (*ptr1)[10]; int ptr2[10]; ptr1=&a; clrscr(); printf("%p %p %pn",a,&a,&a[0]); printf("%p",ptr1); getch(); return 0; } On execution of the above program, the following output is generated: In the above program, ptr1 is a pointer to an integer array. Ptr1 is initialized with array a. Hence a and ptr1 refer to the same entity. Q. No. 21 Category : Pointers What will be output if you compile and execute the following c code?
  • 38. 37 #include<stdio.h> #include<conio.h> int main() { int num , *ptr1 ,*ptr2 ; ptr1 = &num ; ptr2 = ptr1 + 2 ; clrscr(); printf("%d",ptr2 - ptr1); getch(); return(0); } Output : Explanation : Subtraction of two pointers is equal to the number of elements between the two memory locations. In the above program, both ptr1 and ptr2 are integer pointers. ptr1 is initialized with the address of integer variable num. ptr1 is incremented by 2 which advances it by 4 bytes. Assume that the value stored in ptr1 is 1000. Then, ptr2 = 1004. By definition, ptr2 – ptr1 = (1004-1000)/sizeof(int) = 4/2 = 2 Hence the program generates the output 2. Q. No. 22 Category : Variable Declaration Issues Declare the following statement? "An array of three pointers to chars". Output : char *ptr[3];
  • 39. 38 Explanation : In the above declaration, if *ptr is placed in a parentheses as shown below, char (*ptr)[3]; then, ptr becomes pointer to the array of three characters. Q. No. 23 Category : Variable Declaration Issues What does the following declaration signify? int *ptr[30]; Output : ptr is a array of 30 pointers to integers. Explanation : In the above declaration, if *ptr is placed in a parentheses as shown below, int (*ptr)[30]; then, ptr becomes pointer to the array of thirty integers. Consider the following program: #include<stdio.h> #include<conio.h> int main() { int a[]={1,2,3,4}; int (*ptr1)[4]; clrscr(); printf("%p %pn",a,&a[0]); ptr1=a; printf("%pn",*ptr1); printf("%d",**ptr1); getch(); return 0; } On execution of the above program the following output is generated:
  • 40. 39 Q. No. 24 Category : Variable Declaration Issues Declare the following statement? "A pointer to an array of three chars". Output : char (*ptr)[3]; Explanation : Apply chain rule to declare the given statement. A pointer to an (*ptr) array of 3 (*ptr)[3] chars char (*ptr)[3] Q. No. 25 Category : Variable Declaration Issues What does the following declaration signify? char *arr[10]; Output : arr is a array of 10 character pointers Explanation : In order to improve readability, the given statement can be re-written as: char*arr[10]; Clearly, now the data type of arr is array of char* of ten elements.
  • 41. 40 Q. No. 26 Category : Variable Declaration Issues What does the following declaration signify? int (*pf)(); Output : pf is a pointer to a function which returns int and does not accept any arguments Explanation : Consider the following function: int test() { int a=10; a++; return a; } test() is a function which does not accept any parameters and returns int and hence the address of test() function can be stored in pf as shown below: pf = &test; Now, the test() function can be invoked as shown below: int result = (*pf)(); Consider the following program: #include<stdio.h> #include<conio.h> int test() { int a=10; a++; return a; } int main() { int (*pf)();
  • 42. 41 int result; clrscr(); pf = &test; result=(*pf)(); printf("Result : %d",result); getch(); return 0; } On execution of the above program, the following output is generated: Q. No. 27 Category : Variable Declaration Issues Declare the following statement? "A pointer to a function which receives an int pointer and returns float pointer". Output : float *(*ptr)(int*) Explanation : Consider the following function: float* test(int* x) { int a=*x; float b=(float)(a/2); return *b; } test() is a function which accepts a single parameter of type integer pointer and returns a floating point pointer and hence the address of test() function can be stored in pf as shown below: pf = &test; Now, the test() function can be invoked as shown below: int x=10;
  • 43. 42 float* y = (*pf)(&x;); Consider the following program: #include<stdio.h> #include<conio.h> float* test(int* x) { int a=*x; float* b=(float *)malloc(4); *b=(float)(a/2); printf("%f",*b); return b; } int main() { float* (*pf)(int*); int x=10; float* result; clrscr(); pf = &test; result = (*pf)(&x); printf("nResult : %f",*result); getch(); return 0; } On execution of the above program the following output is generated: Q. No. 28 Category : Variable Declaration Issues Declare the following statement? "A pointer to a function which receives nothing and returns nothing".
  • 44. 43 Output : void (*ptr)() Explanation : Ptr is a pointer to a function which receives nothing and returns nothing. Q. No. 29 Category : Variable Declaration Issues What does the following declaration signify? void (*cmp)(); Output : cmp is a pointer to a function with no arguments which returns void Explanation : Apply chain rule, in order to extract the meaning of the above declaration. 3 void (*cmp)(); 1 2 cmp is a pointer to a function with no arguments which returns void. Q. No. 30 Category : Pointers Predict the output generated on execution of the following program: #include<stdio.h> #include<conio.h> void main() { int const * p=5; printf("%d",++(*p)); Line 6 } Output : Compiler error: Cannot modify a constant value.
  • 45. 44 Explanation : p is a pointer to a "constant integer". Any attempt to change the value of the "constant integer" will generate a compiler error. What is difference between constant pointer and pointer to a constant? Consider the declaration of ptr as shown below: int x=10; int* const ptr=&x; ptr, as declared above is a constant pointer. Hence the pointer cannot be dereferenced and made to point another integer. The following statement would result in error. int y=20; ptr=&y; However, x can be modified. At present the value of (*ptr) is 10 and after the statement x=x+10; the value of (*ptr) becomes 20 as shown in the following program. Example to illustrate Constant Pointer #include<stdio.h> #include<conio.h> int main() { int x=10; int y=20; int* const ptr=&x; clrscr(); ptr=&y; Line 10
  • 46. 45 getch(); return 0; } On execution of the above program, the following compiler error is generated. Modify the above program as shown below and re-execute it. #include<stdio.h> #include<conio.h> int main() { int x=10; int y=20; int* const ptr=&x; clrscr(); //ptr=&y; *ptr=*ptr+10; printf("x = %d *ptr = %d",x,*ptr); getch(); return 0; } On execution of the above program, the following output is generated. Example to illustrate Pointer to a Constant Consider the following program: #include<stdio.h> #include<conio.h> int main() { const int x=10;
  • 47. 46 const int y=20; const int* ptr=&x; clrscr(); ptr=&y; *ptr=*ptr+10; Line 11 printf("x = %d *ptr = %d",x,*ptr); getch(); return 0; } On execution of the above program, the following compiler error is generated. Comment the line shown in bold and re-execute the program. The following output is generated. Difference Between Constant Pointer and Pointer to a Constant Constant Pointer – Pointer when declared is initialized with the address of a variable, then onwards it cannot be dereferenced and made to point another variable using address operator. Pointer to a Constant – A pointer is initialized with the address of a constant. The value of a referenced entity cannot be modified through indirection operator. The following C program clearly demonstrates the difference between constant pointer and pointer to a constant. #include <stdio.h> #include <conio.h> void main() { int x=10; int y=20;
  • 48. 47 //Pointer to a constant const int* p1=&y; //Constant Pointer int* const p2=&x; printf("%d",++(*p2)); getch(); } Q. No. 31 Category : Pointers Predict the output generated on execution of the following program: void main() { char s[ ]="man"; int i; for(i=0;s[ i];i++) printf("n%c%c%c%c",s[i],*(s+i),*(i+s),i[s]); } Output : mmmm aaaa nnnn Explanation : s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i]. Q. No. 32 Category : Data Types Predict the output generated on execution of the following program: main() { float me = 1.1; double you = 1.1; if (me==you) printf (“Equal");
  • 49. 48 else printf (“Not Equal"); } Output : Explanation : For floating point numbers (float, double, long double)the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 8 bytes. So float stores 0.9 with less precision than long double. Rule of Thumb: Never compare or at-least be cautious when using floating point numbers with relational operators (== ,>,<, <=, >=,!= ) . Q. No. 33 Category : Storage Classes Predict the output generated on execution of the following program: void main() { static int var = 5; printf ("%d ",var--); if (var) main(); } Output :
  • 50. 49 Explanation : When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. main is also treated like any other ordinary function, which can be called recursively. Re-execute the application by removing static qualifier. void main() { int var = 5; printf ("%d ",var--); if (var) main(); } Since the static qualifier is removed in the variable declaration var, each time main() function is invoked, var is initialized to 5 and the condition shown in bold is never satisfied. Hence main() function is continuously invoked till stack becomes full, after which the application terminates. Q. No. 34 Category : Pointers Predict the output generated on execution of the following program: void main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf (" %d ",*c); ++q; } for(j=0;j<5;j++) { printf(" %d ",*p); ++p; } }
  • 51. 50 Output : Explanation : After the execution of first for loop After the execution of second for loop Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c, and the value stored in the memory location pointed by c is printed, the value 2 will be printed 5 times. In second for loop, p itself is incremented and the value stored in the memory location pointed by p is printed. So the values 2 3 4 6 5 will be printed. Q. No. 35 Category : Operators Predict the output or error(s) for the following: main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }
  • 52. 51 Output : Explanation : Logical operations always give a result of 1 or 0. And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression „i++&& j++ && k++‟ is executed first. The result of this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for „0 || 0‟ combination for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1. Since all the operators in the expression are post increment operators, the expression m is evaluated first and then the variables i, j, k and l are incremented. Q. No. 36 Category :Arrays What will be the output of the program ? #include<stdio.h> int main() { int arr[1]={10}; printf("%dn", 0[arr]); return 0; } Output : Explanation : Step 1: int arr[1]={10}; The variable arr is declared as an integer array with size '2' and it's first element is initialized to value '10' (i.e. arr[0]=10)
  • 53. 52 Step 2: printf("%dn", 0[arr]); It prints the first element value of the variable arr. 0[arr] is same as arr[0]. Hence the output of the program is 10. Q. No. 37 Category : Data Types What will be the output of the program ? #include<stdio.h> #include <conio.h> void main() { char *p; clrscr(); printf("%d %d ",sizeof(*p),sizeof(p)); getch(); } Output : Explanation : The sizeof() operator gives the number of bytes taken by its operand. p is a character pointer, which needs two bytes for storing the address of a character it points. Hence sizeof (p) gives a value of 2 whereas *p is of type char which needs one byte for storing the character assigned to it. Hence sizeof (p) gives 2. Q. No. 38 Category : Control Statements What will be the output of the following program ? #include<stdio.h> #include <conio.h> void main() { int i=3;
  • 54. 53 clrscr(); switch(i) { default: printf ("zero"); case 1: printf("one"); break; case 2: printf("two"); break; case 3: printf("three"); break; } getch(); } Output : Explanation : The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match Q. No. 39 Category : Functions What will be the output of the following program? #include<stdio.h> #include <conio.h> void main() { char string[]="Hello World"; clrscr(); display(string); getch(); }
  • 55. 54 void display(char *string) Line 12 { printf("%s",string); } Output : Explanation : In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs. Q. No. 40 Category : Operators What will be the output of the following program? #include<stdio.h> #include <conio.h> void main() { int c=- -2; clrscr(); printf("c=%d",c); getch(); } Output :
  • 56. 55 Explanation : Here unary minus (or negation) operator is used twice. Same Math rules applies, i.e. .minus* minus= plus. Note: However you cannot give like --2. Because – operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable. Q. No. 41 Category : Operators What will be the output of the following program? #include<stdio.h> #include <conio.h> #define int char void main() { int i=65; clrscr(); printf("sizeof(i)=%d",sizeof(i)); getch(); } Output : Explanation : Since the #define macro replaces the string int by char, on preprocessing the source code becomes #include<stdio.h> #include <conio.h> void main()
  • 57. 56 { char i=65; clrscr(); printf("sizeof(i)=%d",sizeof(i)); getch(); } and sizeof() char data type equal to 1 is displayed. Q. No. 42 Category : Operators What will be the output of the following program ? #include<stdio.h> #include <conio.h> void main() { int i=10; i=!i>14; clrscr(); printf("i=%d",i); getch(); } Output : Explanation : In the expression !i>14 , NOT (!) operator has more precedence than „ >‟ symbol. ! is a unary logical operator. !i(!10) is 0 (not of true is false).0>14 is false(zero). Modify the above program as shown below (the statement modified is shown in bold) and re- execute it. #include<stdio.h> #include <conio.h>
  • 58. 57 void main() { int i=10; i=!(i>14); clrscr(); printf("i=%d",i); getch(); } On execution of the above program, the following output is generated. Now, the expression i=!(i>14); is evaluated as follow: i = !(0) since 10 > 14 is false = 1 Hence the program generates the output of 1. Q. No. 43 Category : Operators What will be the output of the following program ? #include<stdio.h> #include <conio.h> void main() { clrscr(); printf("%x",-1<<4); getch(); } Output :
  • 59. 58 Explanation : -1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value. Q. No. 44 Category : Pointers What will be the output of the following program ? #include<stdio.h> #include <conio.h> void main() { char s[]={'a','b','c','n','c','0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; clrscr(); printf("%d",++*p + ++*str1-32); getch(); } Output : Explanation : As shown in the above Figure, p is pointing to character 'n'. str1 is pointing to character 'a' . p is pointing to 'n' and is incremented by one. The ASCII value of 'n' is 10, which is then incremented to 11. Hence the value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by and it becomes 'b'. ASCII value of 'b' is 98.
  • 60. 59 Now performing (11 + 98 – 32), we get 77. Hence the output generated is 77. Q. No. 45 Category : Arrays and Pointers What will be the output of the following program ? #include<stdio.h> #include <conio.h> void main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; clrscr(); printf("%d----%d",*p,*q); getch(); } Output : Explanation : p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third2D(which you have not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array. Q. No. 46 Category : Abstract Data Types What will be the output of the following program ? #include<stdio.h> #include <conio.h> void main()
  • 61. 60 { struct xx { int x=3; char name[]="hello"; Line 9 }; struct xx *s; printf("%d",s->x); printf("%s",s->name); } Output : Compiler Error as shown below: Explanation : Rule : In structures, you should not initialize variables in declaration. Q. No. 47 Category : Abstract Data Types What will be the output of the following program? #include<stdio.h> #include <conio.h> void main() { struct xx { int x; struct yy { char s; struct xx *p; Error }; struct yy *q; }; }
  • 62. 61 Output : Explanation : The structure yy is nested within structure xx. Hence, the elements of yy are to be accessed through the instance of structure xx, which needs an instance of yy to be known. If the instance is created after defining the structure the compiler will not know about the instance relative to xx. Hence for nested structure yy, you have to declare member. Q. No. 48 Category : Control Characters What will be the output of the following program? #include<stdio.h> #include <conio.h> void main() { clrscr(); printf("nab"); printf("bsi"); printf("rha"); getch(); } Output :
  • 63. 62 Explanation : n - newline b - backspace r – carriage return After first printf() statement, the characters printed are shown below, where _ indicates the position of the cursor. a b _ After second printf() statement(backspace, followed by characters s and i), the characters printed are shown below: a s i _ Carriage return means to return to the beginning of the current line without advancing downward. After third printf() statement (carriage return, followed by characters h and a), the characters printed are shown below: h a i _ Hence the output generated is “hai” Q. No. 49 Category : Operators What will be the output of the following program ? #include <stdio.h> #include <conio.h> void main() { int i=5; clrscr(); printf("%d %d %d %d %d",i++,i--,++i,--i,i); getch(); }
  • 64. 63 Output : Explanation : The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result. Step 1: Pushing arguments onto the stack from left to right. i --i ++i i-- i++ Step 2 : Evaluation of arguments 4 5 5 4 5 Step 3 : Popping out elements from stack 45545 Hence the output is 45545. Alternatively, the expression printf("%d %d %d %d %d",i++,i--,++i,--i,i); is evaluated as follows : The arguments of printf() function are evaluated from right to left
  • 65. 64 Evaluation i++ i-- ++i --i i 4 5 5 4 5 Display Hence the program outputs 45545 Q. No. 50 Category : Macros What will be the output of the following program ? #include <stdio.h> #include <conio.h> #define square(x) x*x void main() { int i; i = 64/square(4); clrscr(); printf("%d",i); getch(); } Output : Explanation : The macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 =64 On preprocessing step, the pseudo code generated is shown below:
  • 66. 65 void main() { int i; i = 64/4*4; clrscr(); printf("%d",i); getch(); } The expression shown in bold is evaluated as: i = (64/4)*4 = 16*4 = 64 Hence the output generated is 64. Modify the program as shown below ( the statement changed is shown in bold) and re-execute it. #include <stdio.h> #include <conio.h> #define square(x) (x*x) void main() { int i; i = 64/square(4); clrscr(); printf("%d",i); getch(); } On execution of the above program, the following output is generated. Q. No. 51 Category : Operators What will be the output of the following program?
  • 67. 66 #include <stdio.h> #include <conio.h> void main() { char *p=“siber",*p1; p1=p; clrscr(); while(*p!='0') ++*p++; printf("%s %s",p,p1); getch(); } Output : Explanation : ++*p++ will be parsed in the given order. The expression ++*p++ is evaluated as shown in the following steps: 1. *p that is value at the location currently pointed by p will be taken. ++*p, the retrieved value will be incremented. 2. The location will be incremented that is p++ will be executed Hence, in the while loop initial value pointed by p is „s‟, which is changed to „t‟ by executing ++*p and pointer moves to point, „i‟ which is similarly changed to „j‟ and so on. Thus, we obtain value in p becomes “tjcfs” and since p reaches „0‟ and p1 points to p thus p1doesnot print
  • 68. 67 anything. Hence after the execution of while loop, the situation is as depicted below: Q. No. 52 Category : Control Statements What will be the output of the following program? #include <stdio.h> #include <conio.h> void main() { int i=3; clrscr(); switch(i) { default: printf ("zero"); case 1: printf("one"); break; case 2: printf("two"); break; case 3: printf("three"); break; } getch(); } Output :
  • 69. 68 Explanation : The default case can be placed anywhere inside the loop. It is executed only when all other cases do not match. Q. No. 53 Category : Preprocessors What will be the output of the following program ? #include <stdio.h> #include <conio.h> #define a 10 void main() { #define a 50 clrscr(); printf("%d",a); getch(); } Output : Explanation : The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken. Consider the following program: #include <stdio.h> #include <conio.h> #define a 10
  • 70. 69 void main() { clrscr(); printf("%dn",a); #define a 50 printf("%d",a); getch(); } On preprocessing, the main function is modified as shown below: void main() { clrscr(); printf("%dn",10); printf("%d",50); getch(); } The lines affected during preprocessing step are shown in bold. On execution of the above program, the following output is generated: Q. No. 54 Category : Preprocesors What will be the output of the following program? #include <stdio.h> #include <conio.h> #define clrscr() 100 void main() { clrscr(); printf("%dn",clrscr()); }
  • 71. 70 Output : Explanation : Preprocessor executes as a separate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs. The input program to compiler looks like this : void main() { 100; printf("%dn",100); } Note:100; is an executable statement but with no action. So it doesn't give any problem Q. No. 55 Category : Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { clrscr(); printf("%p",main); getch(); }
  • 72. 71 Output : Explanation : Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers. Q. No. 56 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=10; i=!i>14; clrscr(); printf("i=%d",i); getch(); } Output : Explanation : In the expression !i>14 , NOT (!) operator has more precedence than „ >‟ symbol. ! is a unary
  • 73. 72 logical operator. !i(!10) is 0 (not of true is false).0>14 is false (zero). Q. No. 57 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { char far *farther,*farthest; clrscr(); printf("%d..%d",sizeof(farther),sizeof(farthest)); getch(); } Output : Explanation : The second pointer is of near type and not a far pointer. Modify the above program as shown below and re-execute it. #include <stdio.h> #include <conio.h> void main() { char far* farther,farthest; clrscr(); printf("%d..%d",sizeof(farther),sizeof(farthest)); getch(); } The statement altered is shown in bold. On execution of the above program, the following output
  • 74. 73 is generated. Now, farther is of type far pointer to a char and farthest is of type char. Q. No. 58 Category : Variable Declaration Issues Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=400,j=300; clrscr(); printf("%d..%d"); getch(); } Output : Explanation : printf takes the values of the first two assignments of the program. Any number of printf's may be given. All of them take only the first two values. If more number of assignments are given in the program, then printf will take garbage values. The numbers printed are in reverse order which is attributed to the nature of printf() function evaluation. The arguments of printf() function are pushed on to the stack and are then output by
  • 75. 74 popping the elements from the stack. Modify the above program as shown below and re-execute it. The statement altered is shown in bold. #include <stdio.h> #include <conio.h> void main() { int i=400,j=300; clrscr(); printf("%d..%d",i,j); getch(); } On execution of the above program, the following output is generated. Q. No. 59 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { char *p; p="Hello"; clrscr(); printf("%cn",*&*p); getch(); }
  • 76. 75 Output : Explanation : * is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H. Alternatively, *&*p = *p since *&=1 Q. No. 60 Category : Control Statements Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; Error i++; } } fun() { here: printf("PP"); }
  • 77. 76 Output : Explanation : Labels have function scope, in other words the scope of the labels is limited to functions. The label 'here' is available in function fun(). Hence it is not visible in function main. Q. No. 61 Category : Arrays Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; Error for (i=0;i<=4;i++) printf("%s",names[i]); } Output : Explanation : Array name is a constant pointer. So it cannot be modified. Hence array names cannot appear on
  • 78. 77 the left hand side of the expression. Q. No. 62 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=5; clrscr(); printf("%d",i++ + ++i); getch(); } Output : Explanation : The expression is evaluated from right to left. The value of i is pre-incremented and becomes 6. Then the same value is used in the first operand, resulting in 12 as the value of the expression. After the evaluation of the expression, the value of i is post-incremented and becomes 7. Modify the above program to add the following statement at the end. printf("n%d",i); On re-execution of the program, the following output is generated.
  • 79. 78 Change the expression to the one shown below and re-execute the program : printf("%d",i++ + i++ + ++i); 7 + 6 + 6 = 19 The following output is generated: Change the expression to the one shown below and re-execute the program : printf("%d",++i + i++ + ++i); 8 + 6 + 6 = 20 The following output is generated: Q. No. 63 Category : Control Statements Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); Line 11 break; } }
  • 80. 79 Output : Explanation : The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error). Note : Enumerated types can be used in case statements. Q. No. 64 Category : Basic Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i; clrscr(); printf("%d",scanf("%d",&i)); // value 10 is given as input here getch(); } Output : Explanation : scanf returns number of items successfully read and not I/0. Here 10 is given as input which
  • 81. 80 should have been scanned successfully. So number of items read is 1. Q. No. 65 Category : Preprocessors Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> #define f(g,g2) g##g2 void main() { int var12=100; clrscr(); printf("%d",f(var,12)); getch(); } Output : Explanation : ## is string concatenation operator. Hence during the preprocessing phase, f(var,12) is evaluated as var12 and printf() statement in the above program becomes, printf(“%d”, var12); which displays 100. Q. No. 66 Category : Basic Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h>
  • 82. 81 void main() { int i=0; clrscr(); for(;i++;printf("%d",i)) ; printf("%d",i); getch(); } Output : Explanation : before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop). Q. No. 67 Category : Variable Declaration Issues Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { printf("%d", out); Line 6 } int out=100; Output :
  • 83. 82 Explanation : The rule is that a variable is available for use from the point of declaration. Even though a is a global variable, it is not available for main. Hence an error Q. No. 68 Category : Storage Classes Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { extern out; clrscr(); printf("%d", out); getch(); } int out=100; Output : Explanation : This is the correct way of writing the previous program. Since the global variables are not available at the time of compilation, extern statement informs the compiler that „out‟ variable will be available to the program at the time of execution. Q. No. 69 Category : Functions Predict the output or error(s) for the following:
  • 84. 83 #include <stdio.h> #include <conio.h> void main() { show(); } void show() Line 9 { printf("I'm the greatest"); } Output : Explanation : When the compiler sees the function show() it doesn't know anything about it. So the default return type (i.e., int) is assumed. But when compiler sees the actual definition of show() mismatch occurs since it is declared as void. Hence the error. The solutions are as follows: 1. declare void show() in main() . 2. define show() before main(). 3. declare extern void show() before the use of show(). Add the following statement before main() function and re-execute the application. void show(); On re-execution of the application, the following output is generated.
  • 85. 84 Q. No. 70 Category :Arrays and Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; clrscr(); printf(“%u %u %u %d n”,a,*a,**a,***a); printf(“%u %u %u %d n”,a+1,*a+1,**a+1,***a+1); getch(); } Output : Explanation : The 12 elements of the 3D array are organized in contiguous memory locations as shown below: 2 4 7 8 3 4 2 2 2 3 3 4 Consider the following program : #include <stdio.h> #include <conio.h> void main() { int a[2][2]={11,22,33,44};
  • 86. 85 clrscr(); printf("a : %un",a); printf("a[0] : %un",a[0]); printf("a[1] : %un",a[1]); printf("a[0][0] : %d t**a : %d tt*a[0] : %dn",a[0][0], **a, *a[0]); printf("a[0][1] : %d t*(*(a+0)+1) : %d t*(a[0]+1) : %dn",a[0][1], *(*(a+0)+1), *(a[0]+1)); printf("a[1][0] : %d t*(*(a+1)+0) : %d t*a[1] : %dn",a[1][0], *(*(a+1)+0), *a[1]); printf("a[1][1] : %d t*(*(a+1)+1) : %d t*(a[1]+1) : %dn",a[1][1], *(*(a+1)+1), *(a[1]+1)); getch(); } On execution of the above program, the following output is generated. Consider the following program: #include <stdio.h> #include <conio.h> void main() { int a[2][2][2]={11,22,33,44,55,66,77,88}; clrscr(); printf("a[0][0][0] : %d t*(a[0][0]+0) : %d t*(*(a[0]+0)+0) ta[0] : %dt*(*(*(a+0)+0)+0 : %dnn",a[0][0][0], *(a[0][0]+0), *(*(a[0]+0)+0),*(*(*(a+0)+0)+0)); printf("a[0][0][1] : %d t*(a[0][0]+1) : %d t*(*(a[0]+0)+1) ta[0] : %dt*(*(*(a+0)+0)+1 : %dnn",a[0][0][1], *(a[0][0]+1), *(*(a[0]+0)+1), *(*(*(a+0)+0)+1)); printf("a[0][1][0] : %d t*(a[0][1]+0) : %d t*(*(a[0]+1)+0) ta[0] : %dt*(*(*(a+0)+1)+0 : %dnn",a[0][1][0], *(a[0][1]+0), *(*(a[0]+1)+0), *(*(*(a+0)+1)+0)); printf("a[0][1][1] : %d t*(a[0][1]+1) : %d t*(*(a[0]+1)+1) ta[0] : %dt*(*(*(a+0)+1)+1 : %dnn",a[0][1][1], *(a[0][1]+1), *(*(a[0]+1)+1), *(*(*(a+0)+1)+1));
  • 87. 86 printf("a[1][0][0] : %d t*(a[1][0]+0) : %d t*(*(a[1]+0)+0) ta[0] : %dt*(*(*(a+1)+0)+0 : %dnn",a[1][0][0], *(a[1][0]+0), *(*(a[1]+0)+0), *(*(*(a+1)+0)+0)); printf("a[1][0][1] : %d t*(a[1][0]+1) : %d t*(*(a[1]+0)+1) ta[0] : %dt*(*(*(a+1)+0)+1 : %dnn",a[1][0][1], *(a[1][0]+1), *(*(a[1]+0)+1), *(*(*(a+1)+0)+1)); printf("a[1][1][0] : %d t*(a[1][1]+0) : %d t*(*(a[1]+1)+0) ta[0] : %dt*(*(*(a+1)+1)+0 : %dnn",a[1][1][0], *(a[1][1]+0), *(*(a[1]+1)+0), *(*(*(a+1)+1)+0)); printf("a[1][1][1] : %d t*(a[1][1]+1) : %d t*(*(a[1]+1)+1) ta[0] : %dt*(*(*(a+1)+1)+1 : %dnn",a[1][1][1], *(a[1][1]+1), *(*(a[1]+1)+1), *(*(*(a+1)+1)+1)); getch(); } On execution of the above program, the following output is generated. Q. No. 71 Category :Arrays Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h>
  • 88. 87 void main( ) { int a[ ] = {10,20,30,40,50},j,*p; clrscr(); for(j=0; j<5; j++) { printf(“%d” ,*a); a++; Line 11 } p = a; for(j=0; j<5; j++) { printf(“%d ” ,*p); p++; } getch(); } Output : Explanation : Error is in line with statement a++. The operand must be an lvalue and may be of any of scalar type for the any operator, array name only when subscripted is an lvalue. Simply array name is a non-modifiable lvalue. Modify the above program as shown below (modified statement is shown in bold) and re-execute the application. #include <stdio.h> #include <conio.h> void main( ) {
  • 89. 88 int a[ ] = {10,20,30,40,50},j,*p; clrscr(); for(j=0; j<5; j++) { printf("%d " ,*(a+j)); } printf("n"); p = a; for(j=0; j<5; j++) { printf("%d " ,*p); p++; } getch(); } On execution of the application, the following output is generated. a cannot be incremented or decremented (since a is a constant pointer), the correct way of accessing the elements of an array using pointer notation is *(a+i) Q. No. 72 Category :Arrays and Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; clrscr(); printf(“n %d %d %d”, ptr-p, *ptr-a, **ptr);
  • 90. 89 *ptr++; printf(“n %d %d %d”, ptr-p, *ptr-a, **ptr); *++ptr; printf(“n %d %d %d”, ptr-p, *ptr-a, **ptr); ++*ptr; printf(“n %d %d %d”, ptr-p, *ptr-a, **ptr); getch(); } Output : Explanation : Subtraction of pointers gives total number of objects between them. For computing the difference between two pointers ptr1 and ptr2, employ the following formula: (ptr2 - ptr1) / Size of Data Type Numerically Subtraction ( ptr2-ptr1 ) differs by 4 As both are Integers they are numerically Differed by 4 and technically by 2 objects Suppose both pointers of float the they will be differed numerically by 8 and technically by 2 objects Consider the below statement and refer the following table – int num = ptr2 - ptr1; and If Two Pointers are of Following Data Type Numerical Difference Technical Difference Integer 2 1 Float 4 1 Character 1 1
  • 91. 90 Q. No. 73 Category :Arrays and Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; clrscr(); printf(“%s”,*--*++p + 3); getch(); } Output : Explanation : In this problem, we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p holds the initial value of ptr, i.e. p = s+3. The next statement increments value in p by 1, thus now value of p = s+2. In the printf statement the expression is evaluated *++p gets value s+1 then the pre decrement is executed and we get s+1 – 1 = s the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is „ck‟.
  • 92. 91 Hence, *p = 1012 **p = s[2]. The printf() statement, contains the following statement, *--*++p + 3 which is evaluated as, (*--*++p)+3 ++p = 1006 *++p = s+1 --*++p =s --*++p + 3 = b l a c k 0 Hence printf() statement will print from c till 0 character is encountered i.e. ck. Hence the program generates the output ck. Q. No. 74 Category :Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i, n; char *x = “girl”; n = strlen(x);
  • 93. 92 *x = x[n]; clrscr(); for(i=0; i<4;i++) { printf(“%sn”,x); x++; } getch(); } Output : Explanation : Here a string (a pointer to char) is initialized with a value “girl”. The strlen() function returns the length of the string, thus n has a value 4. The next statement assigns value at the nth location („0‟) to the first location. Now the string becomes “0irl” . Now the printf statement prints the string after each iteration it increments it starting position. Loop starts from 0 to 4. The first time x[0] = „0‟ hence it prints nothing and pointer value is incremented. The second time it prints from x[1] i.e. “irl” and the third time it prints “rl” and the last time it prints “l” and the loop terminates. Q. No. 75 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=-1; +i; printf("i = %d, +i = %d n",i,+i); getch(); }
  • 94. 93 Output : Explanation : Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator). Q. No. 76 Category :Program Execution What are the files which are automatically opened when a C program is executed? Output : stdin, stdout, stderr (standard input, standard output, standard error). Q. No. 77 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { char *cptr,c; void *vptr,v; Line 7 c=10; v=0; Line 8 cptr=&c; vptr=&v; getch(); printf("%c %v",c,v); clrscr(); }
  • 95. 94 Output : Explanation : You can create a variable of type void * but not of type void, since void is an empty type. In the second line you are creating variable vptr of type void * and v of type void hence an error. The correct way of writing the above program is : #include <stdio.h> #include <conio.h> void main() { char *cptr,c; void *vptr; c=65; cptr=&c; vptr=&c; clrscr(); printf("%c %c",*cptr, *(char*)vptr); getch(); } On execution of the above program, the following output is generated. In the above program, both *cptr and (char*)vptr point to character „A‟ with ASCII value of 65. Since vptr is a void pointer it requires explicit type casting to char *. Q. No. 78 Category : Operators Predict the output or error(s) for the following:
  • 96. 95 #include <stdio.h> #include <conio.h> void main() { char *str1="abcd"; char str2[]="abcd"; clrscr(); printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); getch(); } Output : Explanation : In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5(including the '0' termination character). The third sizeof is similar to the second one. Q. No. 79 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int k=1; clrscr(); printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); getch(); }
  • 97. 96 Output : Explanation : When two strings are placed together (or separated by white-space) they are concatenated (this is called as "stringization" operation). So the string is as if it is given as "%d==1 is %s". The conditional operator( ?: ) evaluates to "TRUE". The expression printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); is evaluated as follows: printf(“%d==1 is %s,1,TRUE);, which displays 1==1 is TRUE, hence the output generated by the program. Q. No. 80 Category : Preprocessors Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> #define max 5 #define int arr1[max] void main() { typedef char arr2[max]; arr1 list={0,1,2,3,4}; Line 10 arr2 name="name"; clrscr(); printf("%d %s",list[0],name); getch(); }
  • 98. 97 Output : Explanation : arr2 is declared of type array of size 5 of characters. So it can be used to declare the variable name of the type arr2. But it is not the case of arr1. Hence an error. Rule of Thumb: #define directives are used for textual replacement whereas typedefs are used for declaring new types. Q. No. 81 Category : Storage Classes Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> int i=10; void main() { extern int i; { int i=20; { const volatile unsigned i=30; clrscr(); printf("%dn",i); } printf("%dn",i); } printf("%dn",i); getch(); }
  • 99. 98 Output : Explanation : '{' introduces new block and thus new scope. In the inner most block i is declared as, const volatile unsigned which is a valid declaration. i is assumed of type int. So printf prints 30. In the next block, i has value 20 and so printf prints 20. In the outermost block, i is declared as extern, so no storage space is allocated for it. After compilation is over the linker resolves it to global variable i (since it is the only variable visible there). So it prints i's value as 10. Remove extern keyword and re-execute the application. The following output is generated. i becomes uninitialized local variable. Hence last printf statements prints garbage. Remove the following statement extern int i; and re-execute the application. The following output is generated. The last printf() statement now uses global variable i. Q. No. 82 Category : Variab le Scope and Life Time Predict the output or error(s) for the following:
  • 100. 99 #include <stdio.h> #include <conio.h> main() { int *j; { int i=10; j=&i; } clrscr(); printf("%d",*j); getch(); } Output : Explanation : The variable i is a block level variable and the visibility is inside that block only. But the lifetime of i is lifetime of the function so it lives up to the exit of main function. Since the variable i is still allocated space, *j prints the value stored in i since j points i. The rule of thumb is that i can be accessed through a pointer and not by its name. To demonstrate this, change the printf() statement to the one shown below and re-execute the program. printf("%d",i); On execution of the program now, the following output is generated. i is not reachable to the printf() statement but is accessible through pointer variable j.
  • 101. 100 Q. No. 83 Category : Arrays and Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; clrscr(); printf("%d..%d",*p,*q); getch(); } Output : Explanation : p=&a[2][2][2] you declare only two 2D arrays. but you are trying to access the third2D (which you have not declared) it will print garbage values. In the statement, *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a and if you print *q, it will print first element of 3D array. Q. No. 84 Category : Storage Classes Predict the output or error(s) for the following:
  • 102. 101 #include <stdio.h> #include <conio.h> void main() { register i=5; char j[]= "hello"; clrscr(); printf("%s %d",j,i); getch(); } Output : Explanation : If you declare i as register, compiler will treat it as ordinary integer and it can store an integer value. i variable may be stored either in register or in memory. Q. No. 85 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=5,j=6,z; clrscr(); printf("%d",i+++j); getch(); }
  • 103. 102 Output : Explanation : There are two possibilities of the expression i+++j being evaluated. Case 1 : (i++) + j In this case the value of the expression is 11. Case 2 : i + (++j) In this case the value of the expression is 12. As seen from the output, Case 1 is the correct way of evaluating the expression. Hence, the expression i+++j is treated as (i++ + j). Q. No. 86 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=-1,j=-1,k=0,l=2,m,n; m=i++&&j++&k++||l++; clrscr(); printf("%d %d %d %d %dn",i,j,k,l,m); n=++i&&++j&++k||++l; printf("%d %d %d %d %d",i,j,k,l,n); getch(); }
  • 104. 103 Output : Explanation : In the expression, m=(i++&&j++&k++)||(l++); In the above expression,& is a bitwise AND operator and&& is a logical AND operator. & has higher precedence compared to &&. Hence the above expression is identical to m=(i++&& (j++&k++) )||(l++); since i and j are true, third condition is evaluated and result is false, since k is zero. Since the result in the first parenthesis is false, the condition in the second parenthesis is also evaluated and the result of the complex condition becomes true. Hence 1 is assigned to m. After the evaluation of the expression, the values of i, j, k and l are incremented. Hence, i=0, j=0, k=1, l=3 and m=1. In the expression, n=++i&&++j&++k||++l; since i and j are true, third condition is evaluated and result is false, since k is zero. Since the result in the first parenthesis is false, the condition in the second parenthesis is also evaluated and the result of the complex condition becomes true. Hence 1 is assigned to m. After the evaluation of the expression, the values of i, j, k and l are incremented. Hence, i=0, j=0, k=1, l=3 and m=1.
  • 105. 104 i j k l m n Initial Values -1 -1 0 2 - - On execution of m=i++&&j++&&k++||l++; 0 0 1 3 1 - On execution of n=++i&&++j&&++k||++l; 1 1 2 4 1 1 Replace the bitwise & operator with the logical && operators and re-execute the program. m=i++&&j++&&k++||l++; n=++i&&++j&&++k||++l; On re-execution of the program, the following output is generated. The following table depicts the values of different variables on execution of different statements. i j k l m n Initial Values -1 -1 0 2 - - On execution of m=i++&&j++&&k++||l++; 0 0 1 3 1 - On execution of n=++i&&++j&&++k||++l; 1 1 2 3 1 1 Q. No. 87 Category : Data Types Predict the output or error(s) for the following:
  • 106. 105 #include <stdio.h> #include <conio.h> main() { signed char i=0; for(;i>=0;i++); clrscr(); printf("%d",i); getch(); } Output : Explanation : Signed char variable assumes the value from -128 to 127. In the for loop once the value of i becomes 127, incrementing it further assigns the value -128 to i and the condition becomes false (observe the semicolon at the end of for statement). Hence i=-128 gets printed in the subsequent statement. Q. No. 88 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> main() { char c='A'; c=c|32; clrscr(); printf("%c",c); getch(); }
  • 107. 106 Output : Explanation : Binary representation of A is 1 0 0 0 0 0 1 and binary representation of 32 is 1 0 0 0 0 0. Performing the logical OR operation on the two operands, we get, 1 1 0 0 0 0 1 whose decimal equivalent is 97 an ASCII code representation of a, hence the output generated. Q. No. 89 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> main() { int i=0,j=1; clrscr(); printf("%d %d %dn",i++ && ++j,i,j); printf("%d",i); getch(); } Output : Explanation : The expression printf("%d %d %dn",i++ && ++j,i,j); is evaluated as follows:
  • 108. 107 The value of i is 0 (false). Since && operator employs short-circuit evaluation, the second operand is not evaluated since the value of first operand is false. Hence j is not pre-incremented and its value remains to be 1. After the first printf() statement, the value of i is incremented to 1 as is evident from second printf() statement. Add the following printf() statement at the end and re-execute the program. printf(“n%d”,j); On execution of the program, the following output is generated. Change the value of i to 1 in the first statement as shown below int i=1,j=1; and re-execute the program. The following output is generated. Change the order of arguments in printf() statement as shown below printf("%d %d %dn",i,j,i++ && ++j); and re-execute the application. The following output is generated.
  • 109. 108 This reveals that in the printf() statement, the operands are evaluated from right to left. Q. No. 90 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int x=011|0x10; printf("x=%d",x); getch(); } Output : Explanation : The prefix 0 indicates octal representation while 0x indicates hexadecimal representation. Hence binary representation of octal 011 is (decimal 9) 1 0 0 1 and the binary representation of hexadecimal 0x10 (decimal 16) is 1 0 0 0 0. Performing the binary OR operation on them, we get, 1 1 0 0 1 whose decimal equivalent is 25, hence the output generated. Q. No. 91 Category : Operators What will be output if you compile and execute the following c code? #include <stdio.h> #include <conio.h>
  • 110. 109 void main() { int i,j=0,k=7; i=j++&k++; clrscr(); printf("j : %d k : %d i : %dn",j,k,i); getch(); } Output : Explanation : Short-circuit evaluation does not apply to bitwise AND operator (&). Hence in the expression i=j++&k++; the second statement, k++ is evaluated even if the first condition is false. The value assigned to i is the bitwise AND operation between j and k, after that the values of j and k are incremented. Since j is 0, the result of expression is 0 which is assigned to i, then j and k are incremented to 1 and 8, respectively. Q. No. 92 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=1; clrscr(); i=(i<<=1%2); printf("%d",i); getch(); }
  • 111. 110 Output : Explanation : The expression i=(i<<=1%2); is evaluated as follows: 1 % 2 evaluates to 1. Left shifting bits of an integer n times is equivalent to multiplying it by 2n . Hence, left shifting bits of i by 1 bit is equivalent to multiplying it by 2. Hence 2 is assigned to i and the same is output in the succeeding printf() statement. Q. No. 93 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=1; clrscr(); i=i+2*i++; printf("%d",i); getch(); } Output :
  • 112. 111 Explanation : The expression i=i+2*i++; is evaluated as follows : Step 1 : The expression i + 2*i is evaluated (=3) Step 2 : The value of 3 is assigned to i. Step 3 : The value of i is incremented by 1. (=4) Hence the output of 4 is generated. Q. No. 94 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int x=20,y=35; clrscr(); x=y+++x++; y=++y+ ++x; printf("%d %d",x,y); getch(); } Output : Explanation : The expression x=y+++x++; is evaluated as follows: Step 1 : The expression is evaluated as follows:
  • 113. 112 x=(y++)+(x++); x is assigned the value of (x + y) (=55) Step 2 : x and y values are incremented to 56 and 36, respectively. Step 3 : The expression y=++y+ ++x; is evaluated as follows: y=(++y)+ (++x); Step 4 : x and y variables are pre-incremented and assigned the values of 57 and 37, respectively. Step 5 : y is assigned the value of (x+y) (=94). Step 6 : x and y get the values of 57 and 94, respectively. Hence the output of 57 and 94 is generated. Q. No. 95 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int a=0xff,b; b=a<<4>>12; clrscr(); b?printf("Sandeep"):printf("Ashok"); getch(); } Output : Explanation : The expression b=a<<4>>12;is evaluated as follows:
  • 114. 113 Step 1 : The expression b=a<<4>>12; is evaluated as b=(a<<4)>>12; Step 2 : Initial value of a is 0000ff, hence a << 4 = 000ff0 (left shift by 4-bits or one hexadecimal digit) Step 3 : The expression 000ff0 >> 12 bits evaluates to 0 (right shift by 12 bits or 3 hexadecimal digits) Step 4 : The value of b is 0 (false). Hence the expression b?printf("Sandeep"):printf("Ashok"); prints “Ashok”. Hence the output generated. Q. No. 96 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { unsigned int x=-10; int y=10; clrscr(); if (y <= x) printf("He is goodn"); if (y == (x=-10)) printf("He is bettern"); if ((int)x==y) printf("He is bestn"); getch(); } Output : Explanation : Sign bit is 1 for –ve number and 0 for +ve number. Hence -10 is represented as 1000 0000 0000 1010
  • 115. 114 Q. No. 97 Category : Control Statements Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int x=0; clrscr(); for(; ;) { if (x++ == 4) break; continue; } printf("x=%dn",x); getch(); } Output : Explanation : In the for loop, the value of x is continuously incremented till it becomes 4. Once the condition x == 4 is satisfied, the control breaks out of the for loop but before that the value of x is incremented to 5. Hence the output x=5 is generated. Q. No. 98 Category : Control Statements Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int score=4; switch(score) {
  • 116. 115 default : ; case 3 : score+=5; if (score==8) { score++; if (score==9) break; score *= 2; } score -= 4; break; case 8 : score += 5; break; } printf("SCORE = %dn",score); getch(); } Output : Explanation : The steps in the execution of the application are given below: Step 1 : Since case 4 is not present, the control enters the default case in switch statement. Step 2 : Since default case does not contain a break statement, the next case, case 3 is entered. Step 3 : score variable is incremented by 5 and its value becomes 9. Step 4 : The if condition evaluates to false and value of score variable is decremented by 4 and its value becomes 5. Step 5 : The control breaks out of the switch statement and the value of score (equal to 5) is printed. Hence the output SCORE=5 is generated.
  • 117. 116 Q. No. 99 Category : Control Statements Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i; i=2; i++; clrscr(); if (i=4) { printf("i=4"); } else { printf("i=3"); } getch(); } Output : Explanation : = is an assignment operator while == is a comparison operator. In the if condition, if (i=4) 4 is assigned to i and the condition evaluates to true and i=4 is displayed on console. Q. No. 100 Category : Recursion Predict the output or error(s) for the following:
  • 118. 117 #include <stdio.h> #include <conio.h> void foo(int nVal) { int i; if (nVal) { nVal--; foo(nVal); for(int i=0;i<nVal;i++) printf("*"); printf("n"); } } int main() { foo(4); getch(); return 0; } Output : Explanation : The first statement in main() invokes a call foo(4). In the function foo(), the statements are executed in the following order. Step 1 : if condition returns true. Step 2 : Value of nVal is decremented by 1, and the function is recursively invoked with argument 3, as foo(3) on pushing the context onto the stack. Step 3 : Recursively invoking the function foo() continues till the terminating condition if (nval) returns false (till nval becomes 0).
  • 119. 118 Step 4 : Output is generated by popping the context from the stack. The output generated in various method calls is depicted in the following table: nVal foo(nVal) Statements printed 1 foo(1) 2 foo(2) * 3 foo(3) ** 4 foo(4) *** Q. No. 101 Category : Abstract Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> union A { char a; char b; }; int main() { union A a; a.a=65; clrscr(); printf("%c %c",a.a,a.b); getch(); return 0; } Output : Explanation : In union, different members share the same memory location. 65, which is the ASCII code corresponding to the alphabet „A‟ is stored in the member a of union variable a, which is also
  • 120. 119 shared by the variable b. Hence both a.a and a.b contain the value 65 (ASCII code of A). Hence the output A A is generated by the preceding printf statement. Q. No. 102 Category : Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> int fun(int i) { return(i++); } void main() { int i=fun(10); clrscr(); printf("%dn",i); getch(); } Output : Explanation : return(i++) statement will first return the value of i and then increments i. i.e. 10 will be returned. Q. No. 103 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { char *p; int *q;
  • 121. 120 long *r; p=q=r=0; p++; q++; r++; clrscr(); printf("%p...%p...%p",p,q,r); getch(); } Output : Explanation : ++ operator, when applied to pointers increments address according to their corresponding data- types. Thus, character pointer is incremented by 1, integer pointer is incremented by 2 and long pointer is incremented by 4. Q. No. 104 Category : Arrays and Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> int one_d[]={1,2,3}; void main() { int *ptr; ptr=one_d; ptr+=3; clrscr(); printf("%d",*ptr); getch(); }
  • 122. 121 Output : Explanation : ptr points to the first element 1 of one_d array as shown below: ++ operator, when applied to pointers increments address according to their corresponding data- types. Thus, integer pointer is incremented by 3. After ptr+=3, ptr pointer is pointing to out of the array range of one_d. Change the statement ptr+=3 to ptr+=2 (so that ptr points to the last element of the array) and re- execute the application. The following output is generated. Q. No. 105 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i =0;
  • 123. 122 j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); } Output : Explanation : The value of i is 0. Since this information is enough to determine the truth value of the boolean expression. So the statement following the if statement is not executed. The values of i and j remain unchanged and get printed. Q. No. 106 Category : Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; }
  • 124. 123 Output : Explanation : Normally the return value from the function is through the information from the accumulator. Here _AH is the pseudo global variable denoting the accumulator. Hence, the value of the accumulator is set 1000 so the function returns value 1000. Q. No. 107 Category : Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> int i; void main() { int t; clrscr(); for ( t=4;scanf("%d",&i)-t;printf("%dn",i)) printf("%d--",t--); getch(); } // If the inputs are 0,1,2,3 find the o/p
  • 125. 124 Output : Explanation : In the given program, scanf() statement is present in the test condition and the output generated is the concatenation of body of the for loop and the increment condition of the for loop. The following table depicts the statements executed during the different iterations of fop loop. Iteration Input t Test Condition scanf("%d",&i)-t o/p printf("%d--",t--); Increment condition printf("%dn",i) + o/p 1 0 4 -4 (true) 4-- 4--0 2 1 3 -2(true) 3-- 3--1 3 2 2 0 (false) 2-- 2--2 Hence the given program generates the output. 4-0 3-1 2-2 In the third iteration the condition becomes false and the for loop terminates. Q. No. 108 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h>
  • 126. 125 void main() { int a= 0; int b = 20; char x =1; char y =10; clrscr(); if(a,b,x,y) printf("hello"); getch(); } Output : Explanation : The comma operator has associativity from left to right. Only the rightmost value is returned and the other values are evaluated and ignored. Thus the value of last variable y is returned to check in if. Since it is a non zero value if becomes true so, "hello" will be printed. Change the statement char y =10; to char y =0; re-execute the application. The if condition evaluates to false and no output is generated as shown below: Q. No. 109 Category : Data Types Predict the output or error(s) for the following: #include <stdio.h>
  • 127. 126 #include <conio.h> void main() { unsigned int i; clrscr(); for(i=1;i>-2;i--) printf("c aptitude"); getch(); } Output : No Output. Explanation : i is an unsigned integer. It is compared with a signed value. Since the both types doesn't match, signed is promoted to unsigned value. The unsigned equivalent of -2 is a huge value so condition becomes false and control comes out of the loop. Q. No. 110 Category : Storage Classes Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { static int i=5; if(--i) { main(); printf("%d ",i); } }
  • 128. 127 Output : Explanation : The variable "i" is declared as static, hence memory for i will be allocated for only once, as it encounters the statement. The function main() will be called recursively unless i becomes equal to 0, and since main() is recursively called, so the value of static i i.e., 0 will be printed every time the control is returned. In the above program, if the static keyword is removed the program will enter into an infinite loop since each time main() function is invoked the value of I is initialized to 5. Q. No. 111 Category : Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { unsigned giveit=-1; int gotit; clrscr(); printf("%u ",++giveit); printf("%u n",gotit=--giveit); getch(); } Output :
  • 129. 128 Explanation : The range of unsigned int is 0 to 65535. Hence decrementing the unsigned int variable initialized to 0 (minimum value) will result in rollback to maximum value of 65535. Similarly, incrementing the unsigned int variable initialized to 65535 (maximum value) will result in rollback to minimum value of 0 as illustrated in the following program: #include <stdio.h> #include <conio.h> void main() { unsigned x=0; clrscr(); printf("%un",--x); getch(); } On execution of the above program, the following output is generated. Change the above program to the one shown below and re-execute it. #include <stdio.h> #include <conio.h> void main() { unsigned x=65535; clrscr(); printf("%un",++x); getch(); } On execution of the above program, the following output is generated.
  • 130. 129 Q. No. 112 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { void *v; int integer=2; int *i=&integer; v=i; Error printf("%d",(int*)*v); } Output : Explanation : void pointer is a generic pointer type. No pointer arithmetic can be done on it. void pointers are normally used for, 1. Passing generic pointers to functions and returning such pointers. 2. As an intermediate pointer type. 3. Used when the exact pointer type will be known at a later point of time.
  • 131. 130 Q. No. 113 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=i++, j=j++, k=k++; printf(“%d%d%d”,i,j,k); } Output : Explanation : An identifier is available to use in program code from the point of its declaration. So expressions such as i = i++ are valid statements. The i, j and k are automatic variables and so they contain some garbage value. Garbage in is garbage out (GIGO). Q. No. 114 Category : Storage Classes Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { static int i=++i, j=++j, k=++k; clrscr(); printf(“%d %d %d”,i,j,k); getch(); }
  • 132. 131 Output : Explanation : Since static variables are initialized to zero by default. Remove the static qualifier and re-execute the program, the following output is generated. int i=++i, j=++j, k=++k; As seen from the output, the variables i, j and k are initialized to garbage values. Q. No. 115 Category : Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { clrscr(); while(1) { if(printf("%d",printf("%d"))) break; else continue; } getch(); }
  • 133. 132 Output : Explanation : The inner printf executes first to print some garbage value. The printf returns no of characters printed and this value also cannot be predicted. Still the outer printf prints something and so returns a non-zero value. So it encounters the break statement and comes out of the while statement. Q. No. 116 Category : Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { unsigned int i=10; clrscr(); while(i-->=0) printf("%u ",i); getch(); } Output :
  • 134. 133 Explanation : Since i is an unsigned integer it can never become negative. So the expression i-- >=0 will always be true, resulting in an infinite loop. Q. No. 117 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; clrscr(); printf("%d %d ",z,x); getch(); } output : Explanation : The value of y%2 is 0. This value is assigned to x. The condition reduces to if (x) or in other words if(0) and so z goes uninitialized. Thumb Rule: Check all control paths to write bug free code. Q. No. 118 Category : Arrays and Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h>
  • 135. 134 void main() { int a[10]; clrscr(); printf("%d",*a+1-*a+3); getch(); } Output : Explanation : *a and -*a cancels out. The result is as simple as 1 + 3 = 4 ! Q. No. 119 Category : Macros Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> #define prod(a,b) a*b void main() { int x=3,y=4; clrscr(); printf("%d",prod(x+2,y-1)); getch(); } Output :
  • 136. 135 Explanation : The macro expands and evaluates to as: x+2*y-1 = x+(2*y)-1 = 10 Q. No. 120 Category : Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { unsigned int i=65000; while(i++!=0); printf("%d",i); } Output : Explanation : Note the semicolon after the while statement. When the value of i becomes 0 it comes out of while loop. Due to post-increment on i the value of i while printing is 1. Q. No. 121 Category : Operators Predict the output or error(s) for the following: #include <stdlib.h> #include <stdio.h>
  • 137. 136 void main() { int i=1; +i; clrscr(); while (i--!=0); printf("%d",i); getch(); } Output : Explanation : Unary + is the only dummy operator in C. So it has no effect on the expression and now the while loop is, while (--i!=0) which is false and so breaks out of while loop. The value –1 is printed due to the post-decrement operator. The condition while (i--!=0); is evaluated in the following steps: Step 1 : The expression i != 0 is evaluated. Step 2 : i is decremented. The condition is first evaluated and then I is decremented. Observe the semicolon next to while loop. In the first iteration the condition (1 != 0) evaluates to true and in the second iteration the condition evaluates to false (0 != 0). Hence the control comes out of the loop, before that the value of i is decremented to -1 and the same is printed by the next printf() statement. Hence the program generates the output -1. Change the post-decrement operator to pre-decrement operator as shown in the following program and re-execute the program. #include <stdlib.h> #include <stdio.h>
  • 138. 137 void main() { int i=1; +i; clrscr(); while (--i!=0); printf("%d",i); getch(); } On execution of the above program, the following output is generated. The condition while (--i!=0); is evaluated in the following steps: Step 1 : i is decremented. Step 2 : The expression i != 0 is evaluated. i is decremented first and then the condition is evaluated. In the first iteration of the while loop the condition (0!=0) becomes false. Hence the control comes out of the for loop and prints 0. Hence the program now generates the output 0. Q. No. 122 Category : Abstract Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { float f=5,g=10; enum{i=10,j=20,k=50};
  • 139. 138 clrscr(); printf("%dn",++k); Line 9 printf("%fn",f<<2); Line 10 printf("%lfn",f%g); Line 11 printf("%lfn",fmod(f,g)); getch(); } Output : Explanation : Enumeration constants cannot be modified, so you cannot apply ++. Bit-wise operators and % operators cannot be applied on float values. fmod() is to find the modulus values for floats as % operator is for ints. Q. No. 123 Category : Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void pascal f1(int i,int j,int k) { printf("%d %d %d",i, j, k); } void cdecl f2(int i,int j,int k) { printf("%d %d %d",i, j, k); } void main() { int i=10; clrscr(); f1(i++,i++,i++);
  • 140. 139 printf(" %dn",i); i=10; f2(i++,i++,i++); printf(" %d",i); getch(); } Output : Explanation : Pascal argument passing mechanism forces the arguments to be called from left to right. cdecl is the normal C argument passing mechanism where the arguments are passed from right to left. The function f1 uses pascal mechanism of parameter invocation. Hence the function call f1(i++, i++,i++) is evaluated as follows: printf() statement in function f1() becomes printf(“%d %d %d”,10 ,11 ,12) which displays 10 11 12 on the console. The function f2 uses cdecl mechanism of parameter invocation. Hence the function call f2(i++, i++,i++) is evaluated as follows: printf() statement in function f2() becomes printf(“%d %d %d”,12 ,11 ,10) which displays 12 11 10 on the console. Q. No. 124 Category : Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { signed char i=0;
  • 141. 140 clrscr(); for(;i>=0;i++) ; printf("%dn",i); getch(); } Output : Explanation : Notice the semicolon at the end of the for loop. The initial value of the i is set to 0. The inner loop executes to increment the value from 0 to 127 (the positive range of char) and then it rotates to the negative value of -128. The condition in the for loop fails and so comes out of the for loop. It prints the current value of i that is -128. Q. No. 125 Category : Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { unsigned char i=0; clrscr(); for(;i>=0;i++) ; printf("%dn",i); getch(); } Output : Infinite Loop
  • 142. 141 Explanation : The difference between the previous question and this one is that the char is declared to be unsigned. So the i++ can never yield negative value and i>=0 never becomes false so that it can come out of the for loop. Q. No. 126 Category : Variable Declaration Issues Is the following statement a declaration/definition. Find what does it mean? int (*x)[10]; Output : x is a pointer to array of(size 10) integers. Explanation : Apply clock-wise rule to find the meaning of this definition. There is a technique known as the ``Clockwise/Spiral Rule'' which enables any C programmer to parse any C declaration. For example consider the following declaration, int *x[10] Step 1 :x is a …. Step 2 : We move in a spiral clockwise direction starting with `x' and the first character we see is x[10] Hence, x is an array of 10 …. Step 3: Continue in a spiral clockwise direction, and the next thing we encounter is the `*' so, we have pointers Hence, x is an array 10 of pointers to... Step 4 :Continue in a spiral direction and we see the end of the line i.e., `;', so keep going and we get to the type `int', Hence, ”x is an array 10 of pointers to int'' In the declaration,
  • 143. 142 int (*x)[10]; (*x) is a single entity, Hence, x is a pointer …. Move in the spiral clockwise direction, to an array of 10 Continue to move in the spiral clockwise direction, integers. Hence, “x is a pointer to an array of 10 integers”. Q. No. 127 Category : Abstract Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> typedef struct error{int warning, error, exception;}error; void main() { error g1; g1.error =1; printf("%d",g1.error); } Output : Explanation : The three usages of name errors can be distinguishable by the compiler at any instance, so valid (they are in different namespaces). typedef struct error { int warning, error, exception;
  • 144. 143 }error; This error can be used only by preceding the error by struct keyword as in: struct error someError; typedef struct error { int warning, error, exception; }error; This can be used only after . (dot) or -> (arrow) operator preceded by the variable name as in : g1.error =1; printf("%d",g1.error); typedef struct error{int warning, error, exception;}error; This can be used to define variables without using the preceding struct keyword as in: error g1; Since the compiler can perfectly distinguish between these three usages, it is perfectly legal and valid. Q. No. 128 Category : Preprocessors Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> #ifdef something int some=0; #endif void main() { int thing = 0; printf("%d %dn", some ,thing); Error }
  • 145. 144 Output : Explanation : This is a very simple example for conditional compilation. The name „something‟ is not already known to the compiler making the declaration int some = 0; effectively removed from the source code. Q. No. 129 Category : Preprocessors Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> #if something == 0 int some=0; #endif void main() { int thing = 0; clrscr(); printf("%d %dn", some ,thing); getch(); } Output :
  • 146. 145 Explanation : This code is to show that preprocessor expressions are not the same as the ordinary expressions. If a name is not known the preprocessor treats it to be equal to zero. Q. No. 130 Category : Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { clrscr(); if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); getch(); } Output : Explanation : ~ (tilde operator or bit-wise negation operator) operates on 0 to produce all ones to fill the space for an integer. –1 is represented in unsigned value as all 1‟s and so both are equal. Q. No. 131 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { char *p = “SIBER”; printf(“%c”,++*(p++)); }
  • 147. 146 Output : Explanation : The expression ++*(p++)) is evaluated as follows: Step 1 : p is declared as a character pointer pointing to the character S of a string “SIBER”. Step 2 : The pointer is dereferenced and then incremented by 1, to a value T (next to S in alphabetic sequence). Step 3 : After the printf statement is executed, the pointer p is incremented by 1 and will point to the next character of the string “SIBER”, I. Add the following statement at the end of the program and re-execute it. printf("%c",*p); On execution of the program, the following output is generated. Q. No. 132 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=5; clrscr(); printf("%d",++i++); Error getch(); }
  • 148. 147 Output : Explanation : ++i yields an rvalue. For postfix ++ to operate an lvalue is required. Q. No. 133 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { char *p = “ayqm”; char c; c = ++*p++; clrscr(); printf(“%c”,c); getch(); } Output : Explanation : There is no difference between the expression ++*(p++) and ++*p++. Parenthesis just works as a visual clue for the reader to see which expression is first evaluated.
  • 149. 148 The expression ++*(p++)) is evaluated as follows: Step 1 : p is declared as a character pointer pointing to the character S of a string “ayqm”. Step 2 : The pointer is dereferenced and then incremented by 1, to a value b (next to a in alphabetic sequence). Step 3 : After the printf statement is executed, the pointer p is incremented by 1 and will point to the next character of the string “ayqm”, y. Add the following statement at the end of the program and re-execute it. printf("%c",*p); On execution of the program, the following output is generated. Q. No. 134 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=5; clrscr(); printf(“%d”,i=++i==6); getch(); } Output :
  • 150. 149 Explanation : The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result. Q. No. 135 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { char p[ ]="%dn"; p[1] = 'c'; clrscr(); printf(p,65); getch(); } Output : Explanation : Due to the assignment p[1] = „c‟ the string becomes, “%cn”. Since this string becomes the format string for printf and ASCII value of 65 is „A‟, the same gets printed. Q. No. 136 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i = 3;
  • 151. 150 clrscr(); for (;i++=0;) Error printf(“%d”,i); getch(); } Output : Explanation : As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an assignment operation. Q. No. 137 Category : Control Statements Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { static int i; clrscr() while(i<=10) (i>2)?i++:i--; printf(“%d”, i); } Output :
  • 152. 151 Explanation : Since i is static it is initialized to 0. Inside the while loop the conditional operator evaluates to false, executing i--. This continues till the integer value rotates to positive value (32767). The while condition becomes false and hence, comes out of the while loop, printing the i value. Q. No. 138 Category : Variable Declaration Issues 1. const char *a; 2. char* const a; 3. char const *a; Differentiate the above declarations. Output : Explanation : 1. 'const' applies to char * rather than 'a' (pointer to a constant char) *a='F' : illegal a="Hi" : legal 2. 'const' applies to 'a' rather than to the value of a (constant pointer to char ) *a='F' : legal a="Hi" : illegal 3. Same as 1. Q. No. 139 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=5,j=10; i=i&=j&&10; clrscr(); printf("%d %d",i,j); getch(); }
  • 153. 152 Output : Explanation : The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1 because j==10. i is 5. i = 5&1 is 1. Hence the result. Q. No. 140 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); clrscr(); printf("%d %d", i, j); getch(); } Output : Explanation : The boolean expression needs to be evaluated only till the truth value of the expression is not known. j is not equal to zero itself means that the expression‟s truth value is 1. Because it is followed by || and true || (condition2) => true where (condition2) will not be evaluated. So the remaining expression is not evaluated and so the value of i remains the same. Similarly when && operator is involved in an expression, when any of the operands become
  • 154. 153 false, the whole expression‟s truth value becomes false and hence the remaining expression will not be evaluated. false&& (condition2) => false where (condition2) will not be evaluated. Hence 1 is assigned to j and i continues to remain at 4. Hence the given program generates the output 4 1. Q. No. 141 Category : Storage Classes Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { register int a=2; clrscr(); printf("Address of a = %d",&a); Line 8 printf("Value of a = %d",a); getch(); } Output : Explanation : & (address of ) operator cannot be applied on register variables. Q. No. 142 Category : Control Statements Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h>
  • 155. 154 void main() { float i=1.5; switch(i) Line 7 { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } Output : Explanation : Switch statements can be applied only to integral types. Q. No. 143 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int a=2,*f1,*f2; f1 = f2 = &a; *f2 += *f2 += a += 2.5; clrscr(); printf("n%d %d %d",a,*f1,*f2); getch(); } Output :
  • 156. 155 Explanation : f1 and f2 both refer to the same memory location a. So changes through f1 and f2 ultimately affects only the value of a. The statement *f2 += *f2 += a += 2.5;is evaluated as follows: Step 1 : f1 and f2 both refer to the same memory location a as shown below: After f2+=8, Hence a, f1 and f2 point to the same memory location containing the 16. Hence the output. Q. No. 144 Category : Arrays and Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h>
  • 157. 156 void main() { char *p="GOOD"; char a[ ]="GOOD"; clrscr(); printf("n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p),strlen(p)); printf("n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); getch(); } Output : Explanation : sizeof(p) => sizeof(char*) => 2 sizeof(*p) => sizeof(char) => 1 Similarly, sizeof(a) => size of the character array => 5 When sizeof operator is applied to an array it returns the sizeof the array and it is not the same as the sizeof the pointer variable. Here the sizeof(a) where a is the character array and the size of the array is 5 because the space necessary for the terminating NULL character should also be taken into account. Q. No. 145 Category : Preprocessors Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> #define DIM( array, type) sizeof(array)/sizeof(type) void main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }
  • 158. 157 Output : Explanation : The size of integer array of 10 elements is 10 * sizeof(int). The macro expands to sizeof(arr)/sizeof(int) => 10 * sizeof(int) / sizeof(int) => 10. Modify the above program as shown below and re-execute it. #include <stdio.h> #include <conio.h> #define DIM( array, type) sizeof(array)/sizeof(type) void main() { int arr[10]; float arr1[50]; clrscr(); printf("The dimension of the array arr is %d", DIM(arr, int)); printf("nThe dimension of the array arr1 is %d", DIM(arr1, float)); getch(); } On re-execution of the program, the following output is generated. Q. No. 146 Category : Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h>
  • 159. 158 int DIM(int array[]) { return sizeof(array)/sizeof(int ); } void main() { int arr[10]; clrscr(); printf(“The dimension of the array is %d”, DIM(arr)); getch(); } Output : Explanation : Arrays cannot be passed to functions as arguments and only the pointers can be passed. So the argument is equivalent to int * array (this is one of the very few places where [] and * usage are equivalent). The return statement becomes, sizeof(int *)/ sizeof(int) that happens to be equal in this case. Q. No. 147 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i = 257; int *iPtr = &i; clrscr(); printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); getch(); }
  • 160. 159 Output : Explanation : The integer value 257 is stored in the memory as, 00000001 00000001, so the individual bytes are taken by casting it to char * and get printed. Q. No. 148 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=300; char *ptr = &i; *++ptr=2; clrscr(); printf("%d",i); getch(); } Output : Explanation : The integer value 300 in binary notation is: 00000001 00101100. It is stored in memory (small- endian) as: 00101100 00000001. Result of the expression *++ptr = 2 makes the memory representation as: 00101100 00000010. So the integer corresponding to it is 00000010
  • 161. 160 00101100 => 556. Q. No. 149 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h void main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = *ptr; clrscr(); printf("%d",least); getch(); } Output : Explanation : After „ptr‟ reaches the end of the string the value pointed by „str‟ is „0‟. So the value of „str‟ is less than that of „least‟. So the value of „least‟ finally is 0. Q. No. 150 Category : Abstract Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { struct student {
  • 162. 161 char name[30]; struct date dob; Line 9 }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.name, &stud.dob.day, &stud.dob.month, &stud.dob.year); } Output : Explanation : Inside the struct definition of „student‟ the member of type struct date is given. The compiler doesn‟t have the definition of date structure (forward reference is not allowed in C in this case) so it issues an error. To remove the error, re-write the program as shown below and re-execute it. Place struct date before struct student. #include <stdio.h> #include <conio.h> void main() { struct date { int day,month,year; }; struct student { char name[30]; struct date dob;
  • 163. 162 }stud; scanf("%s%d%d%d", stud.name, &stud.dob.day, &stud.dob.month, &stud.dob.year); } Q. No. 151 Category : Functions Is there any difference between the two declarations, 1. int foo(int *arr[]) 2. int foo(int *arr[2]) Output : No Explanation : Functions can only pass pointers and not arrays. The numbers that are allowed inside the [] is just for more readability. So there is no difference between the two declarations. Q. No. 152 Category : Functions What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; } Output : Explanation : assert macro should be used for debugging and finding out bugs. The check s != NULL is for error/exception handling and for that assert shouldn‟t be used. A plain if and the corresponding remedy statement has to be given.
  • 164. 163 Q. No. 153 Category : Macros What is the hidden bug with the following statement? assert(val++ != 0); Output : Explanation : Assert macro is used for debugging and removed in release version. In assert, the expression involves side-effects. So the behavior of the code becomes different in case of debug version and the release version thus leading to a subtle bug. Rule to Remember: Don‟t use expressions that have side-effects in assert statements. Q. No. 154 Category : Memory Allocation Predict the output or error(s) for the following: void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; } Output : Undefined Behaviour Explanation : The second statement results in undefined behavior because it points to some location whose value may not be available for modification. This type of pointer in which the non-availability of the implementation of the referenced location is known as 'incomplete type'. Q. No. 155 Category : Abstract Data Types Is the following code legal?
  • 165. 164 struct a { int x; struct a b; } Output : No Explanation : Is it not legal for a structure to contain a member that is of the same type as in this case. Because this will cause the structure declaration to be recursive without end. However, a structure can contain a pointer to a structure as one of its members, as shown below: struct a { int x; struct* a b; } Q. No. 156 Category : Abstract Data Types Is the following code legal? struct a { int x; struct a *b; } Output : Yes Explanation : *b is a pointer to type struct a and so is legal. The compiler knows, the size of the pointer to a structure even before the size of the structure is determined(as you know the pointer to any type is of same size). This type of structures is known as „self-referencing‟ structure.
  • 166. 165 Q. No. 157 Category : Abstract Data Types Is the following code legal? typedef struct a { int x; aType *b; }aType Output : No Explanation : The typename aType is not known at the point of declaring the structure (forward references are not made for typedefs). Re-write the above code as shown below: struct a { int x; aType *b; }; The typename aType is known at the point of declaring the structure, because it is already type defined. Q. No. 158 Category : Abstract Data Types Is the following code legal? void main() { typedef struct a aType; aType someVariable; struct a { int x; aType *b; }; }
  • 167. 166 Output : No Explanation : When the declaration, typedef struct a aType; is encountered body of struct a is not known. This is known as „incomplete types‟. Q. No. 159 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { clrscr(); printf(“sizeof (void *) = %d n“, sizeof( void *)); printf(“sizeof (int *) = %d n”, sizeof(int *)); printf(“sizeof (double *) = %d n”,sizeof (double *)); printf(“sizeof(struct unknown *) = %d n”, sizeof(struct unknown *)); getch(); } Output : Explanation : The pointer to any type is of same size, equal to 2 bytes.
  • 168. 167 Q. No. 160 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; Error clrscr(); printf(“%d”,k); getch(); } Output : Explanation : The programmer intended to divide two integers, but by the “maximum munch” rule, the compiler treats the operator sequence / and * as /* which happens to be the starting of comment. To force what is intended by the programmer, int k = *ip/ *jp; there are two methods available. Method 1 : // give space explicitly separating / and * Method 2 : int k = *ip/(*jp);
  • 169. 168 // put braces to force the intention will solve the problem. Incorporate the above change and re-execute the program, the following output is generated. Q. No. 161 Category : Control Statements Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { char ch; clrscr(); for(ch=0;ch<=127;ch++) printf(“%c %d n“, ch, ch); getch(); } Output :
  • 170. 169 Explanation : The char type may be signed or unsigned by default. If it is signed then ch++ is executed after ch reaches 127 and rotates back to -128. Thus ch is always smaller than 127. Q. No. 162 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main(){ int *ptr; ptr = (int *) 0x400; clrscr(); printf("%d",*ptr); getch(); } Output : Explanation : The pointer ptr will point at the integer in the memory location 0x400. Q. No. 164 Category : Arrays Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { char a[4]="HELLO"; Line 6 clrscr(); printf("%s",a); getch(); }
  • 171. 170 Output : Explanation : The array a is of size 4 but the string constant “HELLO”, requires 6 bytes to get stored. Q. No. 165 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int a=10,*j; void *k; j=k=&a; j++; k++; Line 10 printf("n %u %u ",j,k); } Output : Explanation : void pointers are generic pointers and they can be used only when the type is not known and as
  • 172. 171 an intermediate address storage type. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers. The pointer k can be incremented as follows: Cast void pointer to integer pointer as shown below: (int *)k Increment the pointer now, ((int *)k)++ Predict the output #include <stdio.h> #include <conio.h> void main() { int a=10,*j; void *k; j=k=&a; (*j)++; (*(int*)k)++; clrscr(); printf("n %d %d",*j,*(int*)(k)); getch(); } On execution of the above program, the following output is generated: Q. No. 166 Category : Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h>
  • 173. 172 char *someFun() { char *temp = “string constant"; return temp; } void main() { clrscr(); puts(someFun()); getch(); } Output : Explanation : The program suffers no problem and gives the output correctly because the character constants are stored in code/data area and not allocated in stack, so this doesn‟t lead to dangling pointers. Q. No. 167 Category : Functions #include <stdio.h> #include <conio.h> char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {„s‟, „t‟,‟r‟,‟i‟,‟n‟,‟g‟}; return temp; }
  • 174. 173 void main() { clrscr(); puts(someFun1()); puts(someFun2()); getch(); } Output : Explanation : The above program demonstrates the situation of dangling pointers. The memory for the character array temp is allocated in function someFun1() which is deallocated when the function returns. someFun1() returns the character pointer where the string “string” was previously present. The same situation arises in someFun2(). The correct version of the program is shown below: #include <stdio.h> #include <conio.h> char *someFun1() { char* temp= "string"; return temp; } char *someFun2() { char* temp = (char*)malloc(7*sizeof(char)); //char* temp[]= {'s', 't','r','i','n','g'}; temp[0]='s'; temp[1]='t'; temp[2]='r'; temp[3]='i'; temp[4]='n'; temp[5]='g'; temp[6]='0'; return temp; }
  • 175. 174 void main() { clrscr(); puts(someFun1()); puts(someFun2()); free(someFun2()); getch(); } On execution of the above program, the following output is generated. Q. No. 168 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i,j; j = 10; i = j++ - j++; clrscr(); printf("%d %d", i,j); getch(); } Output :
  • 176. 175 Explanation : In the expression, i = j++ - j++ the expression i = j – j is first evaluated and then the value of j is incremented twice. Hence i gets the value 0 while j becomes 12, hence the output. Q. No. 169 Category : Functions Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int j; clrscr(); for(j=0;j<3;j++) foo(); getch(); } foo() { static int i = 10; i+=10; printf(%dn,i); } Output : Explanation : In the main() function, the function foo() is invoked 3 times in the for loop. In the function foo()
  • 177. 176 since the variable i is declared as static, it is initialized only once and retains the value between function calls. Hence for every subsequent function call, the value of i is incremented by 10 and its value is printed in the next statement, generating the output 20 30 40 Q. No. 170 Category : Abstract Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> union test { int a; int b; int c; }; void main() { union test u; u.a = 10; u.b = 20; printf(“%d %d n”,u.a,u.b); } Output : Explanation : Since test is declared as a union with three members a, b and c of type int, by definition of union, a, b and c share the same memory location of size 2 bytes. Any change to a single member is reflected in other members of the union. Latest assignment overwrites the values of other
  • 178. 177 members. Hence the statement u.b = 20; assigns the value of 20 to all the members of the union. Modify the printf() statement as shown below and re-execute the program. printf(%d %d %dn,u.a,u.b,u.c); On re-execution of the program, the following output is generated. Q. No. 171 Category : Operators Predict the output or error(s) for the following: #include <stdlib.h> #include <stdio.h> void f() { static int a[3] = {1, 2, 3}; a[1]++; printf("%dn", a[1]); } main() { int i; clrscr(); for (i = 0; i < 5; i++) { f(); } getch(); }
  • 179. 178 Output : Explanation : The function f() is invoked 5 times from the main() method. Since a is declared as a static array in f() its elements are initialized only once and the elements of a retain their values between function calls.. In the function f(), the second element of the array is incremented and the same is printed in the next statement. Hence the program generates the output 3 4 5 6 7 Q. No. 172 Category : Control Statements Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=0; clrscr(); if(i=0) printf("statement 1"); else printf("statement 2"); getch(); }
  • 180. 179 Output : Explanation : = is an assignment operator and not a comparison operator. Hence in the expression, if(i=0) 0 is assigned to i and the condition becomes if (0), hence the condition evaluates to false and the statement in the else part is evaluated which displays „statement 2‟, hence the output. Q. No. 173 Category : Operators What do you mean by associativity and precedence of C operators? Answer: Operator precedence determines which operator is performed first in an expression with multiple operators. Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Left to Right or Right to Left. Hence, associativity is used when there are two or more operators of same precedence. Explanation :
  • 181. 180 Associativity of Function Invocations Consider the following program : #include <stdio.h> #include <conio.h> int x = 1; int f1() { x += 5; return x; } int f2() { x *= 3; return x; } int main() { int p = f1() + f2(); clrscr(); printf("%d ", x); getch(); return 0; } The value of p in the following expression int p = f1() + f2(); depends on the order in which the functions are invoked i.e. associativity of function invocations. If f1() is invoked first, followed by f2(), then the output generated is 18, on the other hand if it is reversed, i.e. f2() is invoked first followed by f1(), the output generated is 8. To test this, execute the above program. On execution of the above program, the following output is generated.
  • 182. 181 Hence the associativity of function invocations is from left to right. Q. No. 174 Category : Operators What is the output generated on execution of the following program? #include <stdio.h> #include <conio.h> void main() { int a=(1 & 3) + (3 & 4); clrscr(); printf("%d",a); getch(); } Output : 1 Explanation : The expression a=(1 & 3) + (3 & 4); is evaluated as follows: Step 1 : 1 & 3 is evaluated as 00000000 00000001 & 00000000 00000011 = 00000000 00000001
  • 183. 182 Step 2 : 3 & 4 is evaluated as 00000000 00000011 & 00000000 00000100 = 00000000 00000000 Step 3 : The results obtained in Step 1 and Step 2 are added and the result is assigned to a. Hence a gets the value 1, hence the output. if int a=1&3 + 3&4 then, o/p is 0 Q. No. 175 Category : Functions What is the output generated on execution of the following program? #include <stdio.h> #include <conio.h> void f(int n) { if (n!=0) { printf("%d",n); f(n-1); printf("%d",n); } } void main() { clrscr(); f(5); getch(); } Output : 5432112345
  • 184. 183 Explanation : The main() method invokes the function f with argument 5 passed to it. In the function f(), if condition evaluates to true. Hence 5 is printed and context is pushed on to the stack. Hence successively, 5, 4, 3, 2 and 1 are printed and the if condition evaluates to false. Finally, stack is popped up in the reverse order to generate the output 5432112345 f(5) = 5f(4)5 f(4) = 4f(3)4 f(3) = 3f(2)3 f(2) = 2f(1)2 f(1) = 11 Substituting the values in the reverse order, we get f(2)=2112 f(3)=321123 f(4)=43211234 f(5)=5432112345 Hence the output. Q. No. 176 Category : Abstract Data Types What is the output generated on execution of the following program? #include<stdio.h> int main() { struct emp { char name[20]; int age; float sal; };
  • 185. 184 struct emp e = {"Tiger"}; printf("%d, %fn", e.age, e.sal); return 0; } Output : 0, 0.000000 Explanation : When an automatic structure is partially initialized remaining elements are initialized to 0(zero). Q. No. 177 Category : Operators What is the output generated on execution of the following program? #include<stdio.h> int main() { int x = 10, y = 20; if(!(!x) && x) printf("x = %dn", x); else printf("y = %dn", y); return 0; } Output : x=10 Explanation : The logical not operator takes expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it reverses the value of the expression. Step 1: if(!(!x) && x) Step 2: if(!(!10) && 10) Step 3: if(!(0) && 10) Step 3: if(1 && 10)
  • 186. 185 Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10. Q. No. 178 Category : Arrays and Functions In C, if you pass an array as an argument to a function, what actually gets passed?  A. Value of elements in array  B. First element of the array  C. Base address of the array  D. Address of the last element of array Output : Option C Explanation : The statement 'C' is correct. When we pass an array as a function argument, the base address of the array will be passed. Q. No. 179 Category : Arrays and Operators What is the output generated on execution of the following program? #include<stdio.h> #include <conio.h> int main() { int a[5] = {5, 1, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; clrscr(); printf("%d, %d, %d", i, j, m); getch(); return 0; }
  • 187. 186 Output : Explanation : Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 . Step 2: int i, j, m; The variable i,j,m are declared as an integer type. Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2 Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3. Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3) Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m Hence the output of the program is 3, 2, 15 Q. No. 180 Category : Functions What is the output generated on execution of the following program? #include<stdio.h> #include <conio.h> int main() { void fun(int, int[]); int arr[] = {1, 2, 3, 4}; int i; fun(4, arr); clrscr(); for(i=0; i<4; i++) printf("%d,", arr[i]); getch();
  • 188. 187 return 0; } void fun(int n, int arr[]) { int *p=0; int i=0; while(i++ < n) p = &arr[i]; *p=0; } Output : Explanation : Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything. Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is initialized to a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4 Step 3: int i; The variable i is declared as an integer type. Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function. Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs until the variable i is less than '4' and it prints the each value of array a. Hence the output of the program is 1,2,3,4 Q. No. 181 Category : Arrays and Functions What is the output generated on execution of the following program?
  • 189. 188 #include<stdio.h> #include <conio.h> void fun(int **p); int main() { int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0}; int *ptr; ptr = &a[0][0]; clrscr(); fun(&ptr); getch(); return 0; } void fun(int **p) { printf("%dn", **p); } Output : Explanation : Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0}; The variable a is declared as an multidimensional integer array with size of 3 rows 4 columns. Step 2: int *ptr; The *ptr is a integer pointer variable. Step 3: ptr = &a[0][0]; Here we are assigning the base address of the array a to the pointer variable*ptr. Step 4: fun(&ptr); Now, the &ptr contains the base address of array a. Step 5: Inside the function fun(&ptr); The printf("%dn", **p); prints the value '1'. because the *p contains the base address or the first element memory address of the array a (i.e. a[0]) **p contains the value of *p memory location (i.e. a[0]=1). Hence the output of the program is '1'
  • 190. 189 Q. No. 182 Category : Pointers and Arrays What is the output generated on execution of the following program? #include<stdio.h> #include <conio.h> int main() { static int arr[] = {0, 10, 20, 30, 40}; int *p[] = {arr+4, arr+3, arr+2, arr+1, arr}; int **ptr=p; clrscr(); ptr++; printf("%dn", ptr-p); *ptr++; printf("%dn", ptr-p); *++ptr; printf("%dn", ptr-p); getch(); return 0; } Output : Explanation : In the above program, arr is an integer array and p is an integer pointer pointing to address of the first element of an array in the reverse order as shown below: 0 10 20 30 40 1000 1002 1004 1006 1008
  • 191. 190 1008 1006 1004 1002 1000 2000 p ptr Hence both p and ptr are pointer of pointers. After the execution of the statements ptr++, we have 1008 1006 1004 1002 1000 2000 p ptr Hence ptr-p =1 (no. of objects between ptr and p) After the execution of the statement *ptr++ 1008 1006 1004 1002 1000 2000 p ptr Hence ptr-p = 2 (no. of objects between ptr and p) Similarly, after the execution of the statement *++ptr; ptr-p = 3. Hence the given program generates the output 1 2 3 Q. No. 183 Category : Arrays What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes? #include<stdio.h> #include<conio.h> int main() { int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
  • 192. 191 clrscr(); printf("%u, %un", a+1, &a+1); getch(); return 0; } Output : 65480, 65496 Modify the printf() statement as shown below and execute the program. printf("%u %u, %un", a, a+1, &a+1); The following output is generated. Explanation : Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an integer array having the 3 rows and 4 columns dimensions. Step 2: printf("%u, %un", a+1, &a+1); The base address(also the address of the first element) of array is 65472. For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480 Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".
  • 193. 192 Hence, beginning address 65472 + 24 = 65496. So, &a+1 = 65496 Hence the output of the program is 65480, 65496 Q. No. 184 Category : Arrays What will be the output of the program if the array begins 1200 in memory? #include<stdio.h> #include <conio.h> int main() { int arr[]={2, 3, 4, 1, 6}; clrscr(); printf("%u, %u, %un", arr, &arr[0], &arr); getch(); return 0; } Output : 1200, 1200, 1200 Explanation : Step 1: int arr[]={2, 3, 4, 1, 6}; The variable arr is declared as an integer array and initialized. Step 2: printf("%u, %u, %un", arr, &arr[0], &arr); Here, The base address of the array is 1200. =>arr, &arr is pointing to the base address of the array arr. =>&arr[0] is pointing to the address of the first element array arr. (ie. base address) Hence the output of the program is 1200, 1200, 1200 On execution of the program, the following output is generated.
  • 194. 193 Q. No. 185 Category : Operators What is the output generated on execution of the following program? #include<stdio.h> #include <conio.h> int main() { int i=3, *j; j = &i; clrscr(); printf("%dn", i**j*i+*j); getch(); return 0; } Output : Explanation : The expression i**j*i+*j is evaluated as i*(*j)*i+(*j) i and *j refer to the same entity. Hence the above expression is i * i * i + i = 30. Hence the program generates the output 30.
  • 195. 194 Q. No. 186 Category : Operators What is the output generated on execution of the following program? #include <stdio.h> #include <conio.h> int main() { int x=30, *y, *z; y=&x; /* Assume address of x is 500 and integer is 4 byte size */ z=y; clrscr(); printf("x=%d, y=%d, z=%dn", x, y, z); *y++=*z++; x++; printf("x=%d, y=%d, z=%dn", x, y, z); getch(); return 0; } Output : x=31, y=504, z=504 On execution on the real system, the following output is generated. Explanation : The expression *y++=*z++; is equivalent to (*y)++=(*z)++;
  • 196. 195 Post increment has higher priority than dereference operator. The above expression is evaluated in the following steps: Step 1 : Pointer z is dereferenced Step 2 : Pointer z is incremented. Step 3 : Value in the memory location pointed by z is assigned to variable pointed by y. Step 4 :Pointer y is incremented. To clearly understand the working of the statement *y++=*z++; Consider the following program: #include <stdio.h> #include <conio.h> void main() { char str[] = "0123456789"; char *x = str; char *y = "abcdefghij"; printf("x : %s y : %sn",x,y); *x++ = *y++; printf("x : %s y : %s",x,y); x--; printf("x : %s",x); getch(); clrscr(); } On execution of the above program, the following output is generated.
  • 197. 196 x and y are character pointers pointing to first character of strings "0123456789" and “abcdefghij”, respectively, as shown below: 0 1 2 3 4 5 6 7 8 9 x a b c d e f g h i j y The statement *x++ = *y++; is executed as follows: i. y is dereferenced, so the right hand side of the expression becomes a. ii. y is incremented so that it now points to b. a b c d e f g h i j y iii. a is assigned to memory location pointed by x as shown below: a 1 2 3 4 5 6 7 8 9 x iv. x is incremented. a 1 2 3 4 5 6 7 8 9 x Hence the statement
  • 198. 197 printf("x : %s y : %s",x,y); generates the output. x: 123456789 y : bcdefghij Next x is decremented as shown below: a 1 2 3 4 5 6 7 8 9 x Next, the value of x is printed as a123456789. Q. No. 187 Category : Pointers What is the output generated on execution of the following program? #include<stdio.h> #include <conio.h> int main() { char str[20] = "Hello"; char *const p=str; *p='M'; clrscr(); printf("%sn", str); getch(); return 0; } Output : Explanation : str points to the first character „H‟ of a string “Hello” as shown below:
  • 199. 198 H e l l o 0 str p is declared as a constant pointer initialized to str. Hence, p also points to the first character „H‟ of a string “Hello” as shown below: H e l L o 0 p str Since p is a constant pointer, it cannot be modified. However, it can modify the content of the memory location it is pointing to. The statement *p='M'; changes „H‟ to „M‟ as shown below: M e l L o 0 p str Hence the program generates the output “Mello”. Q. No. 188 Category : Pointers What will be the output of the program if the integer is 4 bytes long? #include<stdio.h> #include<conio.h> int main() { int ***r, **q, *p, i=8; p = &i; q = &p; r = &q; clrscr(); printf("%d, %d, %dn", *p, **q, ***r); getch(); return 0; }
  • 200. 199 Output : Explanation : The snapshot of the memory storing variables is as shown below: i p q r 1000 2000 3000 The expression ***r is evaluated as follows: ***r = ***&q = **q (since *& =1 ) = **&p = *p = *&i = i Hence, ***r = i. On the same arguments, **q = *p = i Hence, *p, **q and ***r all refer to the same variable i. Hence the program generated the output 8, 8, 8. Q. No. 189 Category : Functions What will be the output of the program? #include<stdio.h> #include<conio.h> int *check(static int, static int); int main() { int *c; c = check(10, 20); 8 2000 1000 000 3000
  • 201. 200 clrscr(); printf("%dn", c); getch(); return 0; } int *check(static int i, static int j) Line 14 { int *p, *q; p = &i; q = &j; if (i >= 45) return (p); else return (q); } Output : Compiler Error Explanation : The static keyword means that a variable may have only and exactly one instance in its scope, and that instance is invisible out of its scope. Neither of these requirements make sense for a function argument Q. No. 190 Category : Operators What will be the output of the program ? #include<stdio.h> #include <conio.h> int main() { clrscr();
  • 202. 201 printf("%d, %dn", sizeof(NULL), sizeof("")); getch(); return 0; } Output : Explanation : The literal "" is an array that consists of one char with the value 0. The type is char[1], and sizeof(char) is always one; therefore sizeof(char[1]) is always one. NULL in C id declared as ((void*)0) which is a void pointer, hence its size is 2 bytes. Q. No. 191 Category : Pointers and Arrays What will be the output of the program? #include<stdio.h> #include <conio.h> int main() { int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8}; int *p, *q; p = &arr[1][1][1]; q = (int*) arr; clrscr(); printf("%d, %dn", *p, *q); getch(); return 0; }
  • 203. 202 Output : Explanation : In the above program, arr is a 3D array of dimension 2 x 2 x 2. The elements are stored as follows: i j k a[i][j][k] 0 0 0 10 0 0 1 2 0 1 0 3 0 1 1 4 1 0 0 5 1 0 1 6 1 1 0 7 1 1 1 8 Hence a[1][1][1] = 8 In the expression, p = &arr[1][1][1]; the address of the element a[1][1][1] is assigned to p. Hence, *p is 8. q is initialized with arr, the address of the first element of the array. Hence *q is equal to 10. Hence the program generates the output 8, 10. Q. No. 192 Category : Arrays What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?
  • 204. 203 #include<stdio.h> #include <conio.h> int main() { int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; clrscr(); printf("%u, %u, %un", a[0]+1, *(a[0]+1), *(*(a+0)+1)); getch(); return 0; } Output : 1006, 2, 2 Explanation : The elements of the array are stored in th contiguous memory locations in row major order as shown below: 1 2 3 4 5 6 7 8 9 10 11 12 a[0] a[1] a[2] 1000 1008 1016 If the base address of the first array is 1002, then a[0] = 1002. Since the size of an integer is 4 bytes, a[0]+1 = 1006 a[0] + 1 points to the memory location, next to a[0] and *( a[0] + 1) dereferences it to 2. Hence *( a[0] + 1) = 2 The expression, *(*(a+0)+1)is same as *(a[0]+1), and hence is equal to 2.
  • 205. 204 In general, *(*(a+i)+j)refers to ijth element of a 2D array. Alternatively, The addresses stored in a[0], a[1] and a[2] are as shown below: a[0] a[1] a[2] 1000 1008 1016 2000 A stores the address of a[0] as shown below: a 2000 3000 Hence , *(*(a+0)+1) = *(1000 + 1) = *(1002)=2 Q. No. 193 Category : Pointers What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { int arr[3] = {2, 3, 4}; char *p; p = arr; p = (char*)((int*)(p)); clrscr(); printf("%d, ", *p); p = (int*)(p+1); printf("%d", *p); getch(); return 0; }
  • 206. 205 Output : Explanation : In the above program, arr is an array of three integers as shown below: 2 3 4 arr p is a character pointer. p points to first byte of integer 2 as shown below: 0000000000000010 Hence *p is 2. On incrementing p, it points to the next byte of integer 2. Now the value of *p is 0. On incrementing the pointer p, again and re-executing the program, the following output is generated. p now points to first byte of integer 3. Q. No. 194 Category : Operators What will be output if you compile and execute the following c code? #include <stdio.h> #include <conio.h>
  • 207. 206 void main() { int i,j=1,k=1; i=j++&k++; clrscr(); printf("j : %d k : %d i : %dn",j,k,i); getch(); } Output : Explanation : In the expression i=j++&k++; first condition evaluates to true. The value assigned to i is the bitwise AND operation between j and k, after that the values of j and k are incremented. Since both j and k are 1, the result of expression is 1 which is assigned to i, then j and k are incremented to 2. Q. No. 195 Category : Operators What will be the output of the program? #include<stdio.h> #include <conio.h> int main() { char *str; str = "%dn"; str++; str++; clrscr(); printf(str-2, 300); getch(); return 0; }
  • 208. 207 Output : Explanation : In the above program, str is a character string pointing to the first character of the string “%dn” as shown below: % d n str The pointer str is incremented twice as shown below: % d n str The statement str-2 then relocates the pointer to the original location. Hence the statement printf(str-2,300); becomes printf(“%d”,300) which displays 300. Q. No. 196 Category : Arrays What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { clrscr(); printf("%cn", 7["SIBERINDIA"]); getch(); return 0; }
  • 209. 208 Output : print "D" of SIBERINDIA Explanation : 7["SIBERINDIA"] refers to the character at the index position 7 (index starts at 0). The character at the index position 7 is „D‟, the same is printed. In C, s[i] and i[s] are two different ways of referring to the ith character of string s. Q. No. 197 Category : Operators What will be output if you compile and execute the following c code? #include<stdio.h> #include <conio.h> int main() { int x=258; char* p; clrscr(); p=&x; printf("*p = %un",*p); p++; printf("*p = %u",*p); getch(); return 0; }
  • 210. 209 Output : Explanation : The variable x occupies 16 bits arranged as shown below: 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 Byte 1 Byte p initially points to the least significant byte of 2 byte storage space of x with a bit 1 at position 2 as shown in the Figure. Hence *p is equal to 2. On incrementing p, it will point to next byte of integer with a bit 1 at position 1. Hence *p now equals 1 as evident from the above Figure. Q. No. 198 Category : Operators If the size of integer is 4bytes, What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { int arr[] = {12, 13, 14, 15, 16}; clrscr(); printf("%d, %d, %dn", sizeof(arr), sizeof(*arr), sizeof(arr[0])); getch(); return 0; } Output : 20, 4, 4
  • 211. 210 Explanation : In the above program, arr refers to the integer array of 5 elements. Since the size of integer is 2 bytes, Size of arr is 5 * 2 = 10 *arr and arr[0] both refer to the same entity, i.e. first element of the array, hence the size of *arr and arr[0] is 2. Q. No. 199 Category : Pointers and Arrays What is the output generated on execution of the following program? #include <stdio.h> #include <conio.h> void main() { char s1[]="SIBER"; char s2[]="MCA"; char *p; s1=s2; Line 9 p=s2; clrscr(); printf("%s",p); getch(); } Output : Compiler Error s1 is constant pointer, hence its value cannot be changed.
  • 212. 211 Explanation : Array name is a constant pointer to the first element of the array. Hence array name cannot be modified or it cannot appear on the left hand side of the assignment statement. Q. No. 200 Category : Pointers and Arrays What is the output generated on execution of the following program? #include <stdio.h> #include <conio.h> void main() { char s[]={'a','b','c','n','c','0'}; char *p, *str, *strl; p=&s[3]; str=p; strl=s; clrscr(); printf("%dn",++*p); printf("%d",++*p + ++*strl - 32); getch(); } Output : Explanation : In the above program, s is a pointer to the character array and p points to the character at the position 3, as shown below: a b c n C 0 s p str and str1 are initialized to p and s, respectively, as shown below:
  • 213. 212 a b c n c 0 s str1 p str Now, *p is 10, hence ++*p is 11. ++*str1 is 99. Hence, ++*p + ++*strl – 32 = 11 + 99 – 32 = 78 Q. No. 201 Category : Operators What is the output generated on execution of the following program? #include <stdio.h> #include <conio.h> void main() { int a=10,b=-1,x,y,z; clrscr(); x=(a!=10)&&(b=1); printf("x=%d",x); printf("b=%dn",b); y=(a==10)&&(b=0); printf("y=%d",y); printf("b=%dn",b); z=(a==10)&&(b=1); printf("z=%d",z); printf("b=%dn",b); getch(); } Output : Explanation : In C, logical operators employ short-circuit evaluation, i.e. in the evaluation of logical AND
  • 214. 213 operation shown below Condition1 && Condition2 Condition2 is evaluated if and only if Condition1 is true since then the output of the complex condition depends on Condition2. Similarly, for the similar reason, in the evaluation of logical OR operation shown below Condition1 || Condition2 Condition2 is evaluated if and only if Condition1 is false since then the output of the complex condition depends on Condition2. In the above program, a = 10 and b = -1. Step 1 : The condition x=(a!=10)&&(b=1);is evaluated as follows: The first condition a!=10 evaluates to false. Hence second condition is not evaluated. Hence the value of b is not modified and is still -1. Since the complex condition evaluates to false, 0 is assigned to x. Hence the first printf statement displays x=0 and b=-1 Step 2 : The condition y=(a==10)&&(b=0);is evaluated as follows: The first condition a==10 evaluates to true. Hence second condition is evaluated which assigns 0 to b. Hence the value of b is modified to 0 and the condition becomes false. Since the complex condition evaluates to false, 0 is assigned to y. Hence the second printf statement displays y=0 and b=0. Step 3 : The condition z=(a==10)&&(b=1);is evaluated as follows: The first condition a==10 evaluates to true. Hence second condition is evaluated which assigns
  • 215. 214 1 to b. Hence the value of b is modified to 1 and the condition becomes true. Since the complex condition evaluates to true, 1 is assigned to z. Hence the third printf statement displays z=1 and b=1. Q. No. 202 Category : Operators What is the output generated on execution of the following program? #include <stdio.h> #include <conio.h> void main() { int x=2,y=4; clrscr(); printf("%d %dn",x,y); x^=y; printf("%dn",x); y^=x; printf("%dn",y); x^=y; printf("%d %d",x,y); getch(); } Output : Explanation : ^ is a bitwise XOR operator in C. The bitwise representation of x and y is as follows: 0 0 0 0 0 0 1 0 and 0 0 0 0 0 1 0 0 Hence x ^ y = 0 0 0 0 0 1 1 0, whose decimal equivalent is 6 (=x).
  • 216. 215 Similarly, (0 0 0 0 0 1 1 0) ^ (0 0 0 0 0 1 0 0 ) = 0 0 0 0 0 0 1 0, whose decimal equivalent is 2 (=y). and(0 0 0 0 0 1 1 0) ^ (0 0 0 0 0 0 1 0 ) = 0 0 0 0 0 0 1 0, whose decimal equivalent is 4 (=x). Hence, at this point x=4 and y=2. This example illustrates swapping two values without any intermediate variable. Q. No. 203 Category : Operators What is the output generated on execution of the following program? #include <stdio.h> #include <conio.h> void main() { int x=20,y=35; clrscr(); x=y++ + x++; y=++y + ++x; printf("%d %d",x,y); getch(); } Output : Explanation : The contents of the different memory locations storing x and y get updated as shown below: x y Initial Values 20 35 After the execution of the statement x=y++ + x++; 56 36 After the execution of the statement y=++y + ++x; 57 94 (57+37)
  • 217. 216 Q. No. 204 Category : Data Types Why reference is not same as a pointer? A. A reference can never be null. B. A reference once established cannot be changed. C. Reference doesn't need an explicit dereferencing mechanism. D. All of the above. Output : Option D Explanation : A reference can be viewed as an alternative identifier for a value stored in computer memory which enables the data to be indirectly accessed. The following example creates a reference to a variable x with the name xref. int x=10; int& xref=x; Once defined, a reference cannot be reassigned a new value. A reference always refers to the object with which it is initialized. Consider the following C program: #include<stdio.h> #include<conio.h> int main() { int x=10; int y=20; int& xref=x; xref=y; clrscr(); printf("x : %d",x); getch(); return 0; } In the above example, xref is a reference to the memory location where x is stored. Hence the
  • 218. 217 same memory location is referenced by two variables x and xref as shown below: x xref After the execution of the statement xref=y; x xref The content of the memory location pointed by xref will be updated by y (=20). Hence indirectly the value of x also gets modified to 20 as seen from the following output generated on execution of the program. Hence xref is an alias to x. The contents of the memory location can be modified using either x or xref. Some salient features of a reference which differentiate it from pointer are listed below: A pointer is memory address of an object stored in computing memory whereas an alternative identifier for a value stored in computer memory. In this context it plays the role of an alias for a variable stored in memory. A reference cannot be dereferenced and make point another memory location. A pointer may not be initialized at the time of declaration or can be null indicating invalid reference. In contrast to this a reference must be initialized at the time of declaration and once 10 20
  • 219. 218 initialized it cannot be dereferenced and point to another memory location. It continues to reference the memory location with which it is initialized at the time of declaration. The contents of the memory location can be modified using either a variable or its reference. Q. No. 205 Category : Control Statements What is the output generated on execution of the following program? #include<stdio.h> #include<conio.h> int main() { int x=1, y=1; clrscr(); for(; y; printf("%d %dn", x, y)) { y = x++ <= 5; } printf("n"); getch(); return 0; } utput : Explanation : In the above program, x =1 and y=1. In the for loop, the condition specified is y. Hence initially the condition becomes true and the body of the for loop is executed. The body of the for loop contains the statement y = x++ <= 5; which is evaluated as follows:
  • 220. 219 Step 1 : The condition x <= 5 is evaluated. Step 2 :The output of the condition in Step 1 is a assigned to y. Step 3 : The value of x is incremented. Hence after the first iteration, x=2 and y=1. After second iteration, x=3 and y=1. After third iteration, x=4 and y=1. After fourth iteration, x=5 and y=1. After sixth iteration, x=6 and y=1. After sixth iteration, the condition becomes false and 0 is assigned to y and x is incremented to 7. Since y=0, the condition in for loop evaluates to false and control comes out of the for loop. Q. No. 206 Category : Control Statements What is the output generated on execution of the following program? #include<stdio.h> #include <conio.h> int main() { int i = 5; clrscr(); while(i-- >= 0) printf("%d,", i); i = 5; printf("n"); while(i-- >= 0) printf("%i,", i); while(i-- >= 0) printf("%d,", i); getch(); return 0; }
  • 221. 220 Output : Explanation : Step 1: Initially the value of variable i is '5'. Loop 1:while(i-- >= 0) here i = 5, this statement becomes while(5-- >= 0) Hence the while condition is satisfied and it prints '4'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 2: while(i-- >= 0) here i = 4, this statement becomes while(4-- >= 0) Hence the while condition is satisfied and it prints '3'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 3: while(i-- >= 0) here i = 3, this statement becomes while(3-- >= 0) Hence the while condition is satisfied and it prints '2'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 4: while(i-- >= 0) here i = 2, this statement becomes while(2-- >= 0) Hence the while condition is satisfied and it prints '1'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 5: while(i-- >= 0) here i = 1, this statement becomes while(1-- >= 0) Hence the while condition is satisfied and it prints '0'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 6: while(i-- >= 0) here i = 0, this statement becomes while(0-- >= 0) Hence the while
  • 222. 221 condition is satisfied and it prints '-1'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 7:while(i-- >= 0) here i = -1, this statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits. The output of first while loop is 4,3,2,1,0,-1 Step 2: Then the value of variable i is initialized to '5' Then it prints a new line character(n). See the above Loop 1 to Loop 7 . The output of second while loop is 4,3,2,1,0,-1 Step 3: The third while loop, while(i-- >= 0) here i = -1(because the variable 'i' is decremented to '-1' by previous while loop and it never initialized.). This statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits. Hence the output of the program is 4,3,2,1,0,-1 4,3,2,1,0,-1 Q. No. 207 Category : Control Statements What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { char j=1; clrscr(); while(j < 5) { printf("%d, ", j); j = j+1; } printf("n"); getch(); return 0; }
  • 223. 222 Output : Explanation : Internally, char data type stores ASCII code of a character assigned to it. Hence any arithmetic operation is allowed on it. Thus, incrementing the char variable makes it point to the next alphabet in the character sequence. printf () statement displays the ASCII code of the character j. Q. No. 208 Category : Operators What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { int x, y, z; x=y=z=1; z = ++x || ++y && ++z; clrscr(); printf("x=%d, y=%d, z=%dn", x, y, z); getch(); return 0; } Output : Explanation : Step 1: x=y=z=1; here the variables x ,y, z are initialized to value '1'.
  • 224. 223 Step 2: z = ++x || ++y && ++z; becomes z = ( (++x) || (++y && ++z) ). Here ++x becomes 2. So there is no need to check the other side because ||(Logical OR) condition is satisfied. (z = (2 || ++y && ++z)). There is no need to process ++y && ++z. Hence it returns '1'. So the value of variable z is '1' Step 3: printf("x=%d, y=%d, z=%dn", x, y, z); It prints "x=2, y=1, z=1". here x is incremented in previous step. y and z are not incremented. Q. No. 209 Category : Arrays What will be output if you compile and execute the following c code? #include <stdio.h> #include <conio.h> void main() { int a[2][2]={100,2,3,4}; clrscr(); printf("%d %d", a[3], *(*(a+1)+1)); getch(); } Output : Explanation : a is declared to be a 2D array of size 2 x 2. There are two ways in which the array a can be initialized. Method 1 : int a[2][2]={100,2,3,4}; as shown above.
  • 225. 224 Method 2 : int a[2][2]={{100,2},{3,4}}; In either case, four contiguous memory locations are allocated for storing the elements of a. Irrespective of how the array is initialized, its elements can only be accessed using two dimensions. Using single dimension for retrieving the element will generate garbage as shown above. The correct version of the program is: (modified statement is shown in bold) #include <stdio.h> #include <conio.h> void main() { int a[2][2]={100,2,3,4}; clrscr(); printf("%d %d", a[1][1], *(*(a+1)+1)); getch(); } On execution of the above program, the following output is generated. Q. No. 210 Category : Data Types What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { char *str;
  • 226. 225 str = "%s"; clrscr(); printf(str, "Kn"); getch(); return 0; } Output : Explanation : The first argument, the format string of a printf() statement can be a variable in C. str is assigned a format string for printing a string. Hence the printf() statement becomes, printf(“%s”, “Kn”); and displays the string K. Q. No. 211 Category : Pointers What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { void *vp; char ch=74, *cp="SIBER"; int j=65; vp=&ch; clrscr(); printf("%c", *(char*)vp); vp=&j; printf("%c", *(int*)vp); vp=cp; printf("%s", (char*)vp+2); getch(); return0; }
  • 227. 226 Output : Explanation : vp is a void pointer pointing to the character whose ASCII code is 74 (J). Hence the first printf() statement prints the character J. In the next statement, the pointer vp is dereferenced and starts pointing integer j. The next printf() statement prints the character with the ASCII value „A‟. In the subsequent statement, the pointer vp is dereferenced again and now starts pointing to the string cp. The last printf() statement prints the characters of the string “SIBER” starting with third character up to the end of string, i.e. BER. The concatenation of three printf() statements results in a string, JABER, being output by the program. Q. No. 212 Category : Arrays What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { clrscr(); printf("%cn", 3["SIBER"]); getch(); return 0; }
  • 228. 227 Output : Explanation : 3[“SIBER”] retrieves the element at the index 3 (index starts from 0), i.e. character E. Consider the declaration: char[] s=”SIBER”; In the above declaration, s[3], 3[s] refer to the same element at the index position 3. 3[“SIBER”] is also a valid statement in C. Q. No. 213 Category : Operators What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { char str[] = "peace"; char *s = str; clrscr(); printf("%sn", s++ +3); getch(); return0; } Output :
  • 229. 228 Explanation : In the above program, the expression s++ +3 is evaluated as follows (s++) +3 Since ++ is a post-increment operator, the expression s+3 is evaluated first and then s is incremented. (s+3) points to the character „c‟ of a string “peace”. Hence the printf() statement displays c up to the end of the string i.e. ce is displayed as output. Q. No. 214 Category : Operators What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { char *p; p="hello"; clrscr(); printf("%sn", *&*&p); getch(); return 0; } Output : Explanation : In the printf() statement
  • 230. 229 printf("%sn", *&*&p); *& cancel out and the expression becomes, printf("%sn", p); which simply displays string s. Q. No. 215 Category : Pointers and Arrays What will be the output of the program assuming that the array begins at location 1002? #include<stdio.h> #include<conio.h> int main() { int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2}, {2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} }; clrscr(); printf("%u, %u, %u, %dn", a, *a, **a, ***a); getch(); return 0; } Output : 1002, 1002, 1002, 1 Explanation : In the above program, a is a 3D array of dimensions 2 x 3 x 4. The 24 elements of array a are stored in contiguous memory locations. ***a is an array of integer pointers of size 24. **a is an array of integer pointers of size 12, *a is an array of integer pointers of size 4. All three store the base address of the array a. Using pointer notation, the ijkth element of an array can be accessed as
  • 231. 230 * (*(*(a + i)+j)+k) Hence the syntax to access the first element of the array is ***a. Hence the output 1 is generated by the program. Q. No. 216 Category : Functions What will be the output of the program? #include<stdio.h> #include<conio.h> power(int**); int main() { int a=5, *aa; /* Address of 'a' is 1000 */ aa = &a; a = power(&aa); clrscr(); printf("%dn", a); getch(); return 0; } power(int **ptr) { int b; b = **ptr***ptr; return (b); } Output : Explanation : The snapshot of memory after storing variables is as follows: a aa 1000 2000 5 1000
  • 232. 231 The function power() is invoked with actual argument &aa. Hence, int** ptr = &aa Thus, *ptr=aa and **ptr=*aa = a. The expression, b = **ptr***ptr; is evaluated as follows: b = (**ptr) * (**ptr) = 5 * 5 = 25, the same is returned and printed by the printf() statement in the main() function. Q. No. 217 Category : Pointers What will be the output of the program? #include<stdio.h> #include<conio.h> int main() { char str1[] = "siber"; char str2[] = "MCA"; char *s1 = str1, *s2=str2; clrscr(); while(*s1++ = *s2++) printf("%s", str1); printf("n"); getch(); return 0; } Output : Explanation : In the above program, str1 and str2 point to the string “siber” and “MCA”, respectively and the character pointers s1 and s2 are initialized with str1 and str2, respectively as shown below:
  • 233. 232 S i b e r 0 str1 s1 M C A 0 str2 s2 In the first iteration of the loop, the content of the memory location pointed by s1 is updated by the content of the memory location pointed by s2. After this, the string s1 becomes, M i b e r 0 str1 s1 Hence the string Miber gets displayed and both the pointers s1 and s2 get incremented. In the second iteration of the loop, the content of the memory location pointed by s1 is updated by the content of the memory location pointed by s2. After this, the string s1 becomes, M C b e r 0 str1 s1 M C A 0 str2 s2 Hence the string MCber gets displayed and both the pointers s1 and s2 get incremented. Repeating the same argument, MCAer gets displayed in the third iteration of the loop and pointer s2 points to „0‟ and *s2 („0‟) assigned to *s1 and the while loop terminates. Hence the program generates the output, MiberMCberMCAer Q. No. 218 Category : I/O What will be the output of the program?
  • 234. 233 #include<stdio.h> #include<string.h> #include <conio.h> int main() { int x=10; clrscr(); printf("n",x); getch(); return 0; } Output : No Output Explanation : Since the printf() statement contains only n in the format string and since %d is missing, the value of x will not be displayed. Q. No. 219 Category : Pointers What will be the output of the program? #include<stdio.h> #include<string.h> #include <conio.h> int main() { int i, n; char *x="SIBER"; n = strlen(x); *x = x[n]; clrscr(); for(i=0; i<=n; i++) {
  • 235. 234 printf("%s ", x); x++; } printf("n", x); getch(); return 0; } Output : Explanation : In the above program, x is a pointer to a string “SIBER” as shown below: S I B E R 0 x The statement *x = x[n]; places „0‟ in the memory location pointed by x as shown below: 0 I B E R 0 x In the first iteration of for loop, the string x is displayed. The characters of x are displayed till „0‟ character is encountered. Since x points to „0‟ character, in the first iteration nothing is displayed. Then, the pointer x gets incremented by 1 as shown below: 0 I B E R 0
  • 236. 235 x Hence in the second iteration of the for loop, x is pointing to the character „I‟. The characters starting with „I‟ are displayed till „0‟ character is encountered. Hence IBER gets displayed in the second iteration of the for loop. Again the pointer x gets incremented by 1 as shown below: 0 I B E R 0 x Third iteration of for loop displays, BER and increments the pointer so that it points to the next memory location. Fourth iteration of for loop displays, ER and increments the pointer so that it points to the next memory location and finally, fifth iteration of for loop displays, R and increments the pointer so that it points to the next memory location. The last printf() statement only displays a new line since no format string is specified. Q. No. 220 Category : Operators What will be output if you compile and execute the following c code? #include<stdio.h> #include <conio.h> int main() { char *str; str = "%dn"; str++; *str='c'; str--; clrscr(); printf(str,65);
  • 237. 236 getch(); return 0; } Output : Explanation : In the above program, str is a character array pointing to the first element of a string “%dn” as shown below: % d n str str is incremented and the content of the memory location pointed by it is replaced with „c‟ as shown below: % c n str Next, the pointer str is decremented, so that it now points to the first character of the string “%cn” and the printf(str,65); statement becomes printf(“%cn”,65); which displays character „A‟ on the console. Q. No. 221 Category : Functions What will be the output of the program? #include<stdio.h> #include<conio.h>
  • 238. 237 void change(int*, int); int main() { int i, a[] = {2, 4, 6, 8, 10}; change(a, 5); clrscr(); for(i=0; i<=4; i++) printf("%d, ", a[i]); getch(); return 0; } void change(int *b, int n) { int i; for(i=0; i<n; i++) *(b+1) = *(b+i)+5; } Output : Explanation : The base address of the array a is passed as the first argument to the change() method. The change() method iterates through every element of the array and modifies the second element with the sum of 10 (last element of the array) and 5. Hence the array after invoking change() method is 2 15 6 8 10 The same is printed in the main() method. Q. No. 222 Category : Pointers and Arrays What will be the output of the program? #include<stdio.h> #include<conio.h>
  • 239. 238 int main() { static char *s[] = {"black", "white", "pink", "violet"}; char **ptr[] = {s+3, s+2, s+1, s}, ***p; p = ptr; ++p; clrscr(); printf("%s", **p+1); getch(); return 0; } Output : Explanation : In the above program, s points to the array of strings {"black", "white", "pink", "violet"}. ptr points to the array of strings in the reverse order. p is initialized with ptr, hence points to the first element of ptr. In the next statement, p is incremented so that now it points to the string “pink”. The next printf() statement advances the pointer by one character and prints the characters of the string “pink” from second character to the end of character. Hence the program generates “ink” as output. Q. No. 223 Category : Operators What will be the output of the program? #include<stdio.h> #include<conio.h> void main() { int i=5; clrscr(); printf("%d%d%d%d%d",--i,i--,++i,i++,i); getch(); }
  • 240. 239 Output : Explanation : The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result. The expression printf("%d%d%d%d%d%d",--i,i--,++i,i++,i); is evaluated as follows: Evaluation --i i-- ++i i++ i 5 7 7 5 5 Display Q. No. 224 Category : Operators What will be the output of the program? #include <stdio.h> #include <conio.h> void main() { char *p="hai friends",*p1; p1=p; clrscr(); while(*p!='0') ++*p++; printf("%s %s",p,p1); getch(); }
  • 241. 240 Output : Explanation : In the given program, p is a character pointer pointing to the first character of the string “hai friends”. p1 is initialized with p. Hence both p and p1 point to the same memory location. The statement ++*p++ is parsed as follows: i. *p is computed ii. ++*p, *p is incremented iii. p++ is computed, p advances by 1 byte and points to the next memory location The above three statements constitute a while loop. The loop is executed till the null character, „0‟ is encountered. In the first iteration of the loop, i. *p = h ii. ++*p = i iii. p++ i a i f r i e n d S 0 p1 p After second iteration i b i f r i e n d S 0 p1 p
  • 242. 241 and so on. After the last iteration, i b j ! g s j f o e t 0 p1 p Now p1=”ibj!gsjfoet” and p=”0” Thus, p does not print anything while p1 prints ibj!gsjfoet Hence the given program generates the following output. ibj!gsjfoet Q. No. 225 Category : Built-in Functions What will be the output of the program? #include <conio.h> void main() { clrscr(); } clrscr(); Output : No output/error Explanation : The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is a function declaration (because it is not inside any function). Q. No. 226 Category : Abstract Data Types What will be the output of the program? #include <stdio.h> #include <conio.h> enum colors {BLACK,BLUE,GREEN}
  • 243. 242 void main() { clrscr(); printf("%d..%d..%d",BLACK,BLUE,GREEN); getch(); return(1); } Output : 0..1..2 Explanation : enum assigns numbers starting from 0, if not explicitly defined. Q. No. 227 Category : Operators What will be the output of the program? #include <stdio.h> #include<conio.h> void main() { int i=5; clrscr(); printf("%d",i+++++i); Line 8 getch(); } Output : Compiler Error Explanation : The expression i+++++i is parsed as i ++ ++ + i which is an illegal combination of operators.
  • 244. 243 Q. No. 228 Category : Control Statements Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); } } Output : Runtime error: Abnormal program termination. assert failed (i<5), , Explanation : asserts are used during debugging to make sure that certain conditions are satisfied. If assertion fails, the program will terminate reporting the same. After debugging use, #undef NDEBUG and this will disable all the assertions from the source code. Assertion is a good debugging tool to make use of. Q. No. 229 Category : Operators Predict the output or error(s) for the following:
  • 245. 244 #include <stdio.h> #include <conio.h> void main() { char not; not=!2; clrscr(); printf("%d",not); getch(); } Output : Explanation : ! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0. Q. No. 230 Category : Preprocessors Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> #define TRUE 1 #define NULL 0 #define FALSE -1 void main() { clrscr(); if(NULL) puts("NULL"); else if(FALSE) puts("TRUE");
  • 246. 245 else puts("FALSE"); getch(); } Output : Explanation : The input program to the compiler after processing by the preprocessor is, void main() { if(0) puts("NULL"); else if(-1) puts("TRUE"); else puts("FALSE"); } Preprocessor doesn't replace the values given inside the double quotes. The check by if condition is boolean value false so it goes to else. In second if -1 is boolean value true, hence "TRUE" is printed. Q. No. 231 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int y; scanf("%d",&y); // input given is 2000 clrscr();
  • 247. 246 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); getch(); } Output : Explanation : An ordinary program to check if leap year or not. Q. No. 232 Category : Operators Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=-1; -i; clrscr(); printf("i = %d, -i = %d n",i,-i); getch(); } Output :
  • 248. 247 Explanation : -i is executed and this execution doesn't affect the value of i. In printf first you just print the value of i. After that the value of the expression -i = -(-1) is printed. Q. No. 233 Category : Variable Declaration Issues In the following code, the p2 is Integer Pointer or Integer? typedef int *ptr; ptr p1, p2; A. Integer B. Integer pointer C. Error in declaration D. None of above Explanation : The statement ptr p1,p2; is equivalent to int* p1,p2; Hence p2 is an integer pointer. To clarify the issue consider the following program: #include <stdio.h> #include <conio.h> void main() { typedef int *ptr; ptr p1, p2; int x=10; p2=&x; clrscr(); printf("%d",*p2); getch(); } On execution of the above program, the following output is generated.
  • 249. 248 In the above program, both p1 and p2 are integer pointers. The address of x is assigned to p2, p2 is dereferenced and printed in the next printf() statement. Hence the program displays 10 as output. Q. No. 234 Category : Macros Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> #define x 5+2 void main() { int i; i=x*x*x; clrscr(); printf("%d",i); getch(); } Output : Explanation : The input program to the compiler after processing by the preprocessor is,
  • 250. 249 void main() { int i; i=5+2*5+2*5+2; clrscr(); printf("%d",i); getch(); } The expression i=5+2*5+2*5+2; evaluates to 27. Change the #define macro to #define x (5+2) and re-execute the program. The following output is generated: The expression now becomes, i = (5+2)*(5+2)*(5+2) = 343, hence the output. Q. No. 235 Category : Pointers Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { int i=320; char *ptr=(char *)&i;
  • 251. 250 clrscr(); printf("%d",*ptr); getch(); } Output : Explanation : Since the size of the int data type is 2 bytes while the char pointer can point 1 bytes at a time, initially a char pointer points to only the first byte of a 2 byte integer as shown in the following Figure. Memory Representation of int i = 320 (256+64) First Bye Second Byte 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 *ptr is the content of the first byte i.e. 01000000 whose decimal equivalent is 64. Hence the program generates the output 64. Increment the pointer ptr as shown in the following program and re-execute it. (The new statement added is shown in bold). #include <stdio.h> #include <conio.h> ptr Sign bit
  • 252. 251 void main() { int i=320; char *ptr=(char *)&i; ptr++; clrscr(); printf("%d",*ptr); getch(); } On execution of the above program, the following output is generated. The pointer, ptr, now points to the second byte of the integer 320, 00000001, whose decimal equivalent is 1. Q. No. 236 Category : Data Types Predict the output or error(s) for the following: #include <stdio.h> #include <conio.h> void main() { char c=125; c=c+10; clrscr(); printf("%d",c); getch(); }
  • 253. 252 Output : Explanation : The range of the char data type is 127 to -128. The char data type shows cyclic properties, i.e. if the char variable is increased beyond its maximum value, then it attains the minimum value of - 128. Similarly, if the char variable is decreased beyond its minimum value, then it attains a maximum value of 127, in a cyclic order as shown below: Hence the variable c is incremented as shown in the following table: 125+1=126 125 + 2 =127 125 + 3 = -128 125 + 4 = -127 125 + 5 = -126 125 + 6 = -125 125 + 7 = -124 125 + 8 = -123 125 + 9 = -122 125 + 10 = -121 Hence the given program generates the output -121. Consider the following program: #include <stdio.h> #include <conio.h> Char 127 126 125 -128 -126 -127 0 -1 1 2
  • 254. 253 void main() { char c=127; c++; clrscr(); printf("%d",c); getch(); } On execution of the above program, the following output is generated. Modify the above program as shown below and re-execute it. #include <stdio.h> #include <conio.h> void main() { char c=-128; c--; clrscr(); printf("%d",c); getch(); } On execution of the above program, the following output is generated. Q. No. 237 Category : Data Types What will be output if you will compile and execute the following ccode?
  • 255. 254 #include <stdio.h> #include <conio.h> void main() { float a=5.2; clrscr(); if(a==5.2) printf("Equal"); else if(a<5.2) printf("Less than"); else printf("Greater than"); getch(); } Output : Explanation : In C, floating point literal is of type double and the size of a double data type is 8 bytes while the size of a float variable is 4 bytes. In memory, float and double data types are stored completely in a different manner. In the given program, the variable a is of type float while 5.2 is a double constant. The double constant , 5.2 is stored in memory as 101.00 11001100 11001100 1100110011001100 11001100 11001100 while the variable a is stored in memory as 101. 00110 01100110 01100110 It is clear that the variable a is less than the double constant 5.2. Since 5.2 is a recurring float number its value of different for float and double. Since a is less than 5.2, the given program generates the output “Less than”.
  • 256. 255 Consider the following program: include <stdio.h> #include <conio.h> void main() { float f=5.2; clrscr(); printf("%.10f %.10f",f,5.2); getch(); } On execution of the above program, the following output is generated. As seen from the output generated, floating point 5.2 is always less that double 5.2. Q. No. 238 Category : Operators What will be output if you will compile and execute the following c code? #include <stdio.h> #include <conio.h> void main() { int i=4,x; x=++i + ++i + ++i; clrscr(); printf("%d",x); getch(); }
  • 257. 256 Output : Explanation : In the expression ++a, ++ is the pre-increment operator. In any mathematical expression, pre- increment operator is evaluated first up to the break point which then starts assigning the final value to all the variables. The expression x=++i + ++i + ++i; is evaluated in two steps given below: Step 1 : Increment the variable i up to the break point. x=++i + ++i + ++i; 5 6 7 Step 2 : Start assigning the final value of i (=7) to all the variables i in the expression. Hence x = 7 + 7 + 7 =21 Hence the program generates the output 21. Change the expression to the one shown below and re-execute the program. x=++i + i++ + ++i; On execution of the program, following output is generated.
  • 258. 257 The expression is now evaluated as Step 1: x=++i + i++ + ++i; 5 5 6 Step 2 : x = 6 + 6 + 6 = 18. Hence the program generates the output 18. After the evaluation of the statement the value of i becomes 7. Q. No. 239 Category : Operators What will be output if you will compile and execute the following c code? #include <stdio.h> #include <conio.h> void main() { int a=10; clrscr(); printf("%d %d %d",a,a++,++a); getch(); } Output : Explanation : In C, printf function follows cdecl parameter passing scheme in which parameters are passed from right to left. Hence the expression printf("%d %d %d",a,a++,++a); is evaluated as follows:
  • 259. 258 Before the function call, the value of a is 10. The right most parameter is ++a, hence a is incremented to 11. Hence the value of a is 11. The next parameter is a++, hence the second parameter is 11 and after that the value of a is incremented to 12. As a result third parameter (from right) becomes 12. On evaluation of parameters, printf() statement becomes, printf("%d %d %d",12,11,11); Hence the program generates the output 12, 11, 11. Change the printf() statement to printf("%d %d %d",a,++a,a++); and re-execute the program. The following output is generated. In the modified printf() statement, the rightmost parameter is 10, then a is incremented to 11, in the next parameter a is incremented to 12, then the same is used in the third parameter The modified printf statement is equivalent to printf("%d %d %d",12,12,10); Hence the program generates the output 12 12 10. Q. No. 240 Category : Operators What will be output if you will compile and execute the following c code? #include <stdio.h> #include <conio.h>
  • 260. 259 void main() { clrscr(); printf("%d",sizeof(5.2)); getch(); } Output : Explanation : The default type of the floating point constant is double, i.e. the floating point literal is of type double. Hence 5.2 is double constant and hence its size is 8 bytes. In order to render the type of floating point constant as float, suffix either „f‟ or „F‟ to the constant as shown below: 5.2f To illustrate this modify the program as shown below (change is shown in bold) and re-execute it. #include <stdio.h> #include <conio.h> void main() { clrscr(); printf("%d",sizeof(5.2f)); getch(); } On execution of the above program, the following output is generated.
  • 261. 260 As evident from the output generated, the type of 5.2f is float and hence the size is 4 bytes. Q. No. 241 Category : Macros What will be output if you will compile and execute the following c code? #include <stdio.h> #include <conio.h> #define call(x,y) x##y void main() { int x=5,y=10,xy=20; clrscr(); printf("%d",xy+call(x,y)); getch(); } Output : Explanation : ## is a concatenation operator in preprocessor directive. It concatenates its two operands as shown below: x##y = xy On preprocessing, the following pseudo code is generated before the actual compilation. call(x,y) is replaced with xy.
  • 262. 261 void main() { int x=5,y=10,xy=20; clrscr(); printf("%d",xy+xy); getch(); } Hence the above program generates the output 40. Q. No. 242 Category : Arrays What will be output if you will compile and execute the following c code? #include <stdio.h> #include <conio.h> void main() { int array[]={10,20,30,40}; clrscr(); printf("%d",-2[array]); getch(); } Output : Explanation : In C, the different ways of accessing the ith element of an array, a are, a[i] = *(a+i) = *(i+a) = i[a]. Hence the second element of array, array can be accessed as 2[array] and – sign is a unary – operator. Hence -2[array] = -30, which is generated as an output of a program.
  • 263. 262 Q. No. 243 Category : Storage Classes What will be output if you will compile and execute the following c code? #include <stdio.h> #include <conio.h> void main() { int i=10; static int x=i; Line 7 clrscr(); if(x==i) printf("Equal"); else if(x>i) printf("Greater than"); else printf("Less than"); getch(); } Output : Compiler Error Explanation : Since static variables are load time entities, i.e. the memory for the static variables is allocated when the program is loaded into the memory while auto variables are run time entities, i.e. the memory for the auto variables is allocated at the program execution time. Hence, the runtime variables are not available to the load time variables. Hence, we cannot initialize any load time variable with the runtime variable. In the above program, i is a run time variable while x is a load time variable. Hence we arrive at the following rules:
  • 264. 263 Rule 1 : A runtime variable can be initialized with another runtime variable. Rule 2 : A load time variable can not be initialized with another load time variable. Rule 3 : A runtime variable can be initialized with load time variable. Rule 4 : A load time variable can be initialized with runtime variable. Consider the following program. #include <stdio.h> #include <conio.h> void main() { static int i=10; int x=i; clrscr(); printf("x : %d",x); getch(); } On execution of the above program, the following output is generated. Since s is a runtime variable, it can be initialized with the load time variable i. Q. No. 244 Category : Operators What will be output if you will compile and execute the following c code? #include <stdio.h> #include <conio.h> #define max 5;
  • 265. 264 void main() { int i=0; i=max++; Line 8 clrscr(); printf("%d",i++); getch(); } Output : Compiler Error Explanation : On preprocessing the macro, the following pseudo code is generated. The variable max is replaced with 5. void main() { int i=0; i=5++; clrscr(); printf("%d",i++); getch(); } As seen, the macro constant Max has been replaced with 5. Since it is illegal to increment the constant number, the compiler will generate an error message, „Expression Syntax‟. Q. No. 245 Category : Pointers and Arrays What will be output if you compile and execute the following c code? #include <stdio.h> #include <conio.h>
  • 266. 265 int main(void) { int arr[] = {10, 20}; int *p = arr; ++*p; clrscr(); printf("*p = %d", *p); getch(); return 0; } Output : Explanation : Precedence of prefix ++ and * is same. Associativity of both is right to left. Hence the expression ++*p is equivalent to ++(*p) Initially p is pointing to the element 10 or array, arr. Hence *p is equal to 10 and ++(*p) is equal to 11. Q. No. 246 Category : Pointers and Arrays What will be output if you compile and execute the following c code? #include <stdio.h> #include <conio.h> int main(void) { int arr[] = {10, 20}; int *p = arr; *p++; clrscr();
  • 267. 266 printf("*p = %d", *p); getch(); return 0; } Output : Explanation : Precedence of postfix ++ is higher than both * and prefix ++. Hence the expression *p++ is equivalent to *(p++) Initially p is pointing to the element 10 or array, arr. After the execution of p++, p will point to element 20 of array, arr. Hence dereferencing it, the content of the memory location, 20 is returned. Hence *(p++) is equal to 20. Q. No. 247 Category : Pointers and Arrays What will be output if you compile and execute the following c code? #include <stdio.h> #include <conio.h> int main(void) { int arr[] = {10, 20}; int *p = arr; *++p; clrscr(); printf("*p = %d", *p); getch(); return 0; }
  • 268. 267 Output : Explanation : The expression *++p has two operators of same precedence, so compiler looks for associativity. Associativity of operators is right to left. Therefore the expression is treated as *(++p). Therefore the output of second program is “arr[0] = 10, arr[1] = 20, *p = 20. Q. No. 248 Category : Variable Declaration Issues What will be output if you compile and execute the following c code? #include<stdio.h> #include <conio.h> int main() { int a = 1, 2, 3; Line 6 clrscr(); printf("%d", a); getch(); return 0; } Output :
  • 269. 268 Explanation : In a C/C++ program, comma is used in two contexts: (1) A separator (2) An Operator. In the above program, comma works just as a separator, hence the program generates a compiler error. Q. No. 249 Category : Operators What will be output if you compile and execute the following c code? #include<stdio.h> #include <conio.h> int main() { int a; a = 1, 2, 3; clrscr(); printf("%d", a); getch(); return 0; } Output : Explanation : In the above program, comma works as an operator. Precedence of comma operator is least in operator precedence table. So the assignment operator takes precedence over comma and the expression “a = 1, 2, 3” becomes equivalent to “(a = 1), 2, 3” Q. No. 250 Category : Operators What will be output if you compile and execute the following c code? #include<stdio.h>
  • 270. 269 #include <conio.h> int main() { int a; a = (1, 2, 3); clrscr(); printf("%d", a); getch(); return 0; } Output : Explanation : In the above program, brackets are used so comma operator is executed first and we get the output as 3. Q. No. 251 Category : Operators What will be output if you compile and execute the following c code? #include <stdio.h> #include <conio.h> int main() { int a = 10, b = 20, c = 30; clrscr(); if (c > b > a) printf("TRUE"); else printf("FALSE"); getch(); return 0; }
  • 271. 270 Output : Explanation : (c > b > a) is treated as ((c > b) > a), associativity of '>' is left to right. Therefore the value becomes ((30 > 20) > 10) which becomes (1 > 20) References: 1. https://www.indiabix.com/c-programming 2. http://www.c4learn.com/c-programming 3. http://www.includehelp.com/c-programming-aptitude-questions-and-answers.aspx 4. http://www.sanfoundry.com/c-programming-examples/ 5. http://www.c-program-example.com/p/c-aptitude.html 6. http://www.cquestions.com/2012/05/aptitude-questions-and-answers-in-c.html 7. http://blog.oureducation.in/c-c-aptitude-questions/ 8. http://aptitude.students3k.com/technical-aptitude-questions-in-c-programming-pdf/ 9. http://www.aucev.edu.in/placementcell/Materials/App1.pdf 10. http://techpreparation.com/aptitute-questions/c-cpp-aptitude- question1.htm#.WTt3mDe1u2w 11. http://www.geeksforgeeks.org/c/ 12. https://www.2braces.com/c-programming View publication stats View publication stats