0% found this document useful (0 votes)
5 views12 pages

C Imp

The document discusses the use of the 'bits/stdc++.h' header file in C++, highlighting its advantages for programming contests by saving time on includes, but also noting its disadvantages such as non-portability and increased compilation time. It provides examples of common pitfalls with data types like 'char', 'bool', 'short', and 'unsigned short', demonstrating how exceeding their ranges can lead to infinite loops. Additionally, it covers the use of getline for reading input, nested if statements, and various output operations in C++, along with explanations for specific code examples.

Uploaded by

cskb96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

C Imp

The document discusses the use of the 'bits/stdc++.h' header file in C++, highlighting its advantages for programming contests by saving time on includes, but also noting its disadvantages such as non-portability and increased compilation time. It provides examples of common pitfalls with data types like 'char', 'bool', 'short', and 'unsigned short', demonstrating how exceeding their ranges can lead to infinite loops. Additionally, it covers the use of getline for reading input, nested if statements, and various output operations in C++, along with explanations for specific code examples.

Uploaded by

cskb96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

C++ imp stuff

///

It is basically a header file that includes every standard library. In programming


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.
In programming contests, people do focus more on finding the algorithm to solve a
problem than on software engineering. From, software engineering perspective, it is
a good idea to minimize the include. If you use it actually includes a lot of files, which
your program may not need, thus increases both compile time and program size
unnecessarily.

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.
///

// C++ program to demonstrate

// the problem with 'char'

#include <iostream>

using namespace std;


int main()

for (char a = 0; a <= 225; a++)

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.

// C++ program to demonstrate

// the problem with 'bool'

#include <iostream>

using namespace std;

int main()

// declaring Boolean

// variable with true value

bool a = true;

for (a = 1; a <= 5; a++)

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.

// C++ program to demonstrate

// the problem with 'short'

#include <iostream>

using namespace std;

int main()

// declaring short variable

short a;

for (a = 32767; a < 32770; a++)

cout << a << "\n";

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.

// C++ program to demonstrate

// the problem with 'unsigned short'

#include <iostream>

using namespace std;

int main()

unsigned short a;
for (a = 65532; a < 65536; a++)

cout << a << "\n";

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.

In C++, pre-increment (or pre-decrement) can be used as l-value, but post-increment


(or post-decrement) can not be used as l-value.

For example, following program prints a = 20 (++a is used as l-value)


// CPP program to illustrate
// Pre-increment (or pre-decrement)
#include <cstdio>

int main()
{
int a = 10;

++a = 20; // works

printf("a = %d", a);


getchar();
return 0;
}

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)

// CPP program to illustrate

// Post-increment (or post-decrement)

#include <cstdio>

int main()

{
int a = 10;

a++ = 20; // error

printf("a = %d", a);

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 &ref = ++a; // valid


int &ref = a++; // invalid
Whereas if you recall how a++ works, it doesn't immediately increment the value it
holds. For brevity, you can think of it as getting incremented in the next statement.
So what basically happens is that a++ returns an rvalue, which is basically just a
value like the value of an expression which is not stored. You can think of a++ =
20; as follows after being processed:

int a = 10;

// On compilation, a++ is replaced by the value of a which is an rvalue:


10 = 20; // Invalid

// 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

// A simple C++ program to show working of getline

#include <iostream>

#include <cstring>

using namespace std;

int main()
{

string str;

int t = 4;

while (t--)

// Read a line from standard input in str

getline(cin, str);

cout << str << " : newline" << endl;

return 0;

Sample Input :
This

is

Geeks

for

s expected output is:


This : newline

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.

The code can be modified to exclude such blank lines.

Modified code:
// A simple C++ program that uses getline to read

// input with blank lines

#include <iostream>

#include <cstring>

using namespace std;

int main()

string str;

int t = 4;

while (t--)

getline(cin, str);

// Keep reading a new line while there is

// a blank line

while (str.length()==0 )

getline(cin, str);

cout << str << " : newline" << endl;

return 0;
}

Input:

This

is

Geeks

for
Output:

This : newline

is : newline

Geeks : newline

for : newline

// C++ program to illustrate nested-if statement

int main()

int i = 10;

if (i == 10)

// First if statement

if (i < 15)

cout<<"i is smaller than 15";

// Nested - if statement

// Will only be executed if statement above

// it is true
if (i < 12)

cout<<"i is smaller than 12 too";

else

cout<<"i is greater than 15";

return 0;

//

Output of the below program in C++

#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.

Indicators available in C++


Explanation: There are three indicators are available in C++. They are
Errorindicator, End-Of-File indicator and Position indicator.
//
Output of the below program

#include < iostream >


using namespace std;

int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i + 4;

return 0;
}

We are not allowed to do addition operation on cin. Error

How many groups of output of operation are there in C++?


Explanation

There are two groups of output operation in C++. They are formatted output and
unformatted output.

Correct choice for the below program

#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;

int res1 = c ^ (x ^ y);


int res2 = z ^ (a ^ b);

cout << (res1 | res2);

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.

What will be the result of the below program in C++

#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.

What is the output of the program in C++

#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.

You might also like