C

What Does % Mean In C

To perform different calculations and logical tasks we used the arithmetic operators in the c language, these operators help the compiler to understand what task should be performed or which and how the calculation should be done first. Different operators perform different calculations, in arithmetic operators we have the add operator “+”, subtract operator “-“, multiply operator “*”, divide operator “/”, and modulo operator “%”.% is also an arithmetic operator in the C programming language, it divides 2 operands or values and returns a reminder as a result now let see what does % mean in c.

The % symbol is called a Module operator in the C language and it is used to calculate the reminder by dividing the left-hand side value by the right-hand side value. The % module operator is only used with integers, for better understanding check out the examples below.

Example 1

// code of execution of the modulo operator

#include <stdio.h>

int main(void)
{

               // for storing the integer values
               int a, b;

               // for storing the output of the modulo expression
               int output;

               a = 15;
               b = 4;
               output = a % b;
               printf("%d", output);

               a = 6;
               b = 3;
               output = a % b;
               printf("\n%d", output);

               return 0;
}

Code Explanation

In the above C code, we declared two variables or integers a and b for storing the values which will be used in the modulo expression, then we declared another variable for storing the output of the modulo expression, After that we used the modulo expression using values of a and b, then we just printed the output.

We again declared integers a and b and assign values to them then we used them for another modulo calculation and in the end, we just printed out the output, lastly we returned 0 if the reminder did not obtain. Check out the results below.

Output

3
0

As you can see from the output above, 3 is the reminder of the first module expression, which is obtained by dividing the values of a and b means by diving 15 by 4 we got 3 as a reminder.

0 is returned because we did not get the reminder of 6/3.

Let’s see what happens when we use the float number or double numbers with the modulo operator See the next example below.

Example 2

// code of execution of the modulo operator with float numbers
#include <stdio.h>

int main(void)
{

              // for storing the integer values

               float a, b;

              // for storing the output of the modulo expression
               float output;

               a = 3.4;
               b = 5.9;
               output = a % b;
               printf("%f", output);

               return 0;
}

Code Explanation

In the above C code, we stored the float numbers in a and b integers then we declared another variable for storing the result of the modulo expression in the end we just printed out the output and returned 0 if the reminder didn’t obtain. Check out the results below.

Output

gcc /tmp/wBJdTrVTWn.c -lm
/tmp/wBJdTrVTWn.c: In function 'main':
/tmp/wBJdTrVTWn.c:18:13: error: invalid operands to binary % (have 'float' and 'float')
   18 |  output = a % b;
      |             ^

You can see the error in the above output it is showing the error because the modulo operator does not work with the float numbers, the modulo operator has some restrictions we can’t calculate the reminder of a few sorts of values and float is one of them.

Now let’s see how to use the modulo operator for the calculation of array values check out the example below.

Example 3

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int arr[6] = {20, 32, 61, 29, 95, 87};

    for (int i = 0; i < 6; ++i) {
       
printf("the remainder of %d/%d is 
%d\n", arr[i], 7, arr[i] % 7);
    }

   
exit(EXIT_SUCCESS);
}

Code Explanation

In the above C code, we used for loop to calculate the modulo of array values one by one. First of all, we created an array and then we iterated the for loop after that we divided the array values by 7 and printed out the reminders. See the results below.

Output

the remainder of 20/7 is 
6
the remainder of 32/7 is 
4
the remainder of 61/7 is 
5
the remainder of 29/7 is 
1
the remainder of 95/7 is 
4
the remainder of 87/7 is 
3

Conclusion

In this tutorial we talked about operators in the C programming language, we discussed what % means in C, what it calls, and how it works, we saw some examples as well in different scenarios with code explanations.

In the first example we saw how to calculate the modulo of two integers then in the second example we used float numbers for finding out the reminder and then in the last example we used the loop for calculating the modulo of arrays values.

That’s all for today hope you like the tutorial if you have any queries regarding this article so feel free to contact us below. Thanks for reading.

 

Leave a Comment