Why #define is used in C?

#define is preprocessor command used to define constants and aliases. Look at this example:

#include<stdio.h>  
#define PI 3.14


#define ERROR_1 "File not found."

#define QUOTE "Hello World!"
main()


{

printf("Area of circle = %f * diameter", PI );

printf("\nError : %s",ERROR_1);

printf("\nQuote : %s\n",QUOTE);

system("pause");

}

Output results:

Area of circle = 3.140000 * diameter

Error : File not found.

Quote : Hello World!

Preprocessor step is performed before compilation and it will change the actual source code to below code. Compiler will see the program as below one:


#include<stdio.h>
main()
{
printf("Area of circle = %f * diameter", 3.14 );
printf("\error : %s","File not found.");
printf("\nQuote : %s","Hello World!\n");
system("pause");
}


Final Word

In brief #define allows us to define symbolic constants. We usually use uppercase names for #define variables. Pay attention that we do not use ';' after preprocessor statements.
Know more about C
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...
 
Posts RSSComments RSSBack to top
© 2013 Updated Tech News Results and Reviews