How to get character's position in alphabet in C language?

ascii, c, char

Solution

int position = 'g' - 'a' + 1;

In C, `char` values are convertible to `int` values and take on their ASCII values. In this case, `'a'` is the same as 97 and `'g'` is 103. Since the alphabet is contiguous within the ASCII character set, subtracting `'a'` from your value gives its relative position. Add 1 if you consider `'a'` to be the first (instead of zeroth) position.

Problem

Is there a quick way to retrieve given character's position in the english alphabet in C? Something like: ``` int position = get_position('g'); ```

Original source

Related problems