facebook like button

25 March, 2011

program 9: Area of circle and use of #define

The Need:
     To calculate the area and circumference of a circle with given radius. Get to know the '#define' directive.

The code:
--------------------------------------------
#include<stdio.h>
#define PI 3.14159       //defining PI value
main()
{
    float R,C,A;
    printf("Give the radius of circle \nR= ");
      scanf("%f",&R);    //scanning radius
    C=2*PI*R;       //calculating circumference
    A=PI*R*R;       //calculating area
    printf("Circumference=%f    Area=%f\n",C,A);
}
---------------------------------------------

     First of all compile and run this program and see it works. Now notice that in the very second line of code I wrote a statement "#define PI 3.14159" and further in the formulas for calculating area and the circumference in stead of writing value of pi(that is approx. 3.14159) I have used PI word. What are you thinking......? Yeah you are right its just like when you use the word LOL while chatting with your friends and ask them to interpret it as 'Laughing out loud'...". After that your friends will understand what you mean whenever you say 'LOL' in chat.
    As you will be using #define more in your programs, you will come to know that 
1. you cannot change the previously defined value.
2. Its a 'preprocessor directive'. Don't worry about this new term. You will come to know about it in detail later.

1 comment:

  1. Macros and #define directives are the same?

    ReplyDelete

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply