Modulo operator in C
This article will demonstrate various ways of using the modulo operator in C.
%
Using the modulo operator
in C to calculate the remainder in division
The modulo operator %
is one of the binary arithmetic operators in C language. It produces the remainder after dividing two given numbers. The modulo operator cannot be applied to floating point numbers like float
or double
. In the following sample code, we show %
the simplest example of using the operator, printing the result of taking the given int
array modulo 9.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int arr[8] = {10, 24, 17, 35, 65, 89, 55, 77};
for (int i = 0; i < 8; ++i) {
printf("%d/%d yields the remainder of - %d\n", arr[i], 9, arr[i] % 9);
}
exit(EXIT_SUCCESS);
}
Output:
10/9 yields the remainder of - 1
24/9 yields the remainder of - 6
17/9 yields the remainder of - 8
35/9 yields the remainder of - 8
65/9 yields the remainder of - 2
89/9 yields the remainder of - 8
55/9 yields the remainder of - 1
77/9 yields the remainder of - 5
Using %
the modulo operator to implement leap year check in C
In addition, we can also use %
operators to implement more complex functions. The next example code demonstrates isLeapYear
the Boolean function, which checks whether a given year is a leap year. It is important to note that if a year value is divisible by 4 but not by 100, then the year is considered a leap year. In addition, if the year value is divisible by 400, it is a leap year.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
else
return false;
}
int main(void) {
uint year = 2021;
isLeapYear(year) ?
printf("%d is leap\n", year) :
printf("%d is not leap\n", year);
exit(EXIT_SUCCESS);
}
Output:
2021 is not leap
Use the modulo operator in C %
to generate random numbers within a given integer range
Another useful function of the modulus operator is to place an upper limit on the number that can be generated during random number generation. That is, suppose we have a function that generates a random integer. In this case, we can take the remainder of the divisor between the returned number and the value we want as the maximum (defined as MAX
the macro in the example below). It should be noted that using the srand
and rand
functions to generate random numbers is not a robust approach, and applications that require high-quality random numbers should use other tools.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define COUNT 10
#define MAX 100
int main(void) {
srand(time(NULL));
for (int i = 0; i < COUNT; i++) {
printf("%d, ", rand() % MAX);
}
printf("\b\b \n");
exit(EXIT_SUCCESS);
}
Output:
3, 76, 74, 93, 51, 65, 76, 31, 61, 97
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Flushing stdout output stream in C
Publish Date:2025/04/17 Views:200 Category:C语言
-
stdout This article will demonstrate various methods on how to flush an output stream in C language . Use function in C language fflush to refresh stdout output stream I/O The C standard library provides a stdio buffered version of the I/O
Using the feof function in C language
Publish Date:2025/04/17 Views:87 Category:C语言
-
feof This article will introduce several ways to use functions in C language . Use feof the function to check the end-of-file indicator on a file stream in C language feof The function is part of the C standard input/output library and is d
Use the C language setenv function to access environment variables
Publish Date:2025/04/17 Views:122 Category:C语言
-
setenv This article will introduce several methods of using functions to export environment variables in C language . setenv Use function to export environment variables in C language Every program running on a Unix-base system has an envir
How to get the size of an array in C
Publish Date:2025/04/17 Views:62 Category:C语言
-
This tutorial explains how to determine the length of an array in C. sizeof() The operator is used to get the size/length of an array. sizeof() Operator determines the size of an array in C sizeof() The operator is a compile time unary oper
Initialize array to 0 in C
Publish Date:2025/04/17 Views:133 Category:C语言
-
This article explains how to initialize an array to 0 in C language. The declaration of an array in C language is as follows. char ZEROARRAY[ 1024 ]; It becomes all zeros at runtime in the global scope. If it is a local array, there is a si
Structure arrays in C
Publish Date:2025/04/17 Views:188 Category:C语言
-
This tutorial explains how to create a structure array in C language, which is a collection of multiple structure variables, each of which contains information about a different entity. Structure arrays in C An array is a sequential collect
Printing Character Array in C
Publish Date:2025/04/17 Views:106 Category:C语言
-
This article will introduce various methods on how to print character array in C language. for How to use loop to print character array in C language If we want to print array elements individually and format the output with more details, f
Clearing a character array in C
Publish Date:2025/04/17 Views:119 Category:C语言
-
This article will introduce several methods to clear character arrays in C language. memset Use function to clear char array in C language memset The _set_storage_region function is commonly used to set a storage area to a constant value. T
Initializing character array in C
Publish Date:2025/04/17 Views:92 Category:C语言
-
This article will demonstrate various ways of initializing a character array in C language. {} Initializing a character array in C using curly brace list notation Character arrays are mostly declared as a fixed-size structure and are often