Can we use function on left side of an expression in C and C++?

2.4

In C, it might not be possible to have function names on left side of an expression, but it’s possible in C++.

#include<iostream>

using namespace std;

/* such a function will not be safe if x is non static variable of it */
int &fun()
{
   static int x; 
   return x;
}    

int main()
{
   fun() = 10;

   /* this line prints 10 on screen */
   printf(" %d ", fun());

   getchar();
   return 0;
}

Please write comments if you find anything incorrect or want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.4 Average Difficulty : 2.4/5.0
Based on 14 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.