Shifting from C to C++
Chirag Jain
Incoming SDE Intern @ MicrosoftThis 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.
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 :
#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.
- For a new line, in C we use β\nβ in printf statement while we can use endl in cout statement in c++.
Caution: 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 :
using namespace std;
Multiline inputs
Suppose you need to take the input in the following format :
- The first line contains an integer, a floating point number and a character.
- The second line contains a sentence, with spaces between the words.
Now, try running these codes.
- In C, the code for this would be :
#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 :
#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.
Summarising the differences
For better understanding the differences, you can see this table below :
C | C++ | Explanation |
---|---|---|
#include<stdio.h> #include<math.h> .... | #include <bits/stdc++.h> | You need to include only one header file |
No namespace statement | using namespace std; | Namespace std is used in cpp |
int a; scanf(β%dβ,&a); printf(β%d,a); | int a; cin >> a; cout << a; | Use of cin, cout in place of scanf and printf |
int a; float b; char c; scanf(β%d %f %cβ,&a,&b,&c); printf(β%d %f %cβ,a,b,c); | int a; float b; char c; cin >> a >> b >> c; cout << a << b << c; | Multiple input output at the same time |
printf(β%d\nβ,a); | cout << a << endl; | To print some variable a followed by a newline character |
char a[100]; scanf(β%sβ,a); | char a[100]; cin >> a; | To input a string without spaces |
char a[100]; gets(a); | string a; getline(cin,a); | To input a line Note - If you make a character array, you will have to use gets in cpp as well. |