"Example: Check If The Given Number Is Prime Or Not"
In this example, we'll use a flag variable to determine whether a given number is prime or not. We'll set the flag to 1 if the number is not prime, otherwise it will remain 0.
#include <stdio.h>
int main() {
int num = 17;
int flag = 0;
if (num <= 1) {
flag = 1;
} else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
}
if (flag == 0) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
17 is a prime number.
In this code:
1. We initialize an integer variable `num` with the number we want to check for primality (in this case, 17).
2. We initialize a flag variable `flag` to 0.
3. We first check if `num` is less than or equal to 1. If so, it's not prime, so we set `flag` to 1.
4. If `num` is greater than 1, we use a `for` loop to iterate from 2 to `num/2`.
5. Inside the loop, we check if `num` is divisible by the current value of `i` using the modulus operator `%`.
6. If `num` is divisible by any number other than 1 and itself, it's not prime, so we set `flag` to 1 and `break` out of the loop.
7. After the loop, we check the value of `flag`.
8. If `flag` is 0, we print that `num` is a prime number.
9. If `flag` is 1, we print that `num` is not a prime number.
This code shows how a flag can be used to keep track of whether a number satisfies the conditions for being prime. The flag is set to 1 if the number is found to be not prime at any point during the checking process.
"Enumerated Flags"
In C, enumerated flags provide a way to define a set of named constants that can be used as flags. Each constant in the enumeration is assigned a power of 2, allowing you to combine multiple flags using bitwise operators.
For example:
#include <stdio.h>
enum FilePermissions {
READ = 1,
WRITE = 2,
EXECUTE = 4
};
int main() {
int userPermissions = READ | WRITE;
printf("User has the following permissions:\n");
if (userPermissions & READ) {
printf("- Read\n");
}
if (userPermissions & WRITE) {
printf("- Write\n");
}
if (userPermissions & EXECUTE) {
printf("- Execute\n");
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this code:
1. We define an enumeration `FilePermissions` with three constants: `READ`, `WRITE`, and `EXECUTE`. Each constant is assigned a value that is a power of 2 (1, 2, and 4 respectively).
2. In the `main()` function, we create a variable `userPermissions` and assign it the bitwise OR of `READ` and `WRITE`. This means that `userPermissions` will have both the `READ` and `WRITE` flags set.
3. We print a message indicating that we will display the user's permissions.
4. We use conditional statements to check if each flag is set in `userPermissions` using the bitwise AND operator `&`. If a flag is set, we print the corresponding permission.
5. If `userPermissions & READ` is non-zero, it means the `READ` flag is set, so we print "- Read".
6. Similarly, we check for the `WRITE` and `EXECUTE` flags and print the corresponding permissions if they are set.
In this example, the output will be:
User has the following permissions:
Read
Write
Frequently Asked Questions
What is the purpose of using flags in C programs?
Flags in C are used to track the status or state of certain conditions, allowing you to control the flow of the program based on those conditions.
How do you declare a flag variable in C?
You can declare a flag variable as a regular integer variable, typically initialized to 0 or 1 depending on the desired initial state.
What are enumerated flags and how are they useful?
Enumerated flags are a way to define a set of named constants that can be combined using bitwise operators. They provide a readable and efficient way to work with multiple flags.
Conclusion
In this article, we learned about flags in C and their usefulness in tracking program state and controlling flow. We discussed different examples of using flags to check for even numbers in an array and determine if a number is prime. Moreover, we discussed enumerated flags, which allow you to define and combine multiple flags using bitwise operations. Flags are a powerful tool in C that can help you write more expressive and efficient code.
You can also check out our other blogs on Code360.