Jump to content

what is the meaning of flag in C?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
payam.a

payam.a

    Newbie

  • Members
  • PipPip
  • 12 posts
can you guide to know what is the meaning of flag in programming c. for example what is the meaning of this paragraph?


Second, how do you know if a number is prime? If num is prime, program flow never gets inside the if statement. To solve this problem, you can set a variable to some value, say 1, outside the loop and reset the variable to 0 inside the if statement. Then, after the loop is completed, you can check to see whether the variable is still 1. If it is, the if statement was never entered, and the number is prime. Such a variable is often called a flag.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
A flag, or sentinel, is any variable that's sole purpose is to indicate when a key point in the processing has been reached. This include things like breaking out of a loop, being able to access a resource shared between threads, etc.

the code would resemble this:
bool isprime=true;
for (int i=2;i<n;i++){
  if n%i == 0 then isprime=false;
}
if isprime cout<<n<<" is prime"
else cout<<n<<" is not prime";

in this case, isprime is a flag

Edited by WingedPanther, 24 July 2008 - 07:54 AM.
add explanation

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Flags can also be fixed values for indicating state, mode, behavior, or similar. A fine example is the modes given to fstream:: open in C++. One can pass either ios_base::app, ios_base::ate, ios_base::binary, ios_base::in, ios_base:: out, ios_base::trunc or even several of them together. Each of them will change how the file is being opened.

Edit:
I realized that the OP wasn't exclusively asking about flags. I will however leave my explanation as it is, if somebody else may need it. Also I fixed some formatting, as CodeCall did put some smilies all over the place.

Edited by v0id, 24 July 2008 - 08:06 AM.