Both C and G are "softened" when they are followed by I or E, and to "harden" them back sometimes a silent H is added: "legge" (soft G) versus "leghe" (hard G). Whether what is preserved is the sound (e.g. pag-o -> pagh-i, both hard Gs) or the root (legg-o, legg-i) depends on how strong the word root is, and that's hard to guess, so you should probably learn it together with the word.
. . . . . . . . . . . . . . . . HARD "G" vs SOFT "G"
HARD "g"(girl): ga, ghe, ghi, go, gu, and the g+consonant
- diga (dam), dighe (dams), ghiro (dormouse), gomma (rubber), gustoso (tasty), grazie (thanks)
SOFT "g"(genuine): ge, gi
- gelato (ice cream), giovedì (thursday)
The rule does not change even with the double "gg"
- hard "g" - leggo (I read), leggono (they read)
- soft "g" - leggi (you read), legge (he/she reads), leggiamo (we read), leggete (you all read)
Furthermore, there are the particular combinations "gn" and "gli", where the "g" sound disappear completely forming two new sounds
- pigna (pine cone), ragno (spider)
- figlio (son), famiglia (family)