Reputation: 33
According to my research, a digit is a numerical character. But I'm not sure if it can also be a question mark, a comma and even a dot for example. Could someone please confirm if I am right?
Upvotes: 0
Views: 926
Reputation: 223795
According to my research, a digit is a numerical character. But I'm not sure if it can also be a question mark, a coma and even a dot for example.
Per C 2018 5.2.1 3, the 10 decimal digits are 0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, and 9
(and nothing else).
Per C 2018 6.4.2.1 1, the digit token in the C grammar is one of 0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, or 9
(and is not anything else).
Per C 2018 7.4.1.4 2, the isdigit
function tests for any decimal-digit character, as defined in 5.2.1.
In certain circumstances, we may accept other characters as digits. The hexadecimal digits include A
, B
, C
, D
, E
, F
, and the lowercase versions. But extensions like this should be explicitly stated or clear from context. An ordinary decimal digit is solely one of the ten characters 0
through 9
.
Are digits only numbers?
When we talk about digits, strings, and numbers, I find it helpful to distinguish numbers from numerals:
0x7B
is also a numeral for 123, and so is 12.3e1
.Given this, a decimal digit is not a number. It is a character, and it can be, by itself or with other digits, a numeral. It can also be part of a numeral with other characters, such as with .
in 3.4
.
Upvotes: -1
Reputation: 8424
Simple Answer
The superficial answer is as Bob___ has referred to, the isdigit()
function which is used to determine whether or not a character is counted by the C programming language as a digit. This is adequate for the vast majority of usages.
More Complex Answer
Where it gets more complicated is when one asks the more fundamental question, "what is a character"?
These days we are very used to 1 byte of data representing characters in the ASCII character table, but in C there's no certainty that that is the case. When C was first created there were many competing character tables, e.g. EBCDIC from IBM. And these days there's various flavours of UTF, with UTF8 being binary compatible with the 127 characters in the ASCII table.
Anyway, all these byte values actually do is tell some graphics engine / terminal display or teleprinter what character glyph or golf ball position to select for drawing / hammering on to the page. Whether a particular byte value is or is not a digit is defined solely by the character table one is assuming applies to the data in the first place.
So if you read a byte from a file that originated on an ancient IBM mainframe that used EBCDIC, your modern Linux C compiler's version of isdigit()
is going to give you the wrong answer as it's expecting the input to be for the ASCII character set. Similarly, if you hook up your Linux to a terminal that's expecting to be fed EBCDIC characters, you're going to end up with a very confusing display. Similarly so if you feed a stream of UTF-16 data to a terminal display that doesn't understand it.
And, this is before one starts considering how many different notations there are for numbers anyway. Most people's expectation of what is a digit is the characters 0,1,2,3,4,5,6,7,8,9. A richer notation throws in +, -, and . or , for the decimal place. Scientific notation may throw in an 'e' or and 'E'. If it's base16, it's commonplace to throw in A,B,C,D,E,F.
So the are many different possible interpretations of what is meant by "a numerical character".
[Incidentally, I recently read that Alan Turing was adept at mental arithmetic in base 17 - something relevant to code breaking in the day. Anyone know how they notated base 17? I've a feeling that A-F for hex is quite modern].
This is all just a long winded way of saying it's kind of up to you to choose what conventions you want to follow. Using standard library functions like isdigit(), printf(), atoi(), atof()
means operating with the conventions that the compiler / library developers chose to adopt, which is a good way of being able to be brief about explaining what conventions you're following to anyone else, and readily interoperable with other code. You might like to choose other well understood conventions (e.g. 0..9,A..F for hexadecimal). Or, if you really wanted to, invent your own!
Upvotes: 0
Reputation: 125007
Digits are exactly the set of numerical characters that you can use to write numbers, i.e. the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
if you're talking about base 10; the set of decimal digits plus {A, B, C, D, E, F}
if you're talking about hexadecimal numbers; the set {0, 1}
if you're talking about binary numbers, etc. No other characters (decimal points, thousands separators, negative indicators, operators, punctuation, brackets/parentheses, etc.) are digits.
Another way to look at it is that digits are exactly the characters that can be used to indicate the value of a single place in a number in any base.
Upvotes: 0
Reputation: 1154
Well, "1" is a digit, and a number as well. "12" is not a digit, but a number.
An number is made out of digits, and additional characters, such as the decimal point, or a sign (plus/minus), or the decimal separator, the later use is only to make large numbers more readable.
Upvotes: 0
Reputation: 154169
Are digits only numbers?
No.
"INF"
, "NaN"
are parse-able as floating point values.
"0xABC"
can parse as an integer. Here, 'A'
is a digit, a hexadecimal digit.
Digits such as "0"
, "12345"
, .... can be parse as a string, not only as a number.
Single decimal digit characters '0'
to '9'
have various possibilities in which they are read and interpreted.
Upvotes: -2