C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++

Pi() Function in C

Pi:

Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and math.h libraries.

To use the pi function in c:

One of the following methods is used for using the preprocessor directives as “#define” to make the “PI” value is equals to the 3.142857.

Let’s see #define PI as 3.14 in c:

Assuming that you express "#define PI 3.14", all examples of PI in the program will be supported with 3.14 by the preprocessor before aggregate time.

Assuming that you express "const float PI=3.14", 3.14 will be put away in memory held for the consistent PI and utilized at run time.

M_PI in c:

Pi, the ratio of circle's circumference to its diameter. Pi is divided by two.

Press ctrl + shift + u then type zero-three-C-zero,

then, at that point, press enter and you get the pi image. How would I type the Pi image on Windows 10 in the event that I don't have NUM lock?

You needn't bother with a NUM lock; all things considered, simply hold down the alt key and type 227. The 227 must be from the console on the right half of your console.

The PI constant is there in the cmath header file.

The name of the consistent is M_PI.

We can essentially incorporate that header document, and utilize the steady to perform activity.

Syntax for pi:

#define PI (3.141592653589793)

Let’s see an example program for pi using c:

// to find the radius of circle

#include <stdio.h>


void test()
{
   float pi = 22.0f/7.0;
   printf("float \n");
   printf("%.25lf\n", pi);
   printf("%lf\n", pi);
}


void test1()
{
   double pi = 22.0/7.0;
   printf("doublen");
   printf("%.25lf\n", pi);
   printf("%lf\n", pi);
}


void test2()
{
   long double pi = 22.0L/7.0;
   printf("long double \n");
   printf("%.25Lf\n", pi);
   printf("%Lf\n", pi);
}


int main(void)
{
   test();
   test1();
   test2();
   return 0;
}

Output:

float 
3.142857074737
3.142857
double
3.14285714285714
3.142857
long double 
3.142857142857
3.142857

Let’s see another example on pi function in c:

#include <stdio.h>
#define PI 3.1
int main()
{
  int radius;
  float area;
  printf("Enter the radius of the circle\n");
  scanf("%d", &radius);
  area = PI*radius*radius;
  printf("Area of the circle is %.2f\n", area);
  return 0;

Output:

Enter radius of the circle: 2
Area of circle: 18.1565


Just thing that you have to observe that in our program pi is supported by the 3.14 at whatever point it came in the program even prior to ordering. In this way, PI*radius*radius will be

3.14*radius*radius.

let’s see another to print area of circle using marcos:

#include <stdio.h>  
#define area(r) (3.14*r*r) 
int main()  
{ 
  int radius; 
  float area; 
  printf("Enter the radius of the circle\n"); 
  scanf("%d", &radius); 
  area = area(radius); 
  printf("Area of the circle is %.2f\n", area); 
  return 0; 
}

Output:

Enter radius of the circle 
3 
Area of circle is 27.54

We have characterized a large scale 'region' which takes one contention that is 'r'. In this way, when we called area(radius), it got supplanted by 3.14*radius*radius (as area(r) is 3.14*r*r).

Not at all like functions which are called during compilation, any place there is area(x) in our code it will be straight forwardly supported by 3.14*x*x before compilation.

Let's Compare Macro with Function

We have recently seen a guide to print the region of a circle utilizing macros. Presently we should see a similar model utilizing function.

#include <stdio.h> 
 float CircleArea(int r) 
 { 
   float a; 
   a = 3.14*r*r;  
  int radius; 
  return a; 
}  
int main ()  
{ 
  int radius; 
  float area;  
  printf("Enter radius of the circle\n"); 
  scanf("%d", &radius); 
  area = CircleArea(radius); 
  printf("Area of circle is %.2f\n", area); 
  return 0; 
 } 

 Output:

Enter radius of the circle 
2 
Area of circle is 18.24

The primary contrast is that macros support the codes by their access (as it area(r) with (3.14*r*r)).

Along these lines, each time code will be distributed some space. This implies that each time area(r) will show up, it will be space distributed in the memory. Yet, this isn't true with function.

By this way we use the pi function in the programming languages. Hence the pi function using macros also be useful.



ADVERTISEMENT
ADVERTISEMENT