C enums 📅

preview_player
Показать описание
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("\nIt's the weekend! Party time!");
}
else
{
printf("\nI have to work today :(");
}

return 0;
}
Рекомендации по теме
Комментарии
Автор

#include <stdio.h>

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("\nIt's the weekend! Party time!");
}
else
{
printf("\nI have to work today :(");
}

return 0;
}

BroCodez
Автор

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

masterschlib
Автор

this guy explained my 2h class in 4 minuts

omarmayrec
Автор

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

dowisvo
Автор

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

gabrieleymat
Автор

// 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]);

liamjones
Автор

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?

Miracle-uces
Автор

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

tpespos
Автор

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

kaanbaskaya
Автор

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

Endersoldier
Автор

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

RenatoSantos-myft
Автор

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

tynado.o
Автор

Here's some code I made with enums! :

#include <stdio.h>

#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <ctype.h>

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!");
}
}

PSIwolf