What is Flag in C
Introduction
The control or indication of a transition state or the result of specific activities is frequently done using flags.
The term "flag" refers to any variable or constant that stores data. The place where you store things may be compared to a flagpole. The variable's value might be the flag because it contains a particular significance.
What is Flag in C?
In C programming, a flag variable is used as a signal to let the programme know whenever a criteria has been met. It serves as a boolean variable almost all the time, indicating if a condition is true or false. The flag is a variable that is supposed to signalize.
A flag variable is employed in the C program to denote a particular circumstance or state. It acts as a variable that may be configured to be either true or false and is employed in loops and conditional statements to regulate the program's flow. For instance, a flag variable might be employed to show if a particular criterion was successfully met and then used to control the flow of the program depending on that requirement. It can also monitor a program's status or indicate when an operation is finished.
Typically, the two values 0 and 1 are assigned to a flag variable. As an answer, it can be used as a Boolean variable where the outcome can be either 0 (False) or 1 (True), depending on how the programmer uses it. Many people choose flag=1 and alter it in the program to flag=0 to take a specific activity.
The most typical flag application in C programming is to initialize the value at the start of the code and continue verifying it as needed.
A classic example of flag usage in a C program is seen in the code below. It shows the exact location of the number in the array if it is existent.
Code
#include<stdio.h>
int main()
{
int array[5], flag=0, pos, ind, i, temp;
printf("Enter 5 elements of the array: ");
for(i=0;i<5;i++)
{
scanf("%d", &array[i]);
}
printf("Enter number to search: ");
scanf("%d", &temp);
for(i=0;i<5;i++)
{
if(temp == array[i])
{
flag = 1;
ind = i;
pos=i+1;
break;
}
}
if(flag == 0)
printf("%d Not found in the given array", temp);
else
printf("%d found at index %d and position %d in the given array", temp, and, pos);
return 0;
}
Output
![What is Flag in C](https://static.tutorialandexample.com/c/what-is-flag-in-c1.png)
Let us consider another example code in C to check if a given number is even or odd using a flag
Code
#include<stdio.h>
int main()
{
int flag=0; //flag is set to zero initially
int n=8; //a general variable
if(n%2==0)
{
flag=1; //if the number is even, then the flag is set to 1 (True)
}
if(flag) //it can also be written as if(flag==1)
{
printf("%d is Even Number" ,n);
}
else
{
printf("%d is Odd Number" ,n);
}
return 0;
}
Output
![What is Flag in C](https://static.tutorialandexample.com/c/what-is-flag-in-c2.png)
To demonstrate how the flag function in the C programming language, a simple example of an even or odd number was used.
Two Flags with the same meaning
To allow us to choose whether or not we wish to execute the looping operation once more, we occasionally employ an iteration control structure of do while. The user may be asked to enter a reaction, such as "y" for yes or "n" for no, into a variable called "loop_response." The sample phrase to direct the loop could look as follows after the reply has been taken from the keyboard and saved in our flag variable "loop_response":
Simple flag comparison
loop_response equals 'n'
This is okay, but what if the user unintentionally has caps lock on? If 'Y' was the reply, the mechanism for controlling couldn't repeat, and the procedure could be performed once more. The answer can be found by taking two looks at the flag.
Complex flag comparison
loop_response equals 'y' or loop_response equals 'Y'
Utilizing a highly complicated Boolean expression involving relational and logical operators, we check whether the flag is either a lowercase Y or an uppercase Y.
Multiple Flags in One Byte
In assembly language programming and most technical programmes which regulate specialised parts, a single byte is typically used to denote a collection of flags. The 8 bits that make up a byte are used for representing flags, which achieves the desired result. Each bit has a value of either 1 or 0, along with the ability to express true and false, on or off, yes or no, etc.
Conclusion
We conclude that in C programming, a flag variable is used as a signal to inform the code that a given condition has been satisfied. It normally performs the same job as a boolean variable, indicating whether a condition is true or false.