1

Sorry if it's a stupid question, but I couldn't find much information. I just want to assign the result of a comparison in a variable, like this:

int a = 3, b = 2; // In actual code they're not integer literals
int result = a > b;

When compiling, gcc (with -Wall) doesn't complain, and looking at the assembly output I found it's translated to cmp and setle (or setg etc.). I'm wondering whether it's invalid (C) code or considered bad practice, since I see it's never used.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
lorenzownd
  • 45
  • 2
  • 7
  • `In actual code they're not integer literals` what are they in original code? – Iłya Bursov Mar 23 '16 at 21:23
  • 1
    The `>` and other such operators evaluate to an int, `0` or `1`. – M.M Mar 23 '16 at 21:24
  • 1
    It's valid, it's not bad practice, and [it is used](https://github.com/FFmpeg/FFmpeg/blob/99eabcdd5f904e98a02184889ab84cc9d5c17a8c/libavcodec/libopenjpegdec.c#L186-L189). – Cornstalks Mar 23 '16 at 21:24
  • @Lashane It was just to point out that `a` and `b` wouldn't be optimized out at compile time. They would be `int`s coming from functions or user input. – lorenzownd Mar 23 '16 at 21:35
  • Thanks to all for the quick replies! – lorenzownd Mar 23 '16 at 21:36
  • Just because they're the result of a function doesn't mean they won't be optimized out; inlining and link-time optimizations can still optimize them out. Not that it matters as long as your code is well defined. – Colonel Thirty Two Mar 23 '16 at 22:14

2 Answers2

3

This is a perfectly valid C code. The behavior is detailed in section 6.5.8.6 of the C99 standard:

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.

Unless you are maintaining legacy code that must be compatible with pre-C99 compilers, consider using <stdbool.h> and bool type instead of an int.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
-3

What @dasblinkenlight said is right. In addition to that, and I am not aware with your code, you may need to define the variable "result" as volatile to avoid compiler optimization, where 2 and 3 are magic values and already the result of comparison is known. So, try to replace :

int result = a > b;

with

volatile int result = a > b;

Read more about volatile usage.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Why would you want to avoid optimization – M.M Mar 23 '16 at 21:42
  • Why do you think that `volatile` might be relevant? The value assigned to `result` will be the value of the comparison when the assignment is executed. Even if `a` or `b` (or both) subsequently change, that will not change the value in `result`, whether it is marked `volatile` or not. Your 'read more about voltatile usage' line isn't a link, though the content suggests there should be a link there somewhere. – Jonathan Leffler Mar 23 '16 at 21:43
  • In case this variable used later; not assigning to it anymore just usage; this will be optimized. – Awad A. Bekhet Mar 23 '16 at 22:21
  • for better understanding, we should have the complete code. – Awad A. Bekhet Mar 23 '16 at 22:25
  • `volatile` actively hinders optimization. Using it will not help the code be optimized. Just the opposite. – Cornstalks Mar 24 '16 at 10:05
  • Actually, that's what I meant to say. – Awad A. Bekhet Mar 24 '16 at 14:14