I am given a piece of code for which we have to guess output.
My Output: 60
#include <stdio.h>
int main()
{
int d[] = {20,30,40,50,60};
int *u,k;
u = d;
k = *((++u)++);
k += k;
(++u) += k;
printf("%d",*(++u));
return 0;
}
Expected:
k = *((++u)++) will be equal to 30 as it will iterate once(++u) and then will be iterated but not assigned. So we are in d[1].
(++u) += k here u will iterate to next position, add k to it and then assign the result to further next element of u.
Actual result:
main.c: In function ‘main’:
main.c:16:16: error: lvalue required as increment operand
k = *((++u)++);
^
main.c:18:11: error: lvalue required as left operand of assignment
(++u) += k;
And this has confused me further in concepts of pointers. Please help.
As the compiler has told you, the program is not valid C.
In C, pre-increment results in an rvalue expression, which you may not assign to or increment.
It's not a logical problem; it's a language problem. You should split that complex formula into multiple code statements.
That's all there is to it.
(In C++ it's an lvalue though and you can do both those things.)
In C, ++a is not an l-value.
Informally this means that you can't have it on the left hand side of an assignment.
It also means that you can't increment it.
So (++a)++ is invalid code.
(Note that it is valid C++).
Related
I'm learning C as Javascript developer and a common mistake that I make is when I'm supposed to define multiple variables in C like this
int a, b, c;
a = b = c = 0;
I accidentally do it the Javascript way
a,b,c = 0;
I'm wondering what the above is called and when I should define variables like this.
I'm wondering what the above is called....
Two things about this statement
a,b,c = 0;
First, this is same as
a;
b;
c = 0;
The expression a; and b; result is unused.
The compiler must be throwing warning messages on this statement. When I compiled with clang compiler, I am getting following warnings:
p.c:6:2: warning: expression result unused [-Wunused-value]
a, b, c = 9;
^
p.c:6:5: warning: expression result unused [-Wunused-value]
a, b, c = 9;
^
and Second, the , in the statement is , (comma) operator.
Precisely stated, the meaning of the comma operator in the general expression
e1 , e2
is evaluate the subexpression e1 and discards the result , then evaluate e2; the value of the expression is the value of e2.
So, the value of expression a,b,c = 0 is value of c = 0. The variable a and b will remain uninitialised.
May you can try this and check the variable values after this statement:
a = 99, b = 5, c = 0;
Since you are learning C, let me tell you one more thing - The , (comma) act as separator in function calls and definitions, variable declarations, enum declarations, and similar constructs. To begin with, check this.
and when I should define variables like this.
The statement a,b,c = 0; is not definition of variable a, b and c. You have defined the variable a, b and c here
int a, b, c;
Note that in this statement, the , is act as separator.
Its use is completely depends on you as long as you know very well about it. One of the very common use of , (comma) operator is in for loop, where it can be used to initialise multiple variables and/or increment/decrement loop counter variable and other variables etc., for example :
for (i = 0, j = some_num; i < some_num; ++i, --j) ....
Divide it up by the operators:
int a, b;
a = b = 10;
^ ^^^^^^
| Right operand
Left operand
Here, first it calculates b = 10 to assign to a. b = 10 "returns" the new value of b which is 10, and that is assigned to a. It works similarly with more variables:
int a, b, c;
a = b = c = 10;
^^^^^^-Done first
^^^^^-Done second
^^^^^-Done last
a, b, c = 10 does not work the same way. First of all, you have to declare them first before assigning. Also, this uses the , operator which has a lower precedence than =, so the line is equivalent to a; b; c = 10;. As you can see, nothing really happens to a or b and only c is set to 10.
The comma used as an operator is called the comma operator.
For details, read a C standard, like n1570 (§6.5.17) or later. The semantics is defined as:
The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value
Of course, inside function calls like printf("x=%d y=%d\n", x, y) the comma is separating arguments. Inside macro invocations and definitions also. In that case, it is the comma punctuator (see n1570 §6.5.2, 6.7, 6.7.2.1, 6.7.2.2,6.7.2.3, 6.7.9)
If you compile with GCC, invoke it as gcc -Wall -Wextra -g. You could get useful warnings.
Consider, if so allowed, to use the Clang static analyzer (or other tools like Frama-C or Bismon; you may contact me in 2021 by email to basile.starynkevitch#cea.fr)
Take inspiration from the source code of existing free software programs like GNU make or GNU bash. They rarely use the comma operator.
In c language you can define the variable using one line
int a,b,c;
This is same as
int a;
int b;
int c;
You can define and initialize this one like this
int a,b,c;
a=b=c=10;
if you write
int a,b,c;
a,b,c=10;
it will initialize c as 10 but others will not inilizes as 10;
a and b will be defined but not initialized it will return garbage value.
This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 7 years ago.
I am trying to get some understanding about how pass by value & return are happening in C functions. I cam across a piece of code as follows:
#include <stdio.h>
int fun(int ii, int jj)
{
int kk, ll;
kk = ii + jj;
ll = ii * jj;
return (kk,ll);
}
int main()
{
int i=4, j=5, k, l;
k = fun(i, j);
l = fun(i, j);
printf("%d %d\n", k, l);
return 0;
}
Now apparently I am not getting any errors when I am trying to return 2 values through fun().
Also, the value that is returned by fun() is ll i.e 20 (=4*5) and not kk. Further, If I rewrite the return statement as :
return (ll,kk);
the value returned is that of kk ie. 9 (=4+5).
Query: Why this is so?
In your code, in the return statement
return (kk,ll);
you're using the comma operator. By the property of the comma operator, you're not returning two values, rather you're returning the second operand of the comma operator only.
To elaborate, let's check the property of the comma operator, directly quoting the standard, C11, chapter §6.5.17, (emphasis mine)
The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.
So, essentially, a return statement like
return (kk,ll);
is same as
return ll;
Hope you can figure out the other case.
That said, the recommended signature for main() is int main(int argc, char*argv[]) or , at least, int main(void).
What do you expect when returning an int? C does not support tuples.
You are using the comma-operator. (ll, kk) is a single expression with the inner expressions (seperated by , - thus the name) being evaluated left to right. All but the rightmost (you can have more than two sub-expressions) results are discarded and the rightmost result is the result of the whole expression. Actually the parenthesis are unnecessary and do not change anything.
You are using the comma operator.
The comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).
Could someone please explain what this does and how it is legal C code? I found this line in this code: http://code.google.com/p/compression-code/downloads/list, which is a C implementation of the Vitter algorithm for Adaptive Huffman Coding
ArcChar = ArcBit = 0;
From the function:
void arc_put1 (unsigned bit)
{
ArcChar <<= 1;
if( bit )
ArcChar |= 1;
if( ++ArcBit < 8 )
return;
putc (ArcChar, Out);
ArcChar = ArcBit = 0;
}
ArcChar is an int and ArcBit is an unsigned char
The value of the expression (a = b) is the value of b, so you can chain them this way. They are also right-associative, so it all works out.
Essentially
ArcChar = ArcBit = 0;
is (approximately1) the same as
ArcBit = 0;
ArcChar = 0;
since the value of the first assigment is the assigned value, thus 0.
Regarding the types, even though ArcBit is an unsigned char the result of the assignment will get widened to int.
1 It's not exactly the same, though, as R.. points out in a comment below.
ArcChar = ArcBit = 0;
The assignment is left-associative, so it's equivalent to:
ArcChar = (ArcBit = 0);
The result of ArcBit = 0 is the newly-assined value, that is - 0, so it makes sense to assign that 0 to ArcChar
It sets both variables to zero.
int i, j;
i = j = 0;
The same as writing
int i, j;
j = 0;
i = j;
or writing
int i, j;
i = 0;
j = 0;
That is just chaining of the assignment operator. The standard says in 6.5.16 Assignment operators:
An assignment operator shall have a modifiable lvalue as its left operand.
An assignment operator stores a value in the object designated by the left operand. An
assignment expression has the value of the left operand after the assignment, but is not an
lvalue. The type of an assignment expression is the type of the left operand unless the
left operand has qualified type, in which case it is the unqualified version of the type of
the left operand. The side effect of updating the stored value of the left operand shall
occur between the previous and the next sequence point.
So you may do something like:
a=b=2; // ok
But not this:
a=2=b; // error
An assignment operation (a = b) itself returns an rvalue, which can be further assigned to another lvalue; c = (a = b). In the end, both a and c will have the value of b.
It assigns ArcBit to 0, then assigns ArcChar to the value of the expression ArcBit = 0 (ie. 0)
In some languages, assignments are statements: they cause some action to take place, but they don't have a value themselves. For example, in Python1 it's forbidden to write
x = (y = 10) + 5
because the assignment y = 10 can't be used where a value is expected.
However, C is one of many languages where assignments are expressions: they produce a value, as well as any other effects they might have. Their value is the value that is being assigned. The above line of code would be legal in C.
The use of two equals signs on one line is interpreted like this:
ArcChar = (ArcBit = 0);
That is: ArcChar is beging assigned the value of ArcBit = 0, which is 0, so both variables end up being 0.
1 x = y = 0 is actually legal in Python, but it's considered a special-case of the assignment statement, and trying to do anything more complicated with assignments will fail.
Assignment in C is an expression, not statement. Also you can freely assign values of different size (unsigned char to int and vice versa). Welcome to C programming language :)
You can do this:
http://en.wikibooks.org/wiki/C_Programming/Variables
Moreover,
[a int] = 0; is possible.
[a char] = 0; is possible too.
arcbit and arcchar equals 0.
As Hasturkun said, this is due to operator associativity order
C Operator Precedence and Associativity
The C Operator Preference Table notes the higher precedence of ().
Code:
# include <stdio.h>
int main()
{
int temp=2;
(temp += 23)++; //Statement 1
++(temp += 23); //Statement 2
printf("%d",temp);
return 0;
}
My question is while parentheses has higher precedence than pre-fix operator in Statement 2 why there's an error.
In Statement 1 both has same precedence but order of evaluation is from left to right. Still the same error.
Third doubt: operator += has much lower precedence, then why it's causing error.
error: lvalue required as increment operand
An lvalue is a value that some other value can be assigned to (because it is on the left side of the assignment operator). (temp += 23) is a rvalue. Nothing can be assigned to it.
Something else I'd like to add, is that it looks like you're trying to modify a value more than once in an expression. That's undefined behavior according to C99 standard 6.5(2).
Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression. Furthermore, the prior value
shall be read only to determine the value to be stored.
And footnote 71) shows the example:
This paragraph renders undefined statement expressions such as
i = ++i + 1;
a[i++] = i;
while allowing
i = i + 1;
a[i] = i;
When the evaluation of l-value precedes the evaluation of r-value and the assignment also returns a value, which of the following is evaluated first?
int i = 2;
int x[] = {1, 2, 3};
int y[] = {4, 5, 6};
int z[] = {7, 8, 9};
x[--i] = y[++i] = z[i++]; // Out of bound exception or not?
NOTE: generic C-like language with l-value evaluation coming first. From my textbook:
In some languages, for example C,
assignment is considered to be an
operator whose evaluation, in addition
to producing a side effect, also
returns the r-value thus computed.
Thus, if we write in C:
x = 2;
the evaluation of such a command, in
addition to assigning the value 2 to x,
returns the value 2. Therefore, in C,
we can also write:
y = x = 2;
which should be interpreted as:
(y = (x = 2));
I'm quite certain that the behaviour in this case is undefined, because you are modifying and reading the value of the variable i multiple times between consecutive sequence points.
Also, in C, arrays are declared by placing the [] after the variable name, not after the type:
int x[] = {1, 2, 3};
Edit:
Remove the arrays from your example, because they are [for the most part] irrelevant. Consider now the following code:
int main(void)
{
int i = 2;
int x = --i + ++i + i++;
return x;
}
This code demonstrates the operations that are performed on the variable i in your original code but without the arrays. You can see more clearly that the variable i is being modified more than once in this statement. When you rely on the state of a variable that is modified between consecutive sequence points, the behaviour is undefined. Different compilers will (and do, GCC returns 6, Clang returns 5) give different results, and the same compiler can give different results with different optimization options, or for no apparent reason at all.
If this statement has no defined behaviour because i is modified several times between comsecutive sequence points, then the same can be said for your original code. The assignment operator does not introduce a new sequence point.
General
In C, the order of any operation between two sequence points should not be dependent on. I do not remember the exact wording from the standard, but it is for this reason
i = i++;
is undefined behaviour. The standard defines a list of things that makes up sequence points, from memory this is
the semicolon after a statement
the comma operator
evaluation of all function arguments before the call to the function
the && and || operand
Looking up the page on wikipedia, the lists is more complete and describes more in detail. Sequence points is an extremely important concept in C and if you do not already know what it means, do learn it immediately.
Specific
No matter how well defined the order of evaluation and assignment of the x, y and z variables are, for
x[--i] = y[++i] = z[i++];
this statement cannot be anything but undefined behaviour because of the i--, i++ and i++.
On the other hand
x[i] = y[i] = z[i];
is well defined, but I am not sure what the status for the order of evaluation for this. If this is important however I would rather prefer this to be split into two statements along with a comment "It is important that ... is assigned/initialized before ... because ...".
i think its the same as
x[3] = y[4] = z[2];
i = 3;