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.
what is the meaning of flag in C?
Started by payam.a, Jul 24 2008 04:43 AM
2 replies to this topic
#1
Posted 24 July 2008 - 04:43 AM
|
|
|
#2
Posted 24 July 2008 - 07:54 AM
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:
in this case, isprime is a flag
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
#3
Posted 24 July 2008 - 08:03 AM
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.
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.


Sign In
Create Account


Back to top









