Found 33676 Articles for Programming

Difference between “int main()” and “int main(void)” in C/C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

3K+ Views

Sometimes we see that there are two types of main function definition. The int main() and int main(void). So is there any difference?In C++, there is no difference. In C also both are correct. But the second one is technically better. It specifies that the function is not taking any argument. In C if some function is not specified with the arguments, then it can be called using no argument, or any number of arguments. Please check these two codes. (Remember these are in C not C++)Example#include void my_function() {    //some task } main(void) {    my_function(10, "Hello", "World"); ... Read More

What is long long in C/C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

8K+ Views

In some cases we use long long in C or C++. Here we will see what is basically long long is? The long long takes twice as much memory as long. In different systems, the allocated memory space differs. On Linux environment the long takes 64-bit (8-bytes) of space, and the long long takes 128-bits (16-bytes) of space. This is used when we want to deal with some large value of integers.We can test the size of different types using this simple program.Example#include using namespace std; main() {    int a;    long b;    long long c;    cout

Bool to int conversion in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

13K+ Views

Here we will see how to convert bool to int equivalent in C++. Bool is a datatype in C++, and we can use true or false keyword for it. If we want to convert bool to int, we can use typecasting. Always true value will be 1, and false value will be 0.Example#include using namespace std; main() {    bool my_bool;    my_bool = true;    cout

How to check if an input is an integer using C/C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

4K+ Views

Here we will see how to check whether a given input is integer string or a normal string. The integer string will hold all characters that are in range 0 – 9. The solution is very simple, we will simply go through each characters one by one, and check whether it is numeric or not. If it is numeric, then point to the next, otherwise return false value.Example#include using namespace std; bool isNumeric(string str) {    for (int i = 0; i < str.length(); i++)       if (isdigit(str[i]) == false)       return false; //when one ... Read More

How to create a high resolution timer with C++ and Linux?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

To create high resolution timer we can use the chrono library. This library has high resolution clock. This can count in nanoseconds.In this program we will see the execution time in nanoseconds. We will take the time value at first, then another time value at the last, then find the difference to get elapsed time. Here we are using blank loop to pause the effect for sometimes.Example#include #include typedef std::chrono::high_resolution_clock Clock; main(){    auto start_time = Clock::now();    for(int i = 0; i

How can I create directory tree using C++ in Linux?

Samual Sam
Updated on 30-Jul-2019 22:30:26

1K+ Views

In this section we will see how to create a directory tree using C++ code in Linux. In Linux terminal we can put some command like “mkdir –p /dir/dir1/dir2” Here –p is used to mark as parent (recursively create inner directories).In C++ code we can use some libraries of Linux system. Then we can use Linux terminal commands as string argument of the system() function. We can create directory tree like this.Example#include #include #include #include using namespace std; int main() {    int status;    status = system("mkdir -p TP/My_Folder/test"); // Creating a directory    if ... Read More

How can I clear console using C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

12K+ Views

We can clear the console using C++ code. To do this we have to execute some system commands. In Linux systems, the POSIX is used. We can call system() function to execute system command. For clearing the console in linux, we can use “clear” command. This will be passed inside the system() function.Let us see the code to get the better idea.Example#include using namespace std; int main () {    cout

How to determine the version of the C++ standard used by the compiler?

Samual Sam
Updated on 30-Jul-2019 22:30:26

4K+ Views

Sometimes we need to know that, what is the current C++ standard. To get this kind of information, we can use the macro called __cplusplus. For different standards, the value of this will be like below.Standard__cplusplus outputC++ pre C++981C++98199711LC++98 + TR1This cannot be checked, this will be marked as C++98C++11201103LC++14201402LC++17201703LExample#include int main() {    if (__cplusplus == 201703L)       std::cout

How to output colored text to a Linux terminal?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

8K+ Views

Here we will see how to print some lines into the linux terminal with some color. Here we are doing anything special into C++ code. We are just using some linux terminal commands to do this. The command for this kind of output is like below.\033[1;31m Sample Text \033[0mThere are some codes for text styles and colors. These are listed below.ColorForeground CodeBackground CodeBlack3040Red3141Green3242Yellow3343Blue3444Magenta3545Cyan3646White3747Some additional options are like below −OptionCodeDescriptionReset0Back to normal (remove all styles)Bold1Bold the textUnderline4Underline textInverse7Interchange colors of background and foregroundBold off21Normal from boldUnderline off24Normal from UnderlineInverse off27Reverse of the InverseExample#include using namespace std; main() {    cout Read More

What is difference between instantiating a C++ object using new vs. without new?

Samual Sam
Updated on 30-Jul-2019 22:30:26

8K+ Views

In C++, we can instantiate the class object with or without using the new keyword. If the new keyword is not use, then it is like normal object. This will be stored at the stack section. This will be destroyed when the scope ends. But for the case when we want to allocate the space for the item dynamically, then we can create pointer of that class, and instantiate using new operator.In C++, the new is used to dynamically allocate memory.Example#include using namespace std; class Point {    int x, y, z;    public:       Point(int x, ... Read More

Advertisements