I was doing another K&R question and again my idea was quite different but I thought I would run it by you bright people.
The exercise is to rewrite an if else conditional using ?:
I realize that I don't need parentheses around the conditional as ?: precedence is low but K&R says it is good practice.
The solutions for this exercise that I have seen are slightly but significantly different using only one return at the beginning of the function and I understand those solutions but I was wondering if mine is wrong.
Original:
My idea.Code:int lower(int c) { if (c >= 'A' && c <= 'Z') return c + 'a' - 'A'; else return c; }
Code:int lower(int c) { (c >= 'A' && c <= 'Z') ? return c + 'a' - 'A' : return c; }



LinkBack URL
About LinkBacks



