Online C++ compiler
About C++
C++ is a commonly used high-level, object-oriented programming language developed by Bjarne Stroustrup as an extension of C. It is designed for system and application software and used for game development, embedded systems, computer graphics, and much more. The syntax of C++ is similar to the C programming language. It is a programming language with which most programmers begin their coding journeys.
Features of Online C++ Compiler
- It can be accessed through any web browser
- Easy to use interface and supports various libraries.
- The syntax highlighting feature colours the elements of the code to increase readability.
- Compilerβs Autocompletion feature accelerates coding by predicting already defined variables/functions and completing code.
Syntax Help
The syntax of C++ is very similar to that of the C programming language. In C++, we have additional features like classes, objects, and templates. The syntax in C++ follows a strict hierarchy of statements, variables, functions, keywords, operators, and punctuation used to define program structure. C++ is a clear and easy-to-understand language.
1. If-Else Statement:
We use the if statements in C++ to specify the code to be executed only when the given condition is satisfied. Whereas an else statement in C++ is used to specify the code to be executed when the condition is not satisfied. Let's look at the syntax to use an if-else statement -
//Syntax to use an if-else statement
if (condition) {
//code to be executed if the condition is satisfied
}
else {
//code to be executed if the condition is not satisfied
}
Example:
#include <iostream>
using namespace std;
int main() {
// given two numbers, print "YES" if their XOR is equal to their OR, otherwise print "NO"
int a, b;
a = 10;
b = 8;
int cur_xor = a ^ b;
int cur_or = a | b;
if(cur_or == cur_xor) {
cout << "YES\n";
}
else {
cout << "NO\n";
}
return 0;
}
Output:
NO
2. Switch Statement:
The switch statements in C++ are very similar to the if-else-if statements. We use Switch statements to specify several parts of codes to be executed simultaneously. Let's look at the syntax to use a switch statement -
//Syntax to use a Switch statement
switch (expression) {
case value_1:
// code to be executed if expression = value_1
break;
case value_2:
//code to be executed if expression = value_2
break;
default:
// code to be executed if expression does not equal to either of the cases
}
Example:
#include <iostream>
using namespace std;
int main() {
// program to apply bitwise operations between 2 numbers
int a, b;
a = 14;
b = 11;
char operation;
operation = '&';
switch(operation) {
case '|':
cout << a << " OR " << b << " = " << (a | b);
break;
case '&':
cout << a << " AND " << b << " = " << (a & b);
break;
case '^':
cout << a << " XOR " << b << " = " << (a ^ b);
break;
default:
cout << "Invalid Operation";
} return 0;
}
Output:
14 AND 11 = 10
3. For Loop:
We use "for loop" in C++ when we know how many times we need to run the loop to get the desired output. Let's look at the syntax to use a for loop -
//Syntax to use a for loop
for(initialization; condition to be considered; increment/decrement) {
//code to be executed
}
For example if we want to print numbers from zero to four, we will use the for loop as follows -
for(int i = 0; i<5; i++) {
cout<< i << endl;
}
Example:
#include <iostream>
using namespace std;
int main() {
// given an integers a, b and c
// print all the numbers between them having sum of digits less than c
int a, b, c;
a = 1;
b = 30;
c = 5;
for(int i = a; i <= b; i ++) {
int cur = i, sum = 0;
while(cur) {
sum += (cur % 10);
cur /= 10;
}
if(sum < c) cout << i << ' ';
}
return 0;
}
Output:
1 2 3 4 10 11 12 13 20 21 22 30
4. While Loop:
We use while loops in C++ when we are unaware how many times we will have to use the loop to get the desired output. Hence, a while loop continues executing the code until the given condition is satisfied. Let's look at the syntax to use a while loop -
//Syntax to use a while loop
while (condition to be considered) {
//code to be executed
}
Example:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
// given an array of interges
// find maximum sum between pair of numbers such that
// if the first number is at position i from the start then the second number should be present at position i from end
int n = 5;
vector <int> v(n);
v = {10, 6, 7, 14, 8};
int sum = INT_MIN;
int l = 0, r = n - 1;
while(l <= r) {
sum = max(sum, v[l] + v[r]);
l ++;
r --;
}
cout << sum << '\n';
return 0;
}
Output:
20
5. Do-While Loop:
The do-while loop in C++ works once; if the condition turns out to be true, it will continue as long as it holds true. Let's look at the syntax to use a do-while loop -
//Syntax to use a do-while loop
do {
//code to be executed
}
while (condition to be considered);
Example:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
// find all the next permuatations of a given array
int n = 4;
vector <int> v(n);
v = {1,2,3,3};
do {
for(auto &x : v) cout << x << ' ';
cout << '\n';
} while(next_permutation(v.begin(), v.end()));
return 0;
}
Output:
1 2 3 3
1 3 2 3
1 3 3 2
2 1 3 3
2 3 1 3
2 3 3 1
3 1 2 3
3 1 3 2
3 2 1 3
3 2 3 1
3 3 1 2
3 3 2 1
Functions in C++
A function in C++ is a block of code meant to perform a specific task. They take inputs from the user and return an output which can be a value or a data type. We can call functions multiple times throughout the program. Functions also help us to break down complex problems into more manageable parts and make the code more organized and easier to understand.
Letβs see the below function:
int fun(int a){
int sum=10;
a= sum+10;
return a;
}
Example:
#include <iostream>
int fun(int a){
int sum=10;
a= sum+10;
return a;
}
int main() {
int number;
number = 50;
std::cout << fun(number);
return 0;
}
Output:
20
How to declare Function
A function in C++ is declared by specifying its name, return type, and parameters. For example - "int add(int x, int y)" declares a function named "add" that considers two parameters, "x" and "y," and returns an integer value. Let's look at the syntax to declare a function -
//syntax to declare a function
[return_type] name_function{parameter_list}
{
//function body
}
For example - βint add(int x, int y)β declares a function named βaddβ that considers two parameters βxβ and βyβ and returns an integer value.
// syntax to declare a function named βaddβ
int add(int x, int y) {
return x+y;
}
How to call Function
A function in C++ is called by writing the function's name followed by the two parentheses and necessary arguments if any and ending it by semicolon ;. Let's look at the syntax to call a function -
function_name(parameter 1, parameter 2, β¦parameter n);
For example - Letβs consider a function named βadd_numbersβ that considers two integers, and returns the sum of the two numbers, we can call the function as -
int result add_numbers(1, 2);
This function will now execute the code and return a specified value.
How to define Function
A function in C++ is defined using βreturn_type" followed by the function name and then the parameter list. The code is then placed within the curly braces. The definition for the code is given below the function. Let's look at the syntax to define a function -
//syntax to define a function
return_type name_function{parameter_list}
{
//function body
}
Here, the function body is the definition of the function.
For example - βvoid cpp_fucntion(int x) {code}β defines a function named βcpp_functionβ that considers an integer parameter and doesnβt return any value.
Example:
#include <iostream>
using namespace std;
long long get_cube(long long num) {
return num * num * num;
}
int main() {
// find the cube of a given number
long long num = 6;
cout << get_cube(num);
return 0;
}
Output:
216
Working of the Online C++ Compiler (IDE)
The C++ IDE Online work in various stages for analysing source code to output code. Despite their variations, they usually perform the below actions:
- Lexical Analyser: The online C++ Compiler separates the source code into lexemes. Lexemes are individual pieces of code. It represents specific coding patterns.
- Syntax Analyser: The C++ Compiler Online checks the syntax of the code. Checking is done based on the rules for the source code. This method is also known as parsing.
- Semantic Analyser: The logic of the code is tested by the C++ Online ide for accuracy. By verifying the accuracy of the code, this phase extends beyond syntax analysis.
- Intermediate Code Generation: After the code has gone through all three analysis methods, the Online Compiler for C++ creates an IR code of the source code.
- Optimisation: The C++ Editor Online optimises the IR code. Then the compiler will determine the kind and degree of optimization and generate the output code.
- Output Generation: The online C++ Compiler creates the final output code. Using the optimised IR code.
To access the abilities of our online cpp compiler, you must first Sign in. Code studio online compiler works in the following ways:
- Code Editor: Use the code editor to enter code. You can modify code easily. Syntax highlighting is available for high readability.
- RUN: After entering online c++ editor and choosing the correct compiler version. Users can click the "RUN CODE" button and run their C program.
- Reset Code: To reset the C++ code online entered into the editor. Click the "reset" button in the top right corner of the editor.
- STDIN & STDOUT: Users can utilise our compiler's Standard Input (stdin) console to feed input to the C++ Code editor. And the Standard Output (stdout) of the compiler displays the output of the code.
How to write and run the C++ program online
- To run online programming c++, a user can write the code in the Code editor.
- If the C++ program needs any input for its functioning, provide it in the Custom Input Window (STDIN).
- Finally, click on the Run Code button.
- The compiler currently uses the GNU C++ v5.4 compiler to compile your program and convert it to machine code.
- The output is displayed on the Output Window (STDOUT) if your code has no errors.
- Otherwise, the Output Window displays the errors encountered.
Benefits of using online C++ Compiler
- Installing a C++ compiler and setting up a C++ development environment in your local system is unnecessary.
- It allows us to quickly code, compile and run c++ program online on your favourite browser.
- It helps to verify our program's output on corner test cases using the custom input feature.
- It provides an inclusive IDE experience using switchable dark and light themes and full-screen code features.
Glossary
- Compiler: A compiler is an essential tool that can be used locally or online for compiling a code, that is, converting the code to a machine based language for output.
- Interpreter: An Interpreter is a tool that works similarly to a compiler, converting high level code into machine based language, but it converts the code line by line rather than in a single pass.
- Online IDE: Online IDE, or Integrated Development Environment, is a software-based tool that is used as a single platform for all kinds of computer programming. It comprises an integrated compiler or interpreter, extensions, dependency files, and debugger.
- Code Editor: A Code Editor is another essential part of computer programming. It is a place where all source codes can be created and modified. It can be a stand-alone application or part of IDE.
- Coding Ground: Coding Ground is a tool that provides easy accessibility to developers for using more than one programming language, technology, and framework. It allows developers to use various tools on a single platform.