In medieval times, when instruments were first being forged in a sort of way that divided the octave, the only notes that were there at that time were A to G, without any sharps or flats. Now, most singers, due to the peculiarity of the human construction, sing in the C major scale - or did so back then. These accompaniments were thus designed to hit all the notes of the C (major) scale. Looking at it conversely, the 8 notes that humans use to sing mostly were named from C D E F G A and B.
With the advent of chords and improvement in the music theory, the accidentals came in. The Chords G and F are commonly associated with C. To form them we need F# and Bb respectively. Thus they came into existence. In other words, B had to be flattened and F had to be sharpened to get these. Gradually we got the full complement of sharps and flats.
Now, the scales are derived from the Greek modes. These are permutations and combinations of four half and whole notes (tetrachords).
Ionian mode is whole-whole-half. Now two Ionian modes joined by a whole note interval (Ionian-whole-Ionian) will thus be:
whole-whole-half-whole-whole-whole-half (2-2-1-2-2-2-1) or a major scale.
Thus naming all the notes derived by adding intervals in the above order, starting at C, we get the C major scale, where we find that E to F and B to C are half notes apart (semitones).
Liked 'B to C and E to F are semitones - Why the difference?' enough to share / save?