2

This is my C program:

#include <stdio.h>
#include <string.h>

int main(void) {

    char nama[25];
    enum Jeniskelamin genders;
    strcpy(nama, "Mira");
    genders = 1;

    printf("Nama \t\t: %s\n", nama);
    printf("Jenis kelamin \t: %d", genders);

    return 0;
}

And I get this error while compiling this .c source file

enum.c: In function ‘main’:
enum.c:7:20: error: storage size of ‘genders’ isn’t known
enum Jeniskelamin genders;
                  ^

What is wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Junaruse
  • 21
  • 2
  • 1
    Is this `enum Jeniskelamin genders;` C code? Also assigning an integer to this "thing" you're trying to create... What are you trying to accomplish with this line of code `enum Jeniskelamin genders;`? – nbro Apr 06 '17 at 23:52
  • http://stackoverflow.com/questions/1102542/how-to-define-an-enumerated-type-enum-in-c – pm100 Apr 07 '17 at 00:06
  • I hit the upvote on the wrong thing and down voting didn't do what I wanted. At any rate, @Cody has the answer. You've declared an enumeration but not defined one. Thus, there isn't any known storage type/size. – Andrew Falanga Apr 07 '17 at 00:12

1 Answers1

4

You never define what the enum type can possibly be. When you declare an enum type, you must know what the possible values can be, so it knows how much data might be needed (one byte, two bytes, four bytes, etc). So you must define your enum type before you can use it as a type. Your code should look something like this:

#include <stdio.h>
#include <string.h>

enum Jeniskelamin {Male, Female, Nonbinary};

int main(void) {

    char nama[25];
    enum Jeniskelamin genders;
    strcpy(nama, "Mira");
    genders = 1;

    printf("Nama \t\t: %s\n", nama);
    printf("Jenis kelamin \t: %d", genders);

    return 0;
}

At this point, Male is automatically defined to 0, Female to 1, and Nonbinary to 2. You can change them to Male = 7 if you need that to be 7, for example. At this point, it is more clear for you to change genders = 1 to genders = Female if that is your intent. It just reads better this way.

Cody
  • 2,643
  • 2
  • 10
  • 24