-3
int foo(char c)
{
  switch(c)
  {
    case '1':
     return(1);
    case '2':
    return(2);
  } 
}

int main()
{
cout << foo('a');
}

This program prints 97 .

** It prints the ASCII value as output if none of switch case matches. Why is the function returning the ASCII value if none of the cases match**

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
Kishore
  • 53
  • 6
  • 3
    Undefined beahviour. You have to return allways a value; –  Jul 27 '17 at 08:55
  • 1
    And what do you return from `foo` if none of the cases in the `switch` is matched? Not returning anything from a function declared to do that leads to *undefined behavior*, which makes the whole program *ill-formed* and invalid. – Some programmer dude Jul 27 '17 at 08:55
  • 1
    Don;t make `return` look like a function. – Sourav Ghosh Jul 27 '17 at 08:59
  • Please enable compiler warnings. *warning C4715: 'foo': not all control paths return a value* – Weather Vane Jul 27 '17 at 09:01
  • @SouravGhosh why? it's a one of the conventions (one I don't like, but still) – CIsForCookies Jul 27 '17 at 09:04
  • @CIsForCookies Well, just because we can write `[5]array` and it's valid every bit, we don't use that, right? Similar here. Also, can you cite the source of the convention you mentioned, I'd like to see that. – Sourav Ghosh Jul 27 '17 at 09:06
  • @SouravGhosh [5]array is confusing. return (1) is very simple to understand... I hope you don't think that using parentheses when not necessary is bad (for example: _sizeof 1.1_ vs. _sizeof(1.1)_ ) – CIsForCookies Jul 27 '17 at 09:10
  • 1
    @CIsForCookies Still, I'd stick to my statement. YMMV. :) – Sourav Ghosh Jul 27 '17 at 09:12
  • @CIsForCookies AFAIK there is a problem optimizing the return. But I don't remeber what exactly it was. Maybe RVO or something like that. –  Jul 27 '17 at 09:16
  • @CIsForCookies [a good read](https://stackoverflow.com/q/161879/2173917). – Sourav Ghosh Jul 27 '17 at 09:18
  • @manni66 I looked around and found [this](https://softwareengineering.stackexchange.com/questions/238786/does-c-compiler-remove-optimize-useless-parentheses) – CIsForCookies Jul 27 '17 at 11:22
  • @SouravGhosh I also saw that :). I guess there's no accounting for taste, so both are fine – CIsForCookies Jul 27 '17 at 11:24

3 Answers3

2

Your program behaviour is undefined: you must have an explicit return value on any function returning an int (other than main).

(What you appear to be observing is a corruption of the stack; 97 is the ASCII value for the lower case letter a that you've passed as the function parameter).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1
int foo(char c) {
    switch(c) {
        case '1':
          return(1);
        case '2':
          return(2);

        //code to execute if 'c' does not equal the value following any of the cases
        default:
          return(-1);
    } 
}

int main() {
    cout << foo('a');
}

OR

// simple char to int without switch
int foo(char c) {

   /*
   // reject unwanted char 
   if (c=='a') {
       return -1;
   }
   */

   // convert char to int
   return (int)c;
}
Ihdina
  • 950
  • 6
  • 21
1

What you have here is UB. The foo function gets an input it doesn't handle, since none of your cases support input 'a'. What's probably happening under the hood (if I may try to explain this UB in your specific case) is that foo returns the input it gets casted to int, meaning cout << (int)('a');

Your switch case should contain a default for such cases, for example:

int foo(char c)
{
  switch(c)
  {
    case '1':
      return(1);
    case '2':
      return(2);
    default:
      return (-1); // indicates error!!
  } 
}

int main()
{
int tmp = foo('a');
if (tmp != -1)
    cout << tmp;
else
    cout << "bad input";
}
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124