+ 1

How this code run.

char f1 (char c) { return c=='z' ? 'a' : c+1; } c=f1(c); return c; } int main () { char x='x'; cout<<f2(x); cout<<f2(x); cout<<f2(x); }

19th Dec 2017, 4:20 PM
Deep Prakash Goyal
Deep Prakash Goyal - avatar
1 Answer
0
This doesn't run. // This is an incorrect call to the function and is also called outside of main, which you can't do. c was also not declared as a char yet. If you move this inside of the function f1, then f1 becomes recursive. But this would never be executed if it comes after the return inside of f1. c=f1(c); //This return is not within a function and has no purpose. Because c was not declared yet, this will also be an error return c; //This closing bracket is not closing anything. It is an extra bracket and will cause issues } //In main a function called f2 is called, which doesn't exist cout << f2(x) To fix this, you can have the following: char f1 (char c) { cout << c << endl; return c == 'z' ? 'a' : c + 1; } int main() { char x = 'x'; x = f1(x);//outputs 'x', x = 'y' x = f1(x);//outputs 'y', x = 'z' x = f1(x);//outputs 'z', x = 'a' cout << x << endl;//outputs 'a' return 0; } https://code.sololearn.com/cPukOKDTM2X1
19th Dec 2017, 11:52 PM
Zeke Williams
Zeke Williams - avatar