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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


1


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