The value of 4.0 kcal per gram of dietary carbohydrates is simply a convention used by the Atwater System, which took a single averaged value for each of the available macronutrients found in food. I believe it was meant to represent an average composition of dietary carbohydrates at the time.
However, not all carbohydrates have the exact same heat of combustion. In other words, the amount (weight) of carbohydrate needed to yield a specific amount of energy differs depending on the molecular form of the carbohydrate (1). More specifically, monosaccharides (glucose and fructose) have ~3.75 kcal per gram, disaccharides (sucrose and maltose) ~3.95 kcal per gram, and longer polysaccharides (starches) have 4.15 to 4.20 kcal per gram (2).
The reason for this is again caused by water, specifically the dehydration (loss of water) that occurs as a result of the condensation reaction which forms the glyosidic linkage between monosaccharide monomers (1). For each additional monosaccharide subunit added to a carbohydrate chain, a water molecule is lost (see the example below).
Image credit: https://ib.bioninja.com.au/
So...going back to the original example. The heat of combustion of anhydrous glucose is 3.75 kcal per gram. Glucose (180 g/mol) makes up ~ 90.9% of the weight of dextrose monohydrate (198 g/mol). So, take the heat of combustion of glucose (3.75 kcal/g) multiplied by it's relative proportion in composition of dextrose monohydrate (0.909) and you get 3.75 * 0.909 = 3.4 kcal per gram. The 4.0 kcal per grams of dietary carbohydrate is simply a convention derived from an average value based on work that's over a century old.
1: FAO FOOD AND NUTRITION PAPER 77. Food energy - methods of analysis and conversion factors; Report of a Technical Workshop, Rome, 3-6 December 2002. FOOD AND AGRICULTURE ORGANIZATION OF THE UNITED NATIONS.
2: Agricultural Handbook No. 74. Energy value of food; basis and derivation. Merrill, A., Watt, B. USDA Agricultural Research Branch.