When a programmer-defined its own data type for any specific purpose then it is known as a user-defined-datatypes.Since these self-defined data types are not standard data types so compiler needs to know about these User-defined data types at the time of compilation.
There are two keywords defined in the standard library through which programmer must aware to the compiler about the self-constructed data type in his programme.These two keywords are-
— enum
— typedef
enum is basically used for the integers to give a name which increases the readability of the program.
1 2 3 4 5 6 |
enum enum_defined_dataType { enum_Const1, enum_Const1=value, enum_Const1,..... }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> enum status { FAIL, OK }; int main() { enum status st=FAIL; /*st=0 because FAIL=0*/ return st; /*equivalent to return 0*/ } |
you can see in above code
status = is a data type to store constants FAIL and OK
FAIL ,OK = Constants and values are their indexes(0 and 1).
By default, enum constants take their index value but we can change their indexes by assigning them different value.
See in below code enum_Const4 has index 41 because of previous index change to 40.
1 2 3 4 5 6 7 |
enum test { enum_Const1, /*default value =0*/ enum_Const2=20, enum_Const3=40, enum_Const4 /*default value =41 because previous index change to 40*/ }; |
— enum constants must have values in defined range for integer.
— Multiple enum constants can have same values
1 2 3 4 5 6 7 |
enum test { enum_Const1, /*default value =0*/ enum_Const2=20, enum_Const3=20, enum_Const4=10 }; |
— Since enum represent constants so the size of enum will be the size of an int.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> enum status { FAIL, OK }; int main() { enum status st=FAIL; printf("size of enum st is=%lu",sizeof(st)); return st; } |
1 |
Output: size of enum st is=4 |
— enum constants may have alphabets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> enum test { enum_Const1, enum_Const2=20, enum_Const3='A', enum_Const4=10 }; int main() { enum test st=enum_Const3; printf("value at enum_Const3 is=%c",st); return st; } |
1 |
Output: value at enum_Const3 is=A |
‘typedef’ is used to replace standard data type name with some alias name.
1 |
typedef real_data_type alias_name; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
1. /*you can use int8_t instead of char in this program*/ typedef char int8_t; 2. /*you can use uint32_t instead of unsigned int in this program*/ typedef unsigned int uint32_t; 3. /*now you can use int16_t instead of short int in this program*/ typedef short int int16_t; 4. /*empdetail is the alias name for struct employee*/ typedef struct employee { name; empId; salary; }empdetail; |
so in above examples int8_t,uint32_t,int16_t and empdetail are the user-defined-datatypes.
Let us see a program to be more clear –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> typedef char int8_t; typedef unsigned int uint32_t; typedef short int int16_t; int main() { int8_t i8_var=50; uint32_t ui32_var=50000; int16_t i16_svar=-3567; printf("i8_var=%d\n",i8_var); printf("ui32_var=%u\n",ui32_var); printf("i16_var=%d\n",i16_svar); return 0; } |
1 2 3 4 5 |
output: i8_var=50 ui32_var=50000 i16_var=-3567 |
See more about the typedef in C.