SlideShare uma empresa Scribd logo
1 de 271
Baixar para ler offline
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);
}
}
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book
C aptitude book

Mais conteúdo relacionado

Mais procurados

Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to ProgrammingChaffey College
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Bhavin Darji
 
Fundamentals of Language Processing
Fundamentals of Language ProcessingFundamentals of Language Processing
Fundamentals of Language ProcessingHemant Sharma
 
Chap 1-language processor
Chap 1-language processorChap 1-language processor
Chap 1-language processorshindept123
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & LanguagesGaditek
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)guest91cc85
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture NotesFellowBuddy.com
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recoveryTech_MX
 
265 ge8151 problem solving and python programming - 2 marks with answers
265   ge8151 problem solving and python programming - 2 marks with answers265   ge8151 problem solving and python programming - 2 marks with answers
265 ge8151 problem solving and python programming - 2 marks with answersvithyanila
 
2.2 Demonstrate the understanding of Programming Life Cycle
2.2 Demonstrate the understanding of Programming Life Cycle2.2 Demonstrate the understanding of Programming Life Cycle
2.2 Demonstrate the understanding of Programming Life CycleFrankie Jones
 
CS152 Programming Paradigm
CS152 Programming Paradigm CS152 Programming Paradigm
CS152 Programming Paradigm Kaya Ota
 
Program Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityProgram Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityReggie Niccolo Santos
 

Mais procurados (20)

DISE - Programming Concepts
DISE - Programming ConceptsDISE - Programming Concepts
DISE - Programming Concepts
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
Introduction to compilers
Introduction to compilersIntroduction to compilers
Introduction to compilers
 
Graphical programming
Graphical programmingGraphical programming
Graphical programming
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
 
Fundamentals of Language Processing
Fundamentals of Language ProcessingFundamentals of Language Processing
Fundamentals of Language Processing
 
Chap 1-language processor
Chap 1-language processorChap 1-language processor
Chap 1-language processor
 
Lec 1 intro
Lec 1 introLec 1 intro
Lec 1 intro
 
Programming and problem solving with c++, 3rd edition
Programming and problem solving with c++, 3rd editionProgramming and problem solving with c++, 3rd edition
Programming and problem solving with c++, 3rd edition
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Chapter2
Chapter2Chapter2
Chapter2
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recovery
 
265 ge8151 problem solving and python programming - 2 marks with answers
265   ge8151 problem solving and python programming - 2 marks with answers265   ge8151 problem solving and python programming - 2 marks with answers
265 ge8151 problem solving and python programming - 2 marks with answers
 
Procedural programming
Procedural programmingProcedural programming
Procedural programming
 
2.2 Demonstrate the understanding of Programming Life Cycle
2.2 Demonstrate the understanding of Programming Life Cycle2.2 Demonstrate the understanding of Programming Life Cycle
2.2 Demonstrate the understanding of Programming Life Cycle
 
CS152 Programming Paradigm
CS152 Programming Paradigm CS152 Programming Paradigm
CS152 Programming Paradigm
 
Program Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityProgram Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State University
 
C programming part1
C programming part1C programming part1
C programming part1
 

Semelhante a C aptitude book

with C++ seventh Edition.pdf by walter Savitch
with C++ seventh Edition.pdf by walter Savitchwith C++ seventh Edition.pdf by walter Savitch
with C++ seventh Edition.pdf by walter SavitchMohsinNaushad1
 
ONLINE ACCESSThank you for purchasing a new copy of In.docx
ONLINE ACCESSThank you for purchasing a new copy of In.docxONLINE ACCESSThank you for purchasing a new copy of In.docx
ONLINE ACCESSThank you for purchasing a new copy of In.docxcherishwinsland
 
How to become a software developer
How to become a software developerHow to become a software developer
How to become a software developerEyob Lube
 
Programming of c++
Programming of c++Programming of c++
Programming of c++Ateeq Sindhu
 
Due Date 1159 p.m. EST, Sunday of Unit 7 Points 10.docx
Due Date  1159 p.m. EST, Sunday of Unit 7 Points  10.docxDue Date  1159 p.m. EST, Sunday of Unit 7 Points  10.docx
Due Date 1159 p.m. EST, Sunday of Unit 7 Points 10.docxgertrudebellgrove
 
Due Date 1159 p.m. EST, Sunday of Unit 7 Points 10.docx
 Due Date  1159 p.m. EST, Sunday of Unit 7 Points  10.docx Due Date  1159 p.m. EST, Sunday of Unit 7 Points  10.docx
Due Date 1159 p.m. EST, Sunday of Unit 7 Points 10.docxShiraPrater50
 
Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01Getachew Ganfur
 
Development of a Modular Unit of a Higher Level Framework or Tool for Basic P...
Development of a Modular Unit of a Higher Level Framework or Tool for Basic P...Development of a Modular Unit of a Higher Level Framework or Tool for Basic P...
Development of a Modular Unit of a Higher Level Framework or Tool for Basic P...TELKOMNIKA JOURNAL
 
Algorithms and Application Programming
Algorithms and Application ProgrammingAlgorithms and Application Programming
Algorithms and Application Programmingahaleemsl
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++KurdGul
 
Training report of C language
Training report of C languageTraining report of C language
Training report of C languageShashank Kapoor
 
The paper is a book review &amp; must include all of the following
The paper is a book review &amp; must include all of the followingThe paper is a book review &amp; must include all of the following
The paper is a book review &amp; must include all of the followingBHANU281672
 
Title of PresentationStudent’s nameFeel free to adjust the c.docx
Title of PresentationStudent’s nameFeel free to adjust the c.docxTitle of PresentationStudent’s nameFeel free to adjust the c.docx
Title of PresentationStudent’s nameFeel free to adjust the c.docxherthalearmont
 
Things I wished I knew while doing my tech bachelor / undergraduate
Things I wished I knew while doing my tech bachelor / undergraduateThings I wished I knew while doing my tech bachelor / undergraduate
Things I wished I knew while doing my tech bachelor / undergraduateGeshan Manandhar
 
Teaching Programming to Non-Programmers at Undergraduate Level
Teaching Programming to Non-Programmers at Undergraduate LevelTeaching Programming to Non-Programmers at Undergraduate Level
Teaching Programming to Non-Programmers at Undergraduate LevelDr. Amarjeet Singh
 
vtu data structures lab manual bcs304 pdf
vtu data structures lab manual bcs304 pdfvtu data structures lab manual bcs304 pdf
vtu data structures lab manual bcs304 pdfLPSChandana
 

Semelhante a C aptitude book (20)

with C++ seventh Edition.pdf by walter Savitch
with C++ seventh Edition.pdf by walter Savitchwith C++ seventh Edition.pdf by walter Savitch
with C++ seventh Edition.pdf by walter Savitch
 
ONLINE ACCESSThank you for purchasing a new copy of In.docx
ONLINE ACCESSThank you for purchasing a new copy of In.docxONLINE ACCESSThank you for purchasing a new copy of In.docx
ONLINE ACCESSThank you for purchasing a new copy of In.docx
 
How to become a software developer
How to become a software developerHow to become a software developer
How to become a software developer
 
Programming of c++
Programming of c++Programming of c++
Programming of c++
 
Due Date 1159 p.m. EST, Sunday of Unit 7 Points 10.docx
Due Date  1159 p.m. EST, Sunday of Unit 7 Points  10.docxDue Date  1159 p.m. EST, Sunday of Unit 7 Points  10.docx
Due Date 1159 p.m. EST, Sunday of Unit 7 Points 10.docx
 
Due Date 1159 p.m. EST, Sunday of Unit 7 Points 10.docx
 Due Date  1159 p.m. EST, Sunday of Unit 7 Points  10.docx Due Date  1159 p.m. EST, Sunday of Unit 7 Points  10.docx
Due Date 1159 p.m. EST, Sunday of Unit 7 Points 10.docx
 
Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01
 
School Management (c++)
School Management (c++) School Management (c++)
School Management (c++)
 
Development of a Modular Unit of a Higher Level Framework or Tool for Basic P...
Development of a Modular Unit of a Higher Level Framework or Tool for Basic P...Development of a Modular Unit of a Higher Level Framework or Tool for Basic P...
Development of a Modular Unit of a Higher Level Framework or Tool for Basic P...
 
Algorithms and Application Programming
Algorithms and Application ProgrammingAlgorithms and Application Programming
Algorithms and Application Programming
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++
 
Training report of C language
Training report of C languageTraining report of C language
Training report of C language
 
The paper is a book review &amp; must include all of the following
The paper is a book review &amp; must include all of the followingThe paper is a book review &amp; must include all of the following
The paper is a book review &amp; must include all of the following
 
Title of PresentationStudent’s nameFeel free to adjust the c.docx
Title of PresentationStudent’s nameFeel free to adjust the c.docxTitle of PresentationStudent’s nameFeel free to adjust the c.docx
Title of PresentationStudent’s nameFeel free to adjust the c.docx
 
Notes
NotesNotes
Notes
 
Modern c
Modern cModern c
Modern c
 
Things I wished I knew while doing my tech bachelor / undergraduate
Things I wished I knew while doing my tech bachelor / undergraduateThings I wished I knew while doing my tech bachelor / undergraduate
Things I wished I knew while doing my tech bachelor / undergraduate
 
Oose lab notes
Oose lab notesOose lab notes
Oose lab notes
 
Teaching Programming to Non-Programmers at Undergraduate Level
Teaching Programming to Non-Programmers at Undergraduate LevelTeaching Programming to Non-Programmers at Undergraduate Level
Teaching Programming to Non-Programmers at Undergraduate Level
 
vtu data structures lab manual bcs304 pdf
vtu data structures lab manual bcs304 pdfvtu data structures lab manual bcs304 pdf
vtu data structures lab manual bcs304 pdf
 

Último

办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一A SSS
 
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量sehgh15heh
 
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一2s3dgmej
 
Unlock Your Creative Potential: 7 Skills for Content Creator Evolution
Unlock Your Creative Potential: 7 Skills for Content Creator EvolutionUnlock Your Creative Potential: 7 Skills for Content Creator Evolution
Unlock Your Creative Potential: 7 Skills for Content Creator EvolutionRhazes Ghaisan
 
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改yuu sss
 
Ioannis Tzachristas Self-Presentation for MBA.pdf
Ioannis Tzachristas Self-Presentation for MBA.pdfIoannis Tzachristas Self-Presentation for MBA.pdf
Ioannis Tzachristas Self-Presentation for MBA.pdfjtzach
 
定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一z zzz
 
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一A SSS
 
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一Fs sss
 
Digital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Discovery Institute
 
Jumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3AJumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3Ajumarkdiezmo1
 
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证nhjeo1gg
 
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证diploma001
 
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书saphesg8
 
Ch. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdfCh. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdfJamalYaseenJameelOde
 
Protection of Children in context of IHL and Counter Terrorism
Protection of Children in context of IHL and  Counter TerrorismProtection of Children in context of IHL and  Counter Terrorism
Protection of Children in context of IHL and Counter TerrorismNilendra Kumar
 
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一diploma 1
 
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCRdollysharma2066
 

Último (20)

办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
 
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
 
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
 
Unlock Your Creative Potential: 7 Skills for Content Creator Evolution
Unlock Your Creative Potential: 7 Skills for Content Creator EvolutionUnlock Your Creative Potential: 7 Skills for Content Creator Evolution
Unlock Your Creative Potential: 7 Skills for Content Creator Evolution
 
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改
 
Ioannis Tzachristas Self-Presentation for MBA.pdf
Ioannis Tzachristas Self-Presentation for MBA.pdfIoannis Tzachristas Self-Presentation for MBA.pdf
Ioannis Tzachristas Self-Presentation for MBA.pdf
 
定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一
 
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
 
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
 
Digital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, India
 
Jumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3AJumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3A
 
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
 
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证
 
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档
 
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
 
Ch. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdfCh. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdf
 
Protection of Children in context of IHL and Counter Terrorism
Protection of Children in context of IHL and  Counter TerrorismProtection of Children in context of IHL and  Counter Terrorism
Protection of Children in context of IHL and Counter Terrorism
 
Young Call~Girl in Pragati Maidan New Delhi 8448380779 Full Enjoy Escort Service
Young Call~Girl in Pragati Maidan New Delhi 8448380779 Full Enjoy Escort ServiceYoung Call~Girl in Pragati Maidan New Delhi 8448380779 Full Enjoy Escort Service
Young Call~Girl in Pragati Maidan New Delhi 8448380779 Full Enjoy Escort Service
 
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一
 
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
 

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); } }