What does int flag = 1 or flag = 0 means in C++ language? I have seen it in many programs but I don't know its use.

It just means you are declaring a variable called flag and giving it a value of 1 or 0. This will presumably be used elsewhere in the code to keep a track of the state of something. It is also probably boolean in nature.

The elaborate on DaveAmour's answer, you should know that C (the predecessor of C++) does not have a built-in boolean datatype, and until the C99 standard didn't have a standard defined one either. Thus, it has long been the convention in C to use int variables as booleans, with 0 for false and any other value for true.

While C++ has always had a bool type, a lot of C++ programmers who had started out in C never got into the habit of using it, and a lot of C programs which were converted to C++ were never changed to use it, either.

The variable name flag (or some variation thereof) generally indicates that the variable is being used as a boolean value, and more specifically, a flag value, that is, one used to indicate a particular state or state change in the program. In file formats, an int value may actually hold several packed flag fields, with the individual bits being treated as separate boolean values. However, in the context you are describing, it is more typical for it to hold just a single boolean value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.