0

Can someone walk me through line 42 in speller.c?
I don't remember this syntax anywhere from the course and I can't figure it out.

char* dictionary = (argc == 3) ? argv[1] : DICTIONARY;

What does the question mark do? What does the colon do?

2
  • this is the ternary operator.
    – kzidane
    May 24 '15 at 16:26
  • Thanks Kareem. It is in Kochan, Chapter 6 which I read but did not recall when it was time to use it. May 25 '15 at 2:17
4

As Kareem pointed out, this syntax is the ternary operator. This is covered in Chapter 6 of "Programming in C" by Kochan. The ternary operator is a more compact way to implement the common "if then else" structure.

The part before the question mark is the conditional. If this conditional is true (or non-zero) then the value is the expression between the question mark and the colon. If the conditional is false (or zero) then the value is the expression after the colon.

conditional ? true outcome : false outcome

The string returned from the expression:

(1 == 1) ? "first" : "second"; 

is "first".

The string returned from the expression:

(1==10) ? "first" : "second";

is "second".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .