enum:
- It stands for enumerated type and is a user-defined data type that represents an integer constant. It is used to assign a name to the integer constant which makes the program more readable and easy to maintain.
- The starting value of names in the enum starts with 0 and plus one for the next and so on.
- Names can also be associated with custom user-defined values.
- This is a run time entity.
Syntax:
enum <enum_name> <list of name>
Example:
#include <stdio.h> enum days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; enum colors { white=8, blue, red }; int main() { printf("\n The value of Sunday is: %d", Sunday); printf("\n The value of color blue is: %d\n", blue); return 0; }
Output:
The value of Sunday is: 0
The value of color blue is: 9
Application:
enum is used for values that are not going to change (e.g., days of the week, colors in a rainbow, number of cards in a deck, etc.).
enum is commonly used in switch-case statements
Macro:
- Macro is a preprocessor that gets expanded during the compilation phase of the program.
- It is a block of code with a specific name and if a name is found in a program, this section of the code is substituted at the place of a name.
- It has a global scope.
- It is a compiled time entity.
Syntax:
#define name_of_macro text_to_be_substituted.
Example:
#include <stdio.h> #define square(x) (x * x) int main() { int x =5; printf("\n The square of number is %d\n",square(x)); return 0; } Output: The square of number is 25
Inline function:
Inline functions are like a normal functionss with the keyword inline and the body of the function is substituted at the place of function rather than calling the function and maintaining the stack that is there is no context switching, its just the expansion of the function each time it is called.
These are generally for short function and its a request to the compiler not a compulsion.
The biggest advantage of inline function is that there is no call to the function and hence no overhead of context switching like saving the current context onto stack and restore once function has completed execution.
But with inline the size of the code increases because the function is expanded at function call
Syntax:
inline data_type fun_name( arguments)
Example:
#include <stdio.h> #define square1(x) (x *x) static inline int square(x) { return x*x; } int main() { int x =5; printf("\n The square is: %d\n", square(2+3)); printf("\n Macro used and value is: %d\n",square1(2+3)); return 0; }
Output:
The square is: 25
Macro used and value is: 11
Difference between macro and inline function:
- Macro is a preprocessor and is expanded at compile time where as inline functions are parsed by the compiler.
- Macro does not enforce type checking whereas inline function does.
- Inline function may or may not be expanded by the compiler, its a kind of request to the compiler whereas macros are always expanded.
Difference between inline and normal function:
One of the key differences is that in case of inline function the body is expanded, there is no call to the function. There is no context switching overhead but in case of normal function there is a call to the function creating a separate activation record and again restoring its previous context.
Categories: C Language
Leave a Reply