Shifting from C to C++
Published: 2020-10-15 10:56:54

This is a short guide, for those who are familiar with the syntax of C programming language and want to shift to C++, mainly for competitive programming.

<!--truncate-->

## Header Files

- In C,  you generally use the header files :
  - stdio.h
  - math.h
  - string.h

- In C++,  you generally use the header files :
  - iostream
  - cmath
  - cstring
  - Other STL libraries

- In C++, unlike C, you don’t need to include a bunch of header files. Instead you can include only one header file which will include most of the required header files :

```cpp
#include <bits/stdc++.h>
```

## Input Output Functions

- In C, the basic input output functions are scanf, printf, gets, puts.
- In C++, you use cin, cout, [getline](https://www.geeksforgeeks.org/getline-string-c/).
- For a new line, in C we use ‘\n’ in printf statement while we can use endl in cout statement in c++.
<br/>

<b>  Caution</b>: endl slows down output, so it is better to use ‘\n’.
- To input a line i.e. to input a string with spaces we use gets in C while getline in C++.
- All the functions like cin, cout etc. are defined in a standard namespace. So, instead of directly using it we usually add a namespace to avoid writing std every time. You do not need to understand much about namespace. Just remember to add this statement at the top in your C++ code :
```cpp
using namespace std;
```

## Multiline inputs

Suppose you need to take the input in the following format :

1. The first line contains an integer, a floating point number and a character.
2. The second line contains a sentence, with spaces between the words.

Now, try running these codes.

- In C, the code for this would be :
```cpp
#include <stdio.h>
// No namespace in C

int main()
{
  int a;
  float b;
  char c;
  char d[100]; // No data-type for string

  scanf("%d %f %c",&a,&b,&c);
  scanf("\n");
  gets(d);
  printf("%d %f %c %s",a,b,c,d);
  return 0;
}
```

- In C++, the code would be :
```cpp
#include <bits/stdc++.h>
using namespace std;

int main()
{
  int a;
  float b;
  char c;
  string d;

  cin>>a>>b>>c;
  gets(d);
  gets(d);
  cout<<a<<" "<<b<<" "<<c<<" "<<d;
  return 0;
}
```

- **Note** : Do note the use of two gets in cpp code (line 12) and ‘\n’ in scanf statement in C code (line 12). This is because when you press enter after taking a, b, c as input ‘\n’ gets stored in d. So, we need to input d again. In C code, we also take \n as input in scanf statement. So we don't need to use gets. If we do not use \n in scanf, then we will have to use two gets. This might be a little bit confusing but you shall understand it as you practice.

## Summarizing the differences

For better understanding the differences, you can see this table below :
<table>
<thead>
<tr>
<th>C</th>
<th>C++</th>
<th>Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>#include&lt;stdio.h&gt;</code><br/> <code>#include&lt;math.h&gt;</code> <br/>....</td>
<td><code>#include &lt;bits/stdc++.h&gt;</code></td>
<td>You need to include only one header file</td>
</tr>
<tr>
<td>No namespace statement</td>
<td>using namespace std;</td>
<td>Namespace std is used in cpp</td>
</tr>
<tr>
<td>int a;<br/>scanf(“%d”,&amp;a);<br/>printf(“%d,a);</td>
<td>int a; <br/> cin &gt;&gt; a; <br/> cout &lt;&lt; a;</td>
<td>Use of cin, cout in place of scanf and printf</td>
</tr>
<tr>
<td>int a; float b; char c;<br/>scanf(“%d %f %c”,&amp;a,&amp;b,&amp;c);<br/>printf(“%d %f %c”,a,b,c);</td>
<td>int a; float b; char c;<br/> cin &gt;&gt; a &gt;&gt; b &gt;&gt; c;<br/>cout &lt;&lt; a &lt;&lt; b &lt;&lt; c;</td>
<td>Multiple input output at the same time</td>
</tr>
<tr>
<td>printf(“%d\n”,a);</td>
<td>cout &lt;&lt; a &lt;&lt; endl;</td>
<td>To print some variable a followed by a newline character</td>
</tr>
<tr>
<td>char a[100]; <br/> scanf(“%s”,a);</td>
<td>char a[100];<br/>cin &gt;&gt; a;</td>
<td>To input a string without spaces</td>
</tr>
<tr>
<td>char a[100];<br/>gets(a);</td>
<td>string a;<br/>getline(cin,a);</td>
<td>To input a line <br/>Note - If you make a character array, you will have to use gets in cpp as well.</td>
</tr>
</tbody>
</table>