How to Setup VSCode with C, C++ and Python for Competitive Programming

VSCode is a Text editor that provides support for development operations and version control systems. It provides tools for a user to build hassle-free codes. VSCode can be downloaded and installed from visualstudio.com

This article will show you how to, fetch test cases directly from the browser without copy-pasting and run them in VSCode with just 2 clicks, create your own Snippets, and use the powerful Debugger.

If you have been solving problems on platforms like GeeksforGeeks, CodeChef, and various other judges, you are probably using an online compiler to solve problems. In that case, copying, modifying, and running test cases becomes laborious. You need to switch tabs to copy-paste test cases and modify them. Solving problems is already challenging.

Some Ideone users have suffered plagiarism as their submission matched to someone they didn’t even know. They say that their code was copied from Ideone. It is because default visibility is public on Ideone, that allows anyone to see your code.



Programming on your own IDE has its own great features, such as IntelliSense: code completion, parameter info, quick info, and member lists. Plus there is no latency.

Code like experienced programmers with the help of this guide.

Configuring VSCode

Download VSCode from their official site and install it.

All you need to do is install a few extensions to get it up and running.

  • Competitive Companion: A browser extension for parsing programming problems from webpages and saving them. Available for both chrome and firefox.
  • Competitive Programming helper: Extension for VSCode which does the magic of running the test cases from files. Modify or add test cases manually.
  • C/C++ for Visual Studio Code: This is a helper extension. Required for IntelliSense, debugging, and code browsing.
  • Python for Visual Studio Code: Linting, Debugging (multi-threaded, remote), Intellisense, code formatting, snippets, and more.
  • Code Runner: (Optional) Run code snippet or code file for multiple languages without executing commands.

Now you are all set to solve problems without any hassle. This setup is enough to get going, the rest of the setup takes some time.

Templates

Speed is an important factor in competitive programming. Take problem-solving to the next level by creating your own templates. Snippets are a great tool to write programs faster. You can download a Snippet Generator very easily.

To create or edit your own snippets, select User Snippets under File > Preferences (ctrl + shift + p), and then select the language. Use this awesome site to convert your piece of code into a snippet. Paste the snippet inside the curly braces in the JSON configuration file and save it. Separate different snippets by a comma. Create your cool snippets for different algorithms or make a single large template.



To use snippets you just need to type the tab trigger and hit tab.

Setting up the Debugger

Debugging can be a pain sometimes, especially when the program is large and there are runtime errors. In that case, printing won’t help much. All you need is a debugger to save the day. Thankfully, VSCode comes with a builtin nice GUI for debugging.

Python comes with its debugger and GCC has gdb. Go to debugger tab on the left and click Run and Debug. Make sure you have a file opened to debug.

Debugging Python Script

If you are debugging python you should be prompted like this. Choose python file

This will create a launch.json file under .vscode directory. That looks like this. There is no need to edit this file, it is enough to start debugging. Learn more about editing configurations here.

Debugging C/C++ Scripts

Before debugging, you would need to add the build task. From the main menu, choose Terminal > Configure Default Build Task. A dropdown appears showing various predefined build tasks for C++ compilers. Choose C/C++: g++ build active file.



This will create tasks.json file, save it. You might be prompted to select the environment first. Choose C++ (GDB/LLDB). After that select debug configuration.

This will also create a launch.json file that looks like this. For more information about configuration refer to this page.

Now you can debug your programs in C and C++. 

Note: You will need to create these configurations if you work in a different directory. It doesn’t take much time to create these files but you can also copy them to other directories.

Providing Input while Debugging

The internal debug console will prompt for input when there is any input statement. There is a smarter way to do this. Create a file named input.txt in the current directory. Paste input into the file. In the launch.json file change the args key to the value below:

"args" : ["<", "input.txt"]

Unfortunately, this does not work for Python. For python change the console key to 

"console": "externalTerminal"

It opens an external terminal that can be used to paste input. This can be done for C/C++ by setting externalconsole to true 

"externalConsole": true

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.