Found 7346 Articles for C++

What does the operation c=a+++b mean in C/C++?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

1K+ Views

Let us consider in C or C++, there is a statement like:c = a+++b;Then what is the meaning of this line?Well, Let the a and b are holding 2 and 5 respectively. this expression can be taken as two different types.c = (a++) + bc = a + (++b)There are post increment operator, as well as pre increment operator. It depends on how they are used.There are two basic concepts. The precedence and the associativity. Now if we check the expression from left to right, so the result will be these two.c = (a++) + b → 2 + 5 ... Read More

Is there a performance difference between i++ and ++i in C++ program?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

1K+ Views

The effective result of i++ and ++i are same. The only difference is that the i++ increases the value of i after assigning it, and for ++i, it increases the value first, then assigns its value. We can see the difference in the following code.Example Code#include using namespace std; int main() {    int x = 3, y, z;    y = x++;    z = ++x;    cout

What is the purpose of a function prototype in C/C++?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

6K+ Views

Here we will see what are the purpose of using function prototypes in C or C++. The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of a function parameter, it also tells about the return type of the function. By this information, the compiler cross-checks the function signatures before calling it. If the function prototypes are not mentioned, then the program may be compiled with some warnings, and sometimes generate some strange output.If some function is called somewhere, but its body is not defined yet, that is defined after the ... Read More

How can we return multiple values from a function in C/C++?

Vrundesha Joshi
Updated on 04-Oct-2023 21:21:06

25K+ Views

In C or C++, we cannot return multiple values from a function directly. In this section, we will see how to use some trick to return more than one value from a function. We can return more than one values from a function by using the method called “call by address”, or call by reference. In the invoker function, we will use two variables to store the results, and the function will take pointer type data. So we have to pass the address of the data. In this example, we will see how to define a function that can return ... Read More

What is 0 (zero) - A decimal literal or An octal literal in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

469 Views

The 0 (Zero) before a number is basically the octal literal.In C/C++ we can use octal literals by typing a zero before the actual number. For example, if an octal number is 25, then we have to write 025.Example Code#include int main() {    int a = 025;    int b = 063;    printf("Decimal of 25(Octal) is %d", a);    printf("Decimal of 63(Octal) is %d", b); }OutputDecimal of 25(Octal) is 21 Decimal of 63(Octal) is 51

Random number generation in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

178 Views

Let us see how to generate random numbers using C++. Here we are generating a random number in range 0 to some value. (In this program the max value is 100).To perform this operation we are using the srand() function. This is in the C library. The function void srand(unsigned int seed) seeds the random number generator used by the function rand.The declaration of srand() is like below:void srand(unsigned int seed)It takes a parameter called seed. This is an integer value to be used as seed by the pseudo-random number generator algorithm. This function returns nothing.To get the number we ... Read More

Is it legal to recurse into main() in C++?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

96 Views

In C or C++, the main function is like other functions. So we can use the functionalities that are present in some other functions, also in the main function.In the following program, we will see how the main() is using recursively to print some numbers in reverse order.Example Code#include using namespace std; int main () {    static int x = 10;    cout

How to use the PI constant in C++?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

15K+ Views

Here we will see how to use the PI constant in C++ program. The PI constant is present in the cmath header file. The name of the constant is M_PI. We can simply include that header file, and use the constant to perform operation.In the following example we will see how to find area of a circle using PI constant.Example Code#include #include using namespace std; float area(int radius) {    return M_PI * (radius * radius); } int main () {    cout

How to use namespaces in C++?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

127 Views

In this article we will see how to use the namespace in C++ code.Consider a situation, when we have two persons with the same name, Zara, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother’s or father’s name, etc.Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). ... Read More

Parsing Command Line Parameters in C++ Program

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

229 Views

It is possible to pass some values from the command line to your C++ programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard-coding those values inside the code.The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from ... Read More

Advertisements