Use of flag in c?

I am the beginners of programming so,i don't know the use flag in c.since ,i have searched many question about flag in c but i don't get it.i request you to answer my question to you developers thank you.

1

2 Answers

A "flag" variable is simply a boolean variable whose contents is "true" or "false".

You can use either the bool type with true or false, or an integer variable with zero for "false" and non-zero for "true".

So why would you use a flag?

Suppose you are outputting a byte value in binary, one bit at a time, say decimal 42 which is 00101010.

But, you don't want any leading zeros, and the output should be 1010101.

You can use a flag to tell you whether to suppress the 0 bits. Start with the flag as true, and whenever you output a 1 bit you set the flag to false, telling you not to suppress 0 bits any more.

Of course, the final bit needs to be output even if all bits are 0, but that's another matter.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like