- typedef
- enum
- In C language a user can define an identifier that represents an existing data type.
- The user defined datatype identifier can later be used to declare variables.
- Syntax is
typedef datatype identifier;
here datatype represents the existing primary or derived data type. - Example:
typedef int marks;
Now marks can be used to declare integer variables as
marks mark1,mark2;
- enum is short form of enumeration
- enumeration consists of a set of named integer constants. ie enumeration is a list of constant integer values.
- Syntax for enum declaration is
Enum identifier {value1,value2,.....value n} - The identifier is a user defined enumerated datatype which can be used to declare variables that have one of the values enclosed with in the braces.
- after the declaration,we can define variables with this new type as
enum identifier v1,v2,v3....vn - The enumerated variables v1,v2,v3...vn can have only one of the values value1,value2....value n.
- Example
declaration
enum day {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
definition
enum day day1,day2,day3;
day1=Monday;
day2=Friday;
day3=Sunday;//Error because sunday is not in the enum definition - The first name in an enum has value 0, the next has value 1 and so on,unless explicit values are specified.
- In the above example Monday is assigned to 0,Tuesday is assigned to 1 and so on and Saturday is assigned to 5 internally.
- we can also explicitly assign a value to the set
enum day {Monday,Tuesday=0,Wednesday,Thursday,Friday,Saturday};
The value 0 is associated with Monday by default.Then Tuesday is explicitly set to 0.so Wednesday is set to 1 and so on.
- To explicitly assign an integer value to a variable of an enumerated data type,use a type cast
enum day today;
today=(enum day)Monday;
now today has value 0 - In declaration, enum identifier {value1,value2,.....value n};value1,value2...valuen must all be distinct from each other and different from ordinary variable names,but the values need not be distinct.
enum student1 {Anand=0,Ashok,Alex} Boys;
enum student2 {skippy=0,slowvy,Ashok} Girls;
The above code will create an compiler error because 'Ashok' is available in both the list. - Enum is a set of named integer constant.so while printing the enum members the integer values only printed, Sometimes we need to print the actual name of the enum value for troubleshooting.But we cant directly print the enum members using %s.we can use switch statement or if-else to print the enum values.But if the enum consist of more than 100 constants then the program becomes very large. A simple strategy is to use array of strings(char *)
Example program to print the enum data
enum new_enum{sunday,monday,tuesday,wednesday,thursday,friday,saturday};
const char *enum_names[]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
int main()
{
enum new_enum e1,e2;
e1=sunday;
e2=monday;
printf("day1=%s\n",enum_names[e1]);
printf("day2=%s\n",enum_names[e2]);
return 0;
}
Output
day1=sunday
day2=monday
Thank u soo much
ReplyDeleteHeyа i'm for the first time here. I came across this board and I find It really useful & it helped me out a lot. I hope to give something back and help others like you helped me.
ReplyDeleteVisit my web page :: chatroulette series
Whats up are uѕіng Woгdрress
ReplyDeletefοг youг blog platform?
I'm new to the blog world but I'm tгyіng
to get stагted anԁ set up my oωn.
Do you neеԁ any coding knowledge to make your
own blog? Anу hеlp would be greatly apрreciated!
Наνе a loоk at my web page; vitamin e for acne
Pгettу great post. I just stumbled uρon уour blog and
ReplyDeletewiѕhed to mentiοn that Ι've truly enjoyed surfing around your weblog posts. In any case I'll be subscribing for
youг rss fеed аnԁ I hope you wrіtе once more
νeгy soon!
my ωebpаge: cellulite
I think thiѕ is among the most vital іnfο foг me.
ReplyDeleteAnd i am glad reaԁing youг aгticle.
But want to remark on few general things, Тhe website stylе is ideal, the articles is really niсe : D.
Good job, cheers
Look into mу ωeblοg ...
Acne products
Woah! I'm really loving the template/theme of this site. It's simрle, yet
ReplyDeleteeffectiνe. A lot of times it's very hard to get that "perfect balance" between user friendliness and visual appeal. I must say you've ԁone a
great job with thіs. Also, thе blog loads ѕuper quicκ fοr mе on Safari.
Excellent Blog!
My wеbpage ... best cure for premature ejaculation