1 of 15

Switching from Turbo C++ to GCC

Tutorial

2 of 15

Note to Turbo C++ users

  • Turbo C++ uses a very old 16-bit compiler which is outdated and is not in compliance with the latest standards.
  • While the latest compilers are 32-bit or 64-bit and comply to the standards.
  • If you haven't already, it's the right time to switch from TurboC++ to the worldwide used standard compiler like gcc/g++ (Linux based) or mingw-g++/mingw-gcc (Windows based)

3 of 15

Which IDE to use?

  • Although it's best to code in Linux, but if you're more comfortable with Windows, use CodeBlocks IDE which uses Mingw-gcc as the compiler.
  • CodeBlocks is available for both Windows and Linux, and supports features like code completion, braces completion, etc. being very easy-to-use.
  • You can of course use any of the other available IDE(s), but just make sure that the one you choose uses/supports gcc or mingw-gcc.

4 of 15

Code without IDE in Linux

  • You can use any editing tools like gedit, vim, nano, etc. to write code just like you write other text files using them (and of course give the extension as .cpp (or .c) to them).
  • And to compile and run, you can enter the following (simplest) commands in the terminal:
  • To compile: g++ file-name.cpp
  • To run: ./a.out
  • If your code includes math header file, then compile with -lm option.

5 of 15

  • No programming contest (except some school �contests) and no online coding platforms (like CodeChef, Spoj, etc.) supports Turbo C++, but use gcc for compilation.
  • To make the code compilable with gcc and to ensure that the latest standards are met, few minor changes are required in the Turbo C++ code (though the rest of the code remains the same)
  • Some tips are discussed in the following slide

6 of 15

Tip 1

  • There's no header file named as 'conio.h' in gcc
  • Including such header file would result in compilation error
  • Since conio.h header file doesn't exist, the latest standard does not support functions like clrscr(), getch(), getche(), gets(), cgets(), puts, putch(), cputs(), etc. too.
  • Hence, using such functions would also result in compilation error. So, do NOT use conio.h header file and any of its functions.

7 of 15

So how do you clear the screen then?

  • You don't need to. Atleast you won't be required to �clear the screen in the programming contests
  • Whenever the program runs/starts, the output stream (if not the screen) is cleared automatically. Then the contest's judging system compares the output in the output stream only with the expected output.

8 of 15

Isn't getch() a necessary function to use?

  • No! Program can very well work (and generate output) without that function.

9 of 15

If gets() is not allowed, then how to take string as input?

  • You can always input strings using cin or scanf().
  • You would generally not be required to take strings with spaces as input in any programming contest.
  • And if you ever have to, you can use functions like cin.getline(), etc.
  • In short, you can always do without using conio.h header file and any of its functions.

10 of 15

Tip 2: Some header files are included differently

  • A little more Functions from (most of) rest of the header files can be used normally, as you did in Turbo.
  • But some of the header files are included differently.
  • For example, you write

In Turbo C

In GCC

#include<iostream.h>

#include<iostream>

#include<iomanip.h>

#include<iomanip>

11 of 15

  • And, header files which were originally a part �of C language (like math.h, string.h, stdio.h, time.h, stdlib.h, ctype.h, etc.) must have 'c' as a prefix to them and shouldn't be having .h at the end.
  • You include them like:�#include<cmath>�#include<cstring> �#include<cstdio> �#include<ctime>�#include<cstdlib> �#include<cctype>

12 of 15

Tip 3: Use using namespace std

  • Either you prefix std:: with all cout and cin statements and few other places, or add the following line outside the main function:

using namespace std;

13 of 15

Tip 4: Do not use void main()

  • Do not use void main(), use only int main(),
  • and always return 0 at the end of the main()function

14 of 15

‘Hello World' program in gcc/g++

Here's how it looks like:

#include<iostream>��using namespace std;�int main() { � cout<<"Hello readers!\n"; � return 0; �}

15 of 15

Thank you!

  • Hope that you enjoyed this short tutorial to transition from Turbo C++ to gcc/g++
  • Now you are ready to solve problems at CodeChef or other such platforms using gcc compiler
  • Happy Coding! :)

Source: http://foobar.iiitd.edu.in/turbo.pdf