0

In the book these is a simple C function:

char *month_name(int n)
{
  static char *name[] = {
  "Illegal month",
  "January", "February", "March",
  "April", "May", "June",
  "July", "August", "September",
  "October", "November", "December"
  };

  return (n < 1 || n > 12) ? name[0] : name[n];
}

What does this function do is easy to understand, but I don't understand the return statement, how does this return statement work here.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
user3289218
  • 87
  • 1
  • 5
  • 3
    Which part don't you understand? The `? :` operator? – Yu Hao Sep 07 '14 at 09:23
  • What in that statement do you not understand? – Jens Gustedt Sep 07 '14 at 09:23
  • 3
    The `?:` is called the **conditional operator**. Many people *(wrongly)* call it the **ternary operator** because it is composed of 3 sub-expressions; but the C Standard does not have that name in it anywhere. – pmg Sep 07 '14 at 09:28
  • @pmg: *The ternary operator* is correct (the definite particle is important there): It is the only ternary operator in the language. The fact that it is the *conditional operator* does not make it wrong. – Deduplicator Sep 07 '14 at 09:37
  • @Deduplicator: that's why i put wrongly between parenthesis and italicized. Anyway, I prefer to call it the conditional operator so that when a new ternary operator is introduced to the language I don't have to switch gears :) – pmg Sep 07 '14 at 09:49
  • 2
    That's a very verbose way of writing it. Shorter would be: `return name[n < 1 || n > 12 ? 0 : n];` – Kerrek SB Sep 07 '14 at 09:51
  • possible duplicate of [What does the question mark and the colon (?: ternary operator) mean in objective-c?](http://stackoverflow.com/questions/2595392/what-does-the-question-mark-and-the-colon-ternary-operator-mean-in-objectiv) – Joe Sep 07 '14 at 11:18

7 Answers7

3

Its a ternary operator..

condition? a: b

this means if condition is true then expression a wil be processed, if its false then b will be processed.

In your code, it will check if n is not within the range 1-12 (the number of months). if it is not within the range then it will return name[0] (i.e. illegal month). else if it is within the range then it will print the name of the month using n as the index of the array name.

Haris
  • 12,120
  • 6
  • 43
  • 70
2

return (n < 1 || n > 12) ? name[0] : name[n]; could be rewritten as:

if (n < 1 || n > 12)
    return name[0];
else
    return name[n];

So function returns name of month for n = 1...12 passed to function, otherwise it returns "illegal month".

macfij
  • 3,093
  • 1
  • 19
  • 24
1

? : is ternary operator.

If n < 1 || n > 12 it will return "Illegal month" else name of the month.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
ani627
  • 5,578
  • 8
  • 39
  • 45
1

It's called a ternary operator, and it means: if n < 1 or n > 12, return name[0] - "Illegal month". Else, return name[n] - the proper month.

Eutherpy
  • 4,471
  • 7
  • 40
  • 64
1

It's equivalent to the following:

if(n < 1 || n > 12)
    return name[0];
else
    return name[n];
godel9
  • 7,340
  • 1
  • 33
  • 53
1

Expanded, that return statement is equivalent to this:

if (n < 1 || n > 12)
{
    return name[0];
}
else
{
    return name[n];
}

The return statement employs a ternary operation which has the following syntax:

(if condition)? true_return_value: false_return_value;
Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
0

While many have pointed out that this is the same as:

if(n < 1 || n > 12)
    return name[0];
else
    return name[n];

that's not exactly true. The ternary operator is an operator, so that (n < 1 || n > 12) ? name[0] : name[n] is an expression, not a statement. A closer example would be:

string ret_value;

if(n < 1 || n > 12)
    ret_value = name[0];
else
    ret_value =name[n];

return ret_value;

This becomes important when you need the conditional in the middle of things:

 return String.Format("The test {0} successful", bOK ? "WAS" : "was NOT");
James Curran
  • 101,701
  • 37
  • 181
  • 258