2021年3月12日星期五

Does this equivalency return a bool or int?

I would like to write the following function to return whether a queue is empty or not:

#include <stdbool.h>  bool QueueIsEmpty(Queue *pq)  {      return (pq->items == 0)? true : false;  }  

Would doing the following shorthand work for this?

bool QueueIsEmpty(Queue *pq)  {      return pq->items == 0;  }  

In other words, does that return an int or a bool when doing the equivalency check? And if it returns a bool how does the compiler know to cast it to a bool, or is a bool just an int behind-the-scenes?

When trying it in Compiler Explorer, both seem to just do mov eax, 0 and produce identical assembly: https://godbolt.org/z/bY59r3.

https://stackoverflow.com/questions/66610025/does-this-equivalency-return-a-bool-or-int March 13, 2021 at 12:05PM

没有评论:

发表评论