What does ~(tilde) means before scanf function in c++? [duplicate] - c++

This question already has answers here:
The tilde operator in C
(6 answers)
Closed 3 years ago.
I found a code that takes input string and print them out.
But I don't know what does the tilde means in front of the scanf.
I found tilde can be used for either destructor or binary negation but it doesn't look like both. And the code doesn't work without tilde.
int main() {
char arr;
while (~scanf("%c", &arr)){
putchar(arr);
}
}

I found tilde can be used for either destructor or binary negation but it doesn't look like both.
It's the bitwise NOT operator applied to the return value of scanf() as you mentioned latter.
And the code doesn't work without tilde.
As #Mukul Gupta explained in their comment:
scanf returns the number of values it scanned successfully or EOF
if it reaches the end of file. EOF is a macro that represents a
negative value. On most platforms, the value of EOF is (int) -1.
In this case, taking 1's complement of -1, will make the value as 0
and is used to break from the loop.

Related

Why write `!!x`, when `x` will do? [duplicate]

This question already has answers here:
Double Negation in C++
(14 answers)
What is the usage of "!!" (negating twice)? [duplicate]
(3 answers)
Confused by use of double logical not (!!) operator [duplicate]
(5 answers)
Closed 2 years ago.
I often see experienced programmers write !!x, even though the expected expression is a Boolean (i.e., zero or not zero) and not an integer.
For example, a line from boost:
BOOST_ASSERT(!!p); // where `p` is a pointer
What's the point of !!p when just p will do?
What I understand by a Boolean parameter is an expression converted to a value of integral type, and the value is compared against zero, explicitly or implicitly (with if or its ternary operator equivalent).
Thus, anything that takes a Boolean and expects only 0 or 1 is wrongly implemented, if my understanding of Boolean is correct.
For clarification: it's obvious that ! converts to bool; the question is explicitly asking for why.
By default, BOOST_ASSERT(expr) expands to assert(expr). That C++ assert function takes a scalar argument, not bool. Thus, your statement, "the expected expression is a boolean," is not correct.
Classes in C++ can implement operator bool(); the basic_stream classes are examples of such: assert(std::cin) will not work. The !! operator forces the use of std::cin::operator bool(), and bool after !! will be evaluated to int 0 or 1. This is shorter than assert(bool(std::cin)).
If I were a boost author, I would expand BOOST_ASSERT(expr) to assert(!!(expr)) - as is done for ( BOOST_LIKELY(!!(expr)) ? ((void)0) ).
Closely related to the question
Warnings.
Some compilers issue warnings.
Overflow of the integer storing the Boolean.
I'll just quote the comment mentioning the behaviour
The compiler would treat the values the same either way if it's only used as an operand to an if or &&, but using !! may help on some compilers if if (!!(number & mask)) gets replaced with bit triggered = !!(number & mask); if (triggered); on some embedded compilers with bit types, assigning e.g. 256 to a bit type will yield zero. Without the !!, the apparently-safe transformation (copying the if condition to a variable and then branching) won't be safe.
Related
Some consider defining operator bool() to be "unsafe". And to have an idiomatical way to convert to bool, they define bool operator !().
So writing !!obj is to support such users. And it doesn't even hurt to do so!

Reading in one byte at a time with .get() [duplicate]

This question already has answers here:
Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?
(5 answers)
Closed 7 years ago.
So i'm reading in a input file that contains:
lololololololol
I need to read it in using binary one byte at a time for something I'm doing later on. To do this i'm using get() to read it in then storing it into a char. It seems to be working correctly except for the last char that it reads in. The vector that it is reading into contains:
lololololololol
�
I'm not quite sure what this last value is but it's totally throwing off my finial output. So my question is, is there a reason get() would read in a value or byte from my text document that is not there? Or is it reading in something that I don't know of?
code:
while(istr.good()) {
temp = istr.get();
input.push_back(temp);
}
It's reading the EOF (end of file) character. You need to do the check after reading it to avoid it being inserted to the vector:
while(temp = istr.get(), istr.good()) // comma operator
input.push_back(temp);
Or you might use the 2nd std::istream_base::get overload and let istr implicitly convert to bool:
while(istr.get(temp))
input.push_back(temp);
Or try more advanced approaches. operator>> and std::getline would also work fine for this kind of input.

Return type of printf() in C programing [duplicate]

This question already has answers here:
What does printf return?
(4 answers)
Closed 8 years ago.
A question regarding C Programming
As we all know that printf() is a function to show the out on screen.
And we also know that every function has a return type.
So my question is that:
What the default return type of printf() function ?
printf() returns number of characters successfully printed out.
The return type is int
printf returns int. See this for details.
This is what it has to say
On success, the total number of characters written is returned.
If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.
If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned
int, it returns the number of characters that it has printed.

What is a "literal" in C++? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the word “literal” mean?
Often when reading literature about C++, I encounter the word "literal". It is a bit unclear to me what exactly this term means in C++.
A literal is some data that's presented directly in the code, rather than indirectly through a variable or function call.
Here are some examples, one per line:
42
128
3.1415
'a'
"hello world"
The data constituting a literal cannot be modified by a program, but it may be copied into a variable for further use:
int a = 42; // creates variable `a` with the same value as the literal `42`
This concept is by no means unique to C++.
The term "literal" comes from the fact that you've written data literally into your program, i.e. exactly as written, not "hidden" behind a variable name.
Wikipedia gives you quickly this about literals.
In your C or C++ source code, Things like 1234, nullptr (in recent C++), "abcd" are literals.

Meaning of a comma in a number [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ Comma Operator
that probably a trivial question, but I don't know the answer. And this has been troubling me this afternoon.
I was just writing a function to convert RVB to YUV. Nothing really special, but mistakenly used the comma (,) instead of a dot in my numbers.
It compiles but the result was not what I expected, for example "-3713796" instead of a 0-255 range number.
(0,615*(double) 61) - (0,51498*(double) 61) - (0,10001*(double) 61)
So what does it mean ?
If it's not a compilation error, it's probably usefull for something but what?
Ps: I was using C++ with Qt.
You've accidentally used the comma operator. It evaluates its first operand and discards the result, and then evaluates the second operand and returns its value and type.
In my experience, it's most commonly used in loop statements. For other uses, see Uses of C comma operator
It's the comma operator which evaluates each operand and returns the rightmost. In this case it evaluated 0, then 615*(double) 61 and resulted in a value of that product.