0
#include <stdio.h>
#include <stdbool.h>
typedef enum{
    False,
    True
}Bool;
int main(int argc, char const *argv[])
{
    int a = 1, d = 1;
    Bool b = False, c = True;
    Bool ans;
    if (a == d)
    {
        ans = c;

    }
    else
    {
        ans = b;
    }
    printf("The answer is: %i \n", ans);
    return 0;
}

I think it is only returning the execution result 0 or 1. I want it to return the values from the enum which I created.

CEXDSINGH
  • 23
  • 6

3 Answers3

3

If you want it to print the string true or false you'll have to add something like this:

printf("The answer is: %s\n", ans ? "true" : "false");
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
2

From the C Standard, § 6.7.2.2:2-3:

The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted.) An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value ... is 0. Each subsequent enumerator with no = defines its ... value ... by adding 1 to the value of the previous enumeration constant.

Emphasis mine.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
1

In C an enum is no more than an int type but with special symbols for a subset of those ints. Also, unless you tell it otherwise, the implicit int value for the first enum member is 0, the second is 1, and so on.

%i can be used to print an int, which accounts for the output.

It is not possible to display the actual enum token in portable C, although there are idiomatic approaches that make use of the preprocessor.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • The enum token can only be used in the program text. – Paul Ogilvie Nov 21 '18 at 15:27
  • Token names may also be available in debugging databases, depending on build settings (like a `.pdb` file on Windows) – Govind Parmar Nov 21 '18 at 15:36
  • A `enum` does not have to be a `int`. – 12431234123412341234123 Oct 12 '20 at 12:29
  • A `enum` does no have to a `int` but can also be any integer type, which it can represent all values. *Each enumerated type shall be compatible with `char`, a signed integer type, or an unsigned integer type. The choice of type is implementationdefined, 128) but shall be capable of representing the values of all the members of the enumeration. ...* From 6.7.2.2 Enumeration specifiers - 4 – 12431234123412341234123 Oct 12 '20 at 12:34