C enums 📅

Поділитися
Вставка
  • Опубліковано 20 сер 2024
  • C enums tutorial example explained
    #C #enums #enumerations
    enum Day{Sun = 1, Mon = 2, Tue = 3, Wed = 4, Thu = 5, Fri = 6, Sat = 7};
    int main()
    {
    // enum = a user defined type of named integer identifiers
    // helps to make a program more readable
    enum Day today;
    today = Sun;
    if(today == Sun || today == Sat)
    {
    printf("
    It's the weekend! Party time!");
    }
    else
    {
    printf("
    I have to work today :(");
    }
    return 0;
    }

КОМЕНТАРІ • 39

  • @BroCodez
    @BroCodez  2 роки тому +26

    #include
    enum Day{Sun = 1, Mon = 2, Tue = 3, Wed = 4, Thu = 5, Fri = 6, Sat = 7};
    int main()
    {
    // enum = a user defined type of named integer identifiers
    // helps to make a program more readable
    enum Day today;
    today = Sun;
    if(today == Sun || today == Sat)
    {
    printf("
    It's the weekend! Party time!");
    }
    else
    {
    printf("
    I have to work today :(");
    }

    return 0;
    }

  • @masterschlib
    @masterschlib Рік тому +72

    just a comment for anyone who may be interested, at 1:15 you assigned each enumeration a value, Sun = 1, Mon = 2 Tues = 3 etc. However, if you just assign Sun = 1, the following enumerations will be iterated automatically by + 1, so there is no need to manually assign each value after 'Sun' :)
    Furthermore, if you manually assigned e.g. Sun 1, Mon would be auto assigned with 2. and say after that you manually assigned Tue with 20, Wed would be auto assigned 21, Thu 22, Fri 23 etc. just something i noticed hahah, you make great videos which i find very helpful! thank you

    • @FlowMo
      @FlowMo 5 місяців тому +3

      Thanks for the tip!

    • @carbyte2673
      @carbyte2673 4 місяці тому

      Thanks :)

    • @DrickRT
      @DrickRT 13 днів тому

      thx bro

  • @omarmayrec4277
    @omarmayrec4277 Рік тому +8

    this guy explained my 2h class in 4 minuts

  • @qqutee
    @qqutee 2 роки тому +13

    ur about to save my comp 1 grade bro

  • @user-do8wi4sv6o
    @user-do8wi4sv6o 6 місяців тому

    thx a lot bro, u just saved me 15 min. good luck

  • @liamjones2131
    @liamjones2131 Рік тому +5

    // So, say I left the numbers out;
    enum Day{Sun, Mon, Tues, Wed, Thurs, Fri, Sat};
    int x = 2,
    //Could I then print out the string rather than the number?
    //For example...
    printf("%s is the day today.", Day[x]);

    • @masterschlib
      @masterschlib Рік тому +2

      You could use something like this, there may be better solutions but this is what i thought of.
      // takes in user input 1-7, prints corresponding day of week.
      #include
      enum Day
      {
      SUN = 1,
      MON,
      TUE,
      WED,
      THU,
      FRI,
      SAT
      };
      int main()
      {
      enum Day day_of_week;
      printf("Whats the day today (1-7)? ");
      scanf("%d", &day_of_week);
      switch(day_of_week)
      {
      case SUN:
      printf("Today is Sunday");
      break;
      case MON:
      printf("Today is Monday");
      break;
      case TUE:
      printf("Today is Tuesday");
      break;
      case WED:
      printf("Today is Wednesday");
      break;
      case THU:
      printf("Today is Thursday");
      break;
      case FRI:
      printf("Today is Friday");
      break;
      case SAT:
      printf("Today is Saturday");
      break;
      }
      return 0;
      }

  • @desetff9298
    @desetff9298 Місяць тому

    Good❤

  • @Miracle-uc6es
    @Miracle-uc6es Рік тому +3

    I didnt get it what's the benefit of this actually.
    you say
    enum Sun = 0;
    if (today == Sun)
    prinf("It's weekend)
    Okay I get that but
    I can define a variable today as char, and I can define a string called "Sun"
    So, I can the make following.
    char today[12] = Sun;
    if (today == "Sun" || today == "Sat")
    printf("It's weekend);
    Why should I use enum, rather than that?

    • @Runeite51
      @Runeite51 11 місяців тому +2

      Because then you can do like: enum Day schedule1; enum Day schedule2; etc.

    • @beatboy6690
      @beatboy6690 4 місяці тому

      An enum is just syntactic sugar, sure it doesn't do anything strings can't but sometimes using enums creates a solution that is just cleaner, easier to read, just better. That's the same reason you might use a switch statement instead of an if chain. Sometimes it just makes clearer, better code, other times it might make worse code. For example I could never ever imagine using your solution in a compiler where you have to match a large variety of text from an input file and match them with a type of token. Being able to define every single type of token and their corresponding command in an enum is just a nicer experience than using a string with if statements. A general rule of thumb is if you are matching a large variety of text enums are likely to be better, if you are only matching a few phrases or even just one word strings will probably be nicer.

  • @toxiclucien8168
    @toxiclucien8168 2 роки тому +1

    thanks broooooooooo

  • @gabrieleymat6332
    @gabrieleymat6332 6 місяців тому +1

    Does it mean an enum is just some kind of define?
    If you can say today == Mon, it means that Mon is equal to 2 in all of the code, then what is the point of declaring the today variable by "enum day today"
    I maybe overcomplicating the thing, but it just feels... Weird

  • @MerryMerry632
    @MerryMerry632 8 місяців тому

    thank you!

  • @sulaimanatayi555
    @sulaimanatayi555 Рік тому

    nice man

  • @tpespos
    @tpespos Рік тому +7

    Still not sure why you would use Enum over just declaring all the days of the week as integer variables

    • @sheaton319
      @sheaton319 9 місяців тому +1

      This is my exact question. I have lecture to watch on enums so maybe my instructor will give a reason. If so, I'll post another comment here. Maybe more concise? But also more complicated to use enums.

    • @Turplemaple6318
      @Turplemaple6318 8 місяців тому +4

      You only have 2 cities "AB" and "BA" each take 2 bytes each to store, so if you want to assign these cities to 10 people will cost 20 bytes instead enums can identify two cities using 0 or 1 so you can assign cities to all in just 10 bytes.

    • @Turplemaple6318
      @Turplemaple6318 8 місяців тому

      ​@@sheaton319You only have 2 cities "AB" and "BA" each take 2 bytes each to store, so if you want to assign these cities to 10 people will cost 20 bytes instead enums can identify two cities using 0 or 1 so you can assign cities to all in just 10 bytes.

    • @lorenzzz8630
      @lorenzzz8630 5 місяців тому

      It makes it more readable for other people who are looking at your code
      Mostly just aesthetics

    • @user-or6mz4gy6i
      @user-or6mz4gy6i 5 місяців тому

      You can call the number and get the variable name using ToString method. This is not possible using an integer assignation. You could create a dictionary/ array instead, but enums are value types so they are faster, and lighter.

  • @tynado.o
    @tynado.o 14 днів тому

    uugh, I have just realized it's C enums not C# enums

  • @kaanbaskaya3827
    @kaanbaskaya3827 10 місяців тому

    Great video but isn't monday the first day of the week? Or is that just an American thing?

    • @Neiv74
      @Neiv74 7 місяців тому

      no, monday is not the first day of the week, is sunday. Monday is the second day in america too, its not an american thing, until I know all others countries start work/school at monday even tho its the second day of the week, with saturday being the last.

    • @kaanbaskaya3827
      @kaanbaskaya3827 7 місяців тому +5

      @@Neiv74 No bro, Monday is the first in Europe, sunday being the last

    • @user-or6mz4gy6i
      @user-or6mz4gy6i 5 місяців тому

      There's a reason why Saturday and Sunday are called "the week-end"

  • @RenatoSantos-my3ft
    @RenatoSantos-my3ft 10 місяців тому

    I can't get the enum name using the numbers? I mean, display in the screen that it's sunday just using the number 1

  • @gumbo64
    @gumbo64 6 місяців тому

    hi kevin

  • @Endersoldier504
    @Endersoldier504 2 роки тому +2

    Hey Bro, why do enum constant identifiers like (Sun) can only be defined once within their scope?

    • @madhanprasathr247
      @madhanprasathr247 2 роки тому

      Coz we cant declare many values to a same variable identifier.......

    • @Runeite51
      @Runeite51 11 місяців тому

      that is what a constant means, it's constantly that value, so saying "constant variable" is actually an oxymoron because those are two opposite words. It would be more correct to refer to it as "a constant" as it is not something that can vary. Except for the separate constants themselves, there's different constants so I guess that varies.

  • @PSIwolf39
    @PSIwolf39 8 місяців тому +2

    Here's some code I made with enums! :
    #include
    #include
    #include
    #include
    #include
    typedef enum{english = 1, spanish = 2, french = 3, german = 4} language;
    int main(){
    language userLanguage = english;
    if(userLanguage == english){
    printf("Hello!");
    }
    if(userLanguage == spanish){
    printf("Hola!"); // Technically in spanish you're suposed to put the upside down exclamation mark(¡) in front of this but sadly it does not print correctly if that symbol is used.
    }
    if(userLanguage == french){
    printf("Bonjour!");
    }
    if(userLanguage == german){
    printf("Guten tag!");
    }
    }