Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Char - ASCII relation

A char in the C programming language is a fixed-size byte entity designed specifically to be large enough to store a character value from an encoding such as ASCII.

But to what extent are the integer values relating to ASCII encoding interchangeable with the char characters? Is there any way to refer to 'A' as 65 (decimal)?

getchar() returns an integer - presumably this relates directly to such values? Also, if I am not mistaken, it is possible in certain contexts to increment chars ... such that (roughly speaking) '?'+1 == '@'.

Or is such encoding not guaranteed to be ASCII? Does it depend entirely upon the particular environment? Is such manipulation of chars impractical or impossible in C?

Edit: Relevant: C comparison char and int

like image 831
Stumbler Avatar asked Dec 11 '22 19:12

Stumbler


1 Answers

I am answering just the question about incrementing characters, since the other issues are addressed in other answers.

The C standard guarantees that '0' to '9' are consecutive, so you can increment a digit character (except '9') and get the next digit character, or do other arithmetic with them (C 1999 5.2.1 3).

The relationships between other characters are not guaranteed by the C standard, so you would need documentation from your specific C implementation (primarily the compiler) regarding this.

like image 70
Eric Postpischil Avatar answered Dec 19 '22 11:12

Eric Postpischil