Children who experience pain or discomfort when pooping quickly learn to withhold their poop because they are afraid that the poop will hurt. They learn that the pain or discomfort can be avoided by simply contracting the muscle (sphincter) around their anus whenever they feel the need to poop. Withholding begins as a voluntary response (a conscious decision), but if the painful or uncomfortable bowel movements continue, withholding can become involuntary. This means that the anal muscle "closes" automatically whenever the rectum contracts which is what causes the feeling of urgency, the need to poop. Withholding is no longer a conscious decision. It has become a habit which leads to functional constipation--also called encopresis.
Exactly how long it takes for withholding to become a habit varies with age and temperament. Some infants and children begin to withhold involuntarily after just one painful or uncomfortable bowel movement whereas others are able to tolerate a number of painful bowel movements before becoming habitual withholders. The difference between the two groups is most likely related to the degree of discomfort or pain they experience. The more intense the discomfort the more quickly withholding becomes involuntary.