2,355 views
2 votes
2 votes
int main()
{
     int a = 1, b = 2, c = 3;
     printf("%d", a += (a += 3, 5, a));
}

How is this evaluated ?

2 Answers

1 votes
1 votes
#include <stdio.h>

int main(void) {
	int a = 1, b = 2, c = 3;
	a =a+ 3, 5, a;// Comma (separate expressions) has least precedence in C so a=1+3; so a=4
	a =a+ a; //() has highest precedence are evaluated first so 4+4 see link below
    printf("%d", a);
	return 0;
}

please check the link:https://www.geeksforgeeks.org/c-operator-precedence-associativity/ 

I hope it will clear your doubt.

edited by
0 votes
0 votes

Ok so first let me list out all operators used here..

  1. ()
  2. +=
  3. ,

Now above operator list I wrote, it is as per C's precedence rule here

So first, it let's take () part,

(a+=3,5,a) =>  As we know, comma proceeds in left-to-right, so this is equal to (a=4,5,a),

Now from above we can say a is having value 4 and we have used parenthesis here, it will take last value which is not but a so finally

     a+= a

=> a+=4

=> a=a+4

and we know a=4 hence

=>a = 4+4=8

so answer is 8. 

Related questions

678
views
1 answers
7 votes
GO Classes asked Feb 5
678 views
Consider the following C code:double A[2][3] = {{1, 2, 3}, {4, 5, 6}};Assume that A[0] = 0xFFAA0000 and the sizeof(double) is 8.What will be the value of `A[1]`?0xFFAA00240xFFAA00030xFFAA000C0xFFAA0018
458
views
2 answers
6 votes
GO Classes asked Feb 5
458 views
What is a statically allocated variable in C programming? A variable allocated at an absolute address in a program's data spaceA variable allocated on the ... allocated on the heapA variable which can NOT be defined as local variable
510
views
2 answers
7 votes
GO Classes asked Feb 5
510 views
On a $64$-bit system, which of the following C expressions is equivalent to the C expression $(x[2]+4)[3]?$ Assume $\mathrm{x}$ is declared as $\textsf{int}\ast \ast \textsf{x}$\ast(( ... $(* \ast(x+2)+7)$
496
views
1 answers
5 votes
GO Classes asked Feb 5
496 views
The provided C code is a version of the C string library function strlen(), which calculates the length of a given string.unsigned int mystrlen(char *c) { unsigned int i = 0; ... i++; return i;while (*(c + i) != '\0') ++i; return i;