© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

4. What is the need of the macro?

Ans:

Need of Macro:

Some times there is a need to call the set  of codedata again and agian in the program. In that particular situation we can use macros. Use of Macro can increase the efficiency of the program and also decrease the length of the code. Macro can be defined by using Define statement

Ex - 1:  What is the output?
#include
#define a 10
main()
{
#define a 50
printf("%d",a);
}

Ans:
50

Explanation:
The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.

Ex - 2:  What is the output?
#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
            }

Ans:
100

Ex - 2:  What is the output?
#define FALSE -1
            #define TRUE   1
            #define NULL   0
            main() {
               if(NULL)
                        puts("NULL");
               else if(FALSE)
                        puts("TRUE");
               else
                        puts("FALSE");
               }

Ans:
TRUE

Explanation:
The input program to the compiler after processing by the preprocessor is,
            main(){
                        if(0)
                                    puts("NULL");
            else if(-1)
                                    puts("TRUE");
            else
                                    puts("FALSE");
                        }

Preprocessor doesn't replace the values given inside the double quotes. The check by if condition is boolean value false so it goes to else. In second if -1 is boolean value true hence "TRUE" is printed.


SLogix Student Projects
bottom