C Program to Find Square of a Given Number Using a Preprocessor

Introduction

In this post, I am going to write a c program to find square of a given number using the preprocessor. What is the meaning of a c preprocessor?

The c preprocessor is nothing but a program that processes our source program before it is passed to the compiler. This is a very deep topic in c programming. So I am not discussing here the whole concept. In this post, I am just going to use the concept of macro expansion. We can use this macro for declaring constant.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

Using macro expansion, we can define a constant at the beginning of the program. So, here I am writing two c programs to find the square of a number. One is without using a preprocessor and the other program with the preprocessor.

Here, we can find the square of a number using the standard library function i.e. pow(). for this, we will have to include the header file “math.h”.

Before writing the program, let us see the expected output for this program.

Expected Output

Enter a number
5
Square of 5 is 25

This output will remain the same for all the two programs. Now, let us see our first program.

C Program to Find Square of a Given Number Without Using a Preprocessor

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int num,snum;
    printf("Enter a number\n");
    scanf("%d",&num);
    snum=pow(num,2);
    printf("Square of %d is %d ",num,snum);
    return 0;
}

In the above program, we have used the function pow() to find the square of a number. This function takes two arguments. The first argument is the number that we need to find its square and the second argument is 2. Why? Because in this program, we have to find the square of a number. Suppose you have to find the value of a number raised to the power 7 then the second argument will be 7.

Also Read: C Program to Print Multiples of 5 using do while loop

The same program can be done without using the pow() function. Instead of the statement snum=pow(num,2), we can write snum=num*num. But, I want you to know about the pow() function.

Now, let us move to our next program.

C Program to Find Square of a Given Number Using a Preprocessor

I am just making two changes in the above program. First, I am going to declare the constant using macro expansion i.e. preprocessor. Second change is instead of 2 in the pow() function, I am simply writing the constant name. Just see the following program.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
# define TWO 2    // Macro expansion
int main()
{
    int num,snum;
    printf("Enter a number\n");
    scanf("%d",&num);
    snum=pow(num,TWO);  // TWO instead of 2
    printf("Square of %d is %d ",num,snum);
    return 0;
}

I hope you have understood a c program to find square of a given number using a preprocessor. If you any doubt, then please feel free to contact me.

Thank you.

Some Important C Programs

  1. C Program to Remove Zeros from a number
  2. The while loop in C Programming
  3. Reverse a Number using getchar and putchar function in c
  4. C Program to Print Numbers Except Multiples of n
  5. C Program to Remove White Spaces and Comments from a File
  6. Program in C to Find Longest Line in a File
  7. Palindrome in C using Pointers
  8. Insert and Delete element in Array in C using switch case
  9. C Program to Add Alternate Elements of a 2D Array
  10. Arrays in C for Complete Beginners
  11. C Program to Find Area of a Circle using Preprocessor

1 thought on “C Program to Find Square of a Given Number Using a Preprocessor”

Leave a Comment