Found 7346 Articles for C++

Print leading zeros with C++ output operator

Nancy Den
Updated on 30-Jul-2019 22:30:25

6K+ Views

Here we will see how to print leading zeros as output in C++. We know that if we directly put some zeros before some numeric values, then all zeros are discarded, and only exact numbers are printed.In C, we can solve this problem by using some options of format specifier. In C++ we can manipulate the output sequence using iomanip library. In this library we will get setw() function to make some space between previous text and current text. Then we can use setfill(char) function to add some characters into that field.Please check the following code to get the idea ... Read More

Ternary operator ?: vs if…else in C/C++

Nancy Den
Updated on 30-Jul-2019 22:30:25

636 Views

We know that the ternary operator is the conditional operator. Using this operator, we can check some condition and do some task according to that condition. Without using the ternary operator, we can also use the if-else conditions to do the same.The effect of ternary operator and if-else condition are same in most of the cases. Sometime in some situation we cannot use the if-else condition. We have to use the ternary operator in that situation. One of this situation is assigning some value into some constant variable. We cannot assign values into constant variable using if-else condition. But using ... Read More

C Program to find sum of two numbers without using any operator

Nancy Den
Updated on 30-Jul-2019 22:30:25

1K+ Views

In this section we will see how to print the sum of two numbers without using any type of operator into our program.This problem is tricky. To solve this problem we are using minimum width field of printf() statement. For an example if we want to put x number of spaces before “Hello” using printf() we can write this. Here printf() takes the width and then the character that will be printed. In this case we are writing blank space.Example Code#include main() {    int x = 10;    printf("%*cHello", x, ' '); }OutputHelloNow let us see how this functionality ... Read More

Execution of printf with ++ operators in C

Nancy Den
Updated on 30-Jul-2019 22:30:25

2K+ Views

In some problems we can find some printf() statements are containing some lines with ++ operator. In some questions of competitive examinations, we can find these kind of questions to find the output of that code. In this section we will see an example of that kind of question and try to figure out what will be the answer.Example Code#include int main() {    volatile int x = 20;    printf("%d %d", x, x++);    x = 20;    printf("%d %d", x++, x);    x = 20;    printf("%d %d %d ", x, x++, ++x);    return 0; }Now we ... Read More

Operator Precedence and Associativity in C

Nancy Den
Updated on 30-Jul-2019 22:30:25

10K+ Views

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will ... Read More

Stack Unwinding in C++

Nancy Den
Updated on 30-Jul-2019 22:30:25

1K+ Views

Here we will see what is the meaning of stack unwinding. When we call some functions, it stores the address into call stack, and after coming back from the functions, pops out the address to start the work where it was left of.The stack unwinding is a process where the function call stack entries are removed at runtime. To remove stack elements, we can use exceptions. If an exception is thrown from the inner function, then all of the entries of the stack is removed, and return to the main invoker function.Let us see the effect of stack unwinding through ... Read More

What happen if we concatenate two string literals in C++?

Nancy Den
Updated on 30-Jul-2019 22:30:25

83 Views

In this section we will see another property of string and string literals. If we want to concatenate two strings in C++, we have to remember some of things.If x + y is the expression of string concatenation, where x and y both are string. Then the result of this expression will be a copy of the character of string x followed by the characters of string y.Either x or y can be a string literal or character, but not both. If both are string literal, they will not be concatenated.Example Code#include using namespace std; main(){    cout Read More

StringStream in C++ for Decimal to Hexadecimal and back

Nancy Den
Updated on 30-Jul-2019 22:30:25

565 Views

In this section we will see how to convert Decimal to Hexadecimal string and also from Hexadecimal string to Decimal string in C++. For this conversion we are using the stringstream feature of C++.String streams are used for formatting, parsing, converting a string into numeric values etc. The Hex is an IO manipulator. It takes reference to an IO stream as parameter and returns reference to the string after manipulating it.In the following example we will see how to convert decimal number or hexadecimal number.Example Code#include #include using namespace std; main(){    int decimal = 61;    stringstream my_ss;   ... Read More

Stringize and Token-pasting operator in C

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

2K+ Views

In this section we will see what are the Stringize operator and Token Pasting operator in C. The Stringize operator is a preprocessor operator. It sends commands to compiler to convert a token into string. We use this operator at the macro definition.Using stringize operator we can convert some text into string without using any quotes.Example Code#include #define STR_PRINT(x) #x main() {    printf(STR_PRINT(This is a string without double quotes)); }OutputThis is a string without double quotesThe Token Pasting operator is a preprocessor operator. It sends commands to compiler to add or concatenate two tokens into one string. We use ... Read More

How to find length of a string without string.h and loop in C?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

542 Views

In this section we will see how to find length of a string without using string header file and loops in C. The string length finding problem can be solved without string.h very easily. We can use recursive function to do it.But in this example we are not using recursion. We are using another trick to do that. We are using printf() function to get the length. The printf() function returns the number of character it has printed. If we print only that string using a printf() function, we can easily get the length of it.Example Code#include main() {   ... Read More

Advertisements