C Imp
C Imp
///
Disadvantages of bits/stdc++
bits/stdc++.h is not a standard header file of GNU C++ library. So, if you try to
compile your code with some compiler other than GCC it might fail; e.g.
MSVC do not have this header.
Using it would include a lot of unnecessary stuff and increases compilation
time.
This header file is not part of the C++ standard and is therefore, non-portable,
and should be avoided.
Moreover, even if there were some catch-all header in the standard, you
would want to avoid it in lieu of specific headers, since the compiler has to
actually read in and parse every included header (including recursively
included headers) every single time that translation unit is compiled.
Advantages of bits/stdc++
In contests, using this file is a good idea, when you want to reduce the time
wasted in doing chores; especially when your rank is time sensitive.
This also reduces all the chores of writing all the necessary header files.
You don’t have to remember all the STL of GNU C++ for every function you
use.
So, the user can either use it and save the time of writing every include or save the
compilation time by not using it and writing necessary header files.
///
#include <iostream>
cout << a;
return 0;
Will this code print 'a' till it becomes 226? Well the answer is indefinite loop, because here 'a'
is declared as a char and its valid range is -128 to +127. When 'a' become 128 through a++,
the range is exceeded and as a result the first number from negative side of the range (i.e. -
128) gets assigned to a. Hence the condition "a <= 225" is satisfied and control remains
within the loop.
#include <iostream>
int main()
// declaring Boolean
bool a = true;
cout << a;
return 0;
This code will print '1' infinite time because here 'a' is declared as 'bool' and it's valid range is
0 to 1. And for a Boolean variable anything else than 0 is 1 (or true). When 'a' tries to
become 2 (through a++), 1 gets assigned to 'a'. The condition a<=5 is satisfied and the
control remains with in the loop. See this for Bool data type.
#include <iostream>
int main()
short a;
return 0;
Will this code print 'a' till it becomes 32770? Well the answer is indefinite loop, because here
'a' is declared as a short and its valid range is -32768 to +32767. When 'a' tries to become
32768 through a++, the range is exceeded and as a result the first number from negative
side of the range(i.e. -32768) gets assigned to a. Hence the condition "a < 32770" is
satisfied and control remains within the loop.
#include <iostream>
int main()
unsigned short a;
for (a = 65532; a < 65536; a++)
return 0;
Will this code print 'a' till it becomes 65536? Well the answer is indefinite loop, because here
'a' is declared as a short and its valid range is 0 to +65535. When 'a' tries to become 65536
through a++, the range is exceeded and as a result the first number from the range(i.e. 0)
gets assigned to a. Hence the condition "a < 65536" is satisfied and control remains within
the loop.
int main()
{
int a = 10;
Op/a = 20
The above program works whereas the following program fails in compilation with
error "non-lvalue in assignment" (a++ is used as l-value)
#include <cstdio>
int main()
{
int a = 10;
getchar();
return 0;
}
prog.cpp: In function 'int main()':
prog.cpp:6:5: error: lvalue required as left operand of assignment
a++ = 20; // error
^
How ++a is different from a++ as lvalue?
It is because ++a returns an lvalue, which is basically a reference to the variable to
which we can further assign — just like an ordinary variable. It could also be
assigned to a reference as follows:
int a = 10;
// Value of a is incremented
a = a + 1;
That should help to understand why a++ = 20; won't work.
In C++, if we need to read few sentences from a stream, the generally preferred way is to
use getline() function. It can read till it encounters newline or sees a delimiter provided by
user.
Here is a sample program in c++ that reads four sentences and displays them with " :
newline" at the end
#include <iostream>
#include <cstring>
int main()
{
string str;
int t = 4;
while (t--)
getline(cin, str);
return 0;
Sample Input :
This
is
Geeks
for
is : newline
Geeks : newline
for : newline
The above input and output look good, there may be problems when input has blank
lines in between.
Sample Input :
This
is
Geeks
for
Output:
This : newline
: newline
is : newline
: newline
It doesn't print the last 2 lines. The reason is that getline() reads till enter is
encountered even if no characters are read. So even if there is nothing in the third
line, getline() considers it as a single line. Further observe the problem in the second
line.
Modified code:
// A simple C++ program that uses getline to read
#include <iostream>
#include <cstring>
int main()
string str;
int t = 4;
while (t--)
getline(cin, str);
// a blank line
while (str.length()==0 )
getline(cin, str);
return 0;
}
Input:
This
is
Geeks
for
Output:
This : newline
is : newline
Geeks : newline
for : newline
int main()
int i = 10;
if (i == 10)
// First if statement
if (i < 15)
// Nested - if statement
// it is true
if (i < 12)
else
return 0;
//
#include <iostream>
using namespace std;
int main()
{
if (sizeof(int) > -1)
cout << "Yes";
else
cout << "No";
return 0;
}
Op/ no
Explanation
In C++, when an integer value is compared with an unsigned it, the int is promoted to
unsigned. Negative numbers are stored in 2’s complement form and unsigned value
of the 2’s complement form is much higher than the sizeof int.
Which will be used with physical devices to interact from C++ program?
Explanation
C++ library uses streams to operate with physical devices such as Keyboards,
Printers, Terminals or with any other type of files supported by the system.
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i + 4;
return 0;
}
There are two groups of output operation in C++. They are formatted output and
unformatted output.
#include <iostream>
using namespace std;
int main()
{
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = a + b * c / d;
cout << e << endl ;
return 0;
Ans 50
Precedence
Predict the correct choice for the below XOR and OR operation in the program
#include <iostream>
using namespace std;
int main()
{
int x = 3, y = 5, z = 6;
int a = 2, b = 4, c = 7;
return 0;
}
Ans 1
Explanation
C ^ (x ^ y) and z ^ (a ^ b) both are evaluated and the result is stored in res1 and res2
as 1 and 0 and at last OR is performed on them.
#include <iostream>
using namespace std;
int main()
{
int x = 2, y = 3;
x = y << x;
y = x << y;
cout << (x >> 1) << " " << (y >> 1);
return 0;
}
Explanation
Value of x and y after bitwise left shift evaluation becomes 12 and 96 and after 1 bit
right shift both becomes 6 and 48.
#include <iostream>
using namespace std;
int main()
{
int i = 0;
switch (i)
{
case '0': cout << "Geeks";
break;
case '1': cout << "Quiz";
break;
default: cout << "GeeksQuiz";
}
return 0;
}
Explanation
At first look, the output of the program seems to be Geeks. But, the cases are
labeled with characters which gets converted to their ascii values 48(for 0) and 49(for
1). None of the cases is labeled with value 0. So, the control goes to the default
block and GeeksQuiz is printed.