What does the operation c=a+++b mean?
It's parsed as c = a++ + b
, and a++
means post-increment, i.e. increment after taking the value of a
to compute a + b == 2 + 5
.
Please, never write code like this.
C++: what does (a b) mean?
1 << 1 means:
00000000 00000001 changes to 00000000 00000010
1 << 8 means:
00000000 00000001 changes to 00000001 00000000
It's a bit shift operation. For every 1 on the right, you can think of yourself as multiplying the value on the left by 2. So, 2 << 1 = 4 and 2 << 2 = 8. This is much more efficient than doing 1 * 2.
Also, you can do 4 >> 1 = 2 (and 5 >> 1 = 2 since you round down) as the inverse operation.
Why does c = ++(a+b) give compilation error?
It's just a rule, that's all, and is possibly there to (1) make it easier to write C compilers and (2) nobody has convinced the C standards committee to relax it.
Informally speaking you can only write ++foo
if foo
can appear on the left hand side of an assignment expression like foo = bar
. Since you can't write a + b = bar
, you can't write ++(a + b)
either.
There's no real reason why a + b
couldn't yield a temporary on which ++
can operate, and the result of that is the value of the expression ++(a + b)
.
Why is the expression c=(a+b)++ not allowed in C?
The postincrement operator can only be applied to an l-value, i.e. something that can appear to the left of the assignment operator, most commonly a variable.
When x++
appears in an expression it is evaluated as x
and x
is increased afterward. For example a = 2; b = 2 * (a++);
is equivalent to a = 2; b = 2 * a; a = a + 1;
.
Your example fails to compile because it is not possible to assign a value to a+b
. To be more explicit c=(a+b)++
would be equivalent to c = (a + b); (a + b) = (a + b) + 1;
, which makes no sense.
3 plus symbols between two variables (like a+++b) in C
I like the explanation from Expert C Programming:
The ANSI standard specifies a convention that has come to be known as
the maximal munch strategy. Maximal munch says that if there's more
than one possibility for the next token, the compiler will prefer to
bite off the one involving the longest sequence of characters. So the
example will be parsedc = a++ + b;
What is the difference between rb+ and ab in fopen()?
With the mode specifiers above the file is open as a text file. In
order to open a file as a binary file, a"b"
character has to be
included in the mode string. This additional"b"
character can either
be appended at the end of the string (thus making the following
compound modes:"rb"
,"wb"
,"ab"
,"r+b
","w+b"
,"a+b"
) or be inserted
between the letter and the"+"
sign for the mixed modes ("rb+"
,"wb+"
,
"ab+"
).
From fopen
documentation which I advise you read before asking questions. It will give you a lot of information about possible parameters, return values, similar functions etc.
Also, from the same document :
"a"
= append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek
,fsetpos
,rewind
) are ignored. The file is created if it does not exist.
"r+"
= read/update: Open a file for update (both for input and output). The file must exist.
Why a+++++b can not be compiled in gcc, but a+++b, a++ + ++b, and a+++ ++b can be?
That's because a+++++b
is parsed as a ++ ++ + b
and not as a ++ + ++ b
[C's tokenizer is greedy]. a++
returns an rvalue and you cannot apply ++
on an rvalue so you get that error.
a+++b; // parsed as a ++ + b
a+++ ++b; // parsed as a ++ + ++ b
Read about Maximal Munch Rule.
The tilde operator in C
The ~
operator is bitwise NOT, it inverts the bits in a binary number:
NOT 011100
= 100011
What does the statement if (counter & (1 j)) mean and how does it work?
The statement:
if (counter & (1<<j))
checks if the j
-th bit of counter
is set. In more detail, 1 << j
uses shifting of 1
to generate a bit mask in which only the j
-th bit is set. The &
operator then masks out the j
-bit of counter
; if the result is not zero (which means that the j
-th bit of counter
was set), the condition is satisfied.
Consider the following example. If counter
is 320, its binary representation is 101000000
, which means that the 6th bit (the one corresponding to the value of 64) is set; let's test for that bit. The bit mask is generated by shifting 1
, which has the binary representation 000000001
, 6 places to the right, which results in the binary value 001000000
. The value of counter
, namely:
101000000
is combined with &
, which is the bitwise and-operator, with the bit mask as follows:
101000000
& 001000000
---------
001000000
The value 001000000
again corresponds to the value of 64; however this is not important here, it only matters that it is not zero (as it has a nonzero bit, namely the bit for which we intended to check). In total, the condition
if ( 64 )
is satisfied. In the semantics of C (which does not feature a native Boolean data type) any nonzero value is treated as true when checked with if
.
How does a pair unify with the types of the Arrow functions
There is an instance for Arrow (->)
. So
(&&&) :: (Arrow a) => a b c -> a b c' -> a b (c,c')
has the instantiation
(&&&) :: (->) b c -> (->) b c' -> (->) b (c,c')
or, written in more conventional notation,
(&&&) :: (b -> c) -> (b -> c') -> (b -> (c,c'))
The rest should follow from that.
I use the arrow functions (especially (***)
and (&&&)
) all the time on the (->)
instance. My usage of those combinators for any other instance of Arrow
is very rare. So whenever you see a b c
, think "(generalized) function from b
to c
", which works for regular functions too.
Related Topics
Who Deletes the Memory Allocated During a "New" Operation Which Has Exception in Constructor
Cpp - Valgrind - Invalid Read of Size 8
Difference Between C++03 Throw() Specifier C++11 Noexcept
Declare Abstract Signal in Interface Class
Sharing a Global/Static Variable Between a Process and Dll
Round a Float to a Regular Grid of Predefined Points
What's the Behavior of an Uninitialized Variable Used as Its Own Initializer
Calling Python from a C++ Program for Distribution
Simple Way to Unzip a .Zip File Using Zlib
Nonstatic Member as a Default Argument of a Nonstatic Member Function
Why Does Windows 10 Start Extra Threads in My Program
Receiving Chunked Http Data with Winsock
Why Are Memcpy() and Memmove() Faster Than Pointer Increments
Why Is It Undefined Behavior to Delete[] an Array of Derived Objects via a Base Pointer
Why Is the New Random Library Better Than Std::Rand()
Template Partial Ordering - Why Does Partial Deduction Succeed Here