Table of contents
1.
Introduction
2.
"Example: Check If An Array Has Any Even Number"
3.
"Example: Check If The Given Number Is Prime Or Not"
4.
"Enumerated Flags"
5.
Frequently Asked Questions
5.1.
What is the purpose of using flags in C programs?
5.2.
How do you declare a flag variable in C?
5.3.
What are enumerated flags and how are they useful?
6.
Conclusion
Last Updated: Dec 10, 2024
Easy

What is Flag in C?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Flags are simple indicators used in programming to signal various states or conditions within a program’s execution. They act as signals that can be set, checked, and changed to control the program's flow. They are used to make decisions, control loops, and perform different actions based on certain conditions. Flags can be implemented using basic data types, specifically integers or boolean values, to represent different states. 

What is Flag in C?

In this article, we'll learn what flags are, how they work, and their examples.

"Example: Check If An Array Has Any Even Number"

In this example, we'll use a flag variable to check if an array contains any even number. We'll set the flag to 1 if an even number is found, otherwise it will remain 0.

#include <stdio.h>

int main() {
    int arr[] = {1, 3, 5, 7, 9, 2};
    int size = sizeof(arr) / sizeof(arr[0]);
    int flag = 0;


    for (int i = 0; i < size; i++) {
        if (arr[i] % 2 == 0) {
            flag = 1;
            break;
        }
    }
    if (flag == 1) {
        printf("The array contains an even number.\n");
    } else {
        printf("The array does not contain any even number.\n");
    }


    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

The array contains an even number.


In this code:

1. We initialize an integer array `arr` with some values.
 

2. We calculate the size of the array using `sizeof()`.
 

3. We initialize a flag variable `flag` to 0.
 

4. We use a `for` loop to iterate through each element of the array.
 

5. Inside the loop, we check if the current element is even using the modulus operator `%`.
 

6. If an even number is found, we set `flag` to 1 & `break` out of the loop.
 

7. After the loop, we check the value of `flag`.
 

8. If `flag` is 1, we print "The array contains an even number."
 

9. If `flag` is 0, we print "The array does not contain any even number."

"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.

Live masterclass