-1

(This is a very basic question, but I can't seem to find an answer because it involves ||, which is a problematic search term.)

Since a char can be interpreted as a boolean in a conditional like this:

if (str[i]) ...  
if (c) ...  

where '\0' is considered falsey and any other character is truthy, I was expecting this to work too:

char c = str[i] || 'x';

but it's always giving a value of 1. Is there a simple shorthand in C for this? I've become used to it in JavaScript, and I find it clear and concise.

  • 1
    Use the ternary operator. – XCS Aug 14 '16 at 22:36
  • 1
    About the shortest you're going to get is `char c = str[i] ? str[i] : 'x';`. – Oliver Charlesworth Aug 14 '16 at 22:37
  • @OliverCharlesworth I was hoping to avoid repeating str[i], but I guess the overhead is only visual, and therefor unimportant. – m69's been on strike for years Aug 14 '16 at 22:38
  • The problem is that JS handles boolean expressions fundamentally different. It only works because in JS, any expression can be coerced into a boolean. In C, this is simply not the case. – Ingo Bürk Aug 14 '16 at 22:42
  • @IngoBürk - Depends what you mean. In C, anything that equates to 0 is effectively equivalent to `FALSE` in relevant situations (i.e. `if`, operands to boolean operators, conditional operator). That applies to all types. – Oliver Charlesworth Aug 14 '16 at 22:49
  • 1
    the `||` returns either 1 (if either operand is true) or 0 (if both are false), and it's that returned result you're assigning to `c`. You *could* do `char c; (c = str[i]) || (c = 'x');`, but that would do `c = str[i]` regardless of its value and follow with `c = 'x'` only if it was false. – Dmitri Aug 14 '16 at 22:52
  • 1
    See also http://stackoverflow.com/q/16925429/583456 for a link to GCC extension which allows for `char c = str[i++] ?: 'x';`. – mike.dld Aug 14 '16 at 22:54
  • What was the problem getting a list of C operators and learning what the `||` operator means? Best would be, of course to get a C book. Seraching for `||` is definitively not the only way! Any reason you expect two very different languages behave the same? – too honest for this site Aug 14 '16 at 22:59
  • @Olaf Well, I read "C Primer Plus" in 1996, but that's a long time ago, and I haven't used C much since. – m69's been on strike for years Aug 14 '16 at 23:04
  • @m69: The meaning of that operator has noit changed much. Though you should read a more recent book. Anyway, you apparently did no research on your own. – too honest for this site Aug 14 '16 at 23:09
  • Well here I was thinking I'd do someone a favour by adding a code example to an answer; I won't be doing that again anytime soon. – m69's been on strike for years Aug 14 '16 at 23:44

2 Answers2

2

The ternary operator is what you are looking for:

char c = str[i] ? str[i] : 'x';

Exaplanation: if str[i] is truthy, assign value of str[i] to variable c, other case assign letter 'x'.

jedrzejginter
  • 402
  • 3
  • 10
1

You should handle it like any other abstraction, and write a function for it:

char non_zero_or_default(const char c, const char d)
{
    return c ? c : d;
}

then use it as:

char c = non_zero_or_default(str[i++], 'x');

You can inline it if you're worried about overhead.

Crowman
  • 25,242
  • 5
  • 48
  • 56