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;
}
#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;
}
❤
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
Thanks for the tip!
Thanks :)
thx bro
this guy explained my 2h class in 4 minuts
ur about to save my comp 1 grade bro
thx a lot bro, u just saved me 15 min. good luck
// 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]);
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;
}
Good❤
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?
Because then you can do like: enum Day schedule1; enum Day schedule2; etc.
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.
thanks broooooooooo
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
thank you!
nice man
Still not sure why you would use Enum over just declaring all the days of the week as integer variables
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.
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.
@@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.
It makes it more readable for other people who are looking at your code
Mostly just aesthetics
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.
uugh, I have just realized it's C enums not C# enums
Great video but isn't monday the first day of the week? Or is that just an American thing?
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.
@@Neiv74 No bro, Monday is the first in Europe, sunday being the last
There's a reason why Saturday and Sunday are called "the week-end"
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
Use ToString method
hi kevin
Hey Bro, why do enum constant identifiers like (Sun) can only be defined once within their scope?
Coz we cant declare many values to a same variable identifier.......
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.
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!");
}
}