So I picked up one book and is going thru it, since I have already some work experience behind me, I will rather focus more on points which i have came across till now in my work with C.
In this part we will focus on
1) Enum
2) Struct
3) Union
4) Anonymous Union and Enums
5) Void pointers
6) Scope resolution Operator
7) Macro (#define)
8) Typedef
If we avoid all the tricky questions which can be asked, The basic difference between
TYPEDEF , ENUM , MACRO ( #define) is that
typedef has to be used only when you are trying to have a new DATA TYPE.
ENUM has to be used, when you need to define long list of const , which are in real life.
Ex : Weekdays , Months , list of student name in class....So even though we can use #define or const int , it will be difficult , firstly due to
1) Readability purpose
2) If we need to insert some new variable in list, we have to manual then change the value of each #define
MACRO (#define) 1) Sud not be used as such, since they actually don't anything , but just replace text. So if you can avoid them you should do that. But they can be used when want to better readability or you have to replace a lengthy text with small code
Ex : Suppose if you use lot of for loops in your code . You can then simply use
#define FOR(start,end) for(int i=start;i>=0;i--)
In my embedded code , we generally use this with enum to assign function pointers , since the function called will change depending on input.
Also from MEMORY point of view , MACRO don't allocate any space , enum allocate space for just one variable .
Ex :
enum {SUNDAY=0, MONDAY=1) // total size will be 4 bytes ; for a machine assigned 4 bytes for int
const int SUNDAY =0;
const int MONDAY =1;
total size will be 8 bytes
#define SUNDAY 0
#define MONDAY 1
no space allocated.
So as can be seen enum is much better than const int , since the space used will be less. I have hardly seen many const in my code , May be const use will be more in OO languages rather than C
Also #define should not be used to assign integer or some other values to some variables, since code can get complex.
TYPEDEF is pretty straightforward, it should be used when you want to give easy name to some new data type which you have created from existing data types.
It also brings us to check the difference between typedef and #define
typedef int* INT
INT a,b ;
a=&c;
b=&d;
#define INT int*
INT a,b ;
a=&c;
b=&d;
The second one throws error since, #define will simply replace text of INT, and only a will be treated as int* and not b.
In below code
int* a,b; or int *a,b , only A is pointer and not b.
Same effect happens with #define use.
Anonymous Enum is simply when you just want to use a given variables as CONST, you don't want to assign any value in it.
So don't have to give any type name to enum in that case.
Scope Resolution Operator is :: and
1) It is available only in C++ and not in C.
2) It is used when their is clash between local and global variable.
Ex :
int a=20;
int main()
{
int a=30;
{
int a=40;
printf("%d",::a)
}
}
here ::a will print 20 and not 30 ...it will always take GLOBAL VARIABLE value.
Struct and unions are pretty much self explanatory, if someone has done some C/C++ coding.
So there is no need to go much deep in it. the below program will clear some more doubt.
http://ideone.com/qzt9wU
Void pointers we will cover later once going thru function pointers.
No comments:
Post a Comment