How to test for the number of players in a gamemode in minecraft? [duplicate]

If a selector such as @a[m=0] selects Alice, Bob, Carol, Dave, then @a[m=0,c=2] will select Alice, Bob. c=2 does not mean "only select anyone if there are exactly 2 players", but rather "limit the players selected to at most 2". Even if there are more than 2 players, the command will still select 2 players and succeed.

When a /testfor command is run, a command block's SuccessCount tag will be set equal to the number of entities that the selector found. You can then use a /testforblock command to check whether the first command block's SuccessCount is a certain number.

For example:

/testfor @a[m=0]
/testforblock X Y Z command_block -1 {SuccessCount:2}

Change X Y Z in the second command block to the coordinates of the first command block. You may also need to change command_block to chain_command_block or repeating_command_block.

The second command will only succeed if the first command has a SuccessCount of 2 (it found exactly 2 players). You can then run a conditional chain block off of that second command to activate whatever you want to happen when there are exactly 2 players:

Setup