-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconst_#define_typedef_enum
63 lines (44 loc) · 2.3 KB
/
const_#define_typedef_enum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
---------------------------------------------------------------------------------------------------
/*** CONSTANT KEYWORD ***/
---------------------------------------------------------------------------------------------------
* It tells the compiler that this value cannot be changed.
* const int i =5;
i++; // Error : compilation
*Type checking is performed in constant whereas not performed in #define[PRE_PROCESSING].
ex : #define MYVALUE '5.0'
int i = MYVALUE;
while preprocessing it will substitute the value without type-checking.
* const variables are separetly stored in "READ_ONLY SECTION".
---------------------------------------------------------------------------------------------------
/*** TYPEDEF KEYWORD ***/
---------------------------------------------------------------------------------------------------
* It is used to create new data types, or assign a new name to exiting data types.
* syntax: typedef <data_type> <new data_type>
* example: typedef unsigned char BYTE;
* int main()
{
BYTE ch1, ch2;
ch1 = 10, ch2 = 20;
printf("ch1 is %d/t ch2 is %d\n",ch1,ch2);
return 0;
}
---------------------------------------------------------------------------------------------------
/*** TYPEDEF VS #DEFINE ***/
---------------------------------------------------------------------------------------------------
-compilation stage -preprocessing stage
-new name/another name to data_type -Data types,Variables
-typedef unsigned char *PTR; -#define unsigned char* PTR
PTR a,b,c; PTR a, b, c;
=> unsigned char *a, b, c; => unsigned char* a, b, c;
---------------------------------------------------------------------------------------------------
/*** ENUMS ***/
---------------------------------------------------------------------------------------------------
*enum is used to create new data type.
*enum is used only for integer constants.
*enum is used to create user-defined data types.
*sizeof(enum) == sizeof(integer)
*Addition, Substraction on enum values can be possible.
*You can assign integer value to it.
*scope rule in enum not in #define
*You can assign same values to members in enum.
---------------------------------------------------------------------------------------------------