I'm a novice in C and I came across the code like this :
int n[10];
if(c>='0' && c<='9')
++n[c-'0']
In if
loop why we have to use single quotes around 0
, whats the use of it, why we can't define 0
as an integer straight away? And in the second line of code ++n[c-'0']
, whats the use of using array like this, in sense why we need to subtract 0
(once again why the use of single quotes in this context?) from c
in the array index?
If i do like this n[c-'0']
, the result of index operation(c-'0'
) will be an character or integer?
Given that can anyone say me, whats the real use of such array and what are the disadvantages well?
Thanks in advance.
'\0' is referred to as NULL character or NULL terminator It is the character equivalent of integer 0(zero) as it refers to nothing In C language it is generally used to mark an end of a string.
“\0” is the null termination character. It is used to mark the end of a string. Without it, the computer has no way to know how long a group of characters (string) goes. In C/C++ it looks for the Null Character to find the end of a string.
Because, the literals are arranged in sequence. So if 0 was 48, 1 will be 49, 2 will be 50 etc.. in ASCII, then x would contain, ascii value of '9' minus the ascii value of '0' which means, ascii value of '9' would be 57 and hence, x would contain 57 - 48 = 9 . Also, char is an integral type. – Silviu.
In C, all escape sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence. For example, \n is an escape sequence that denotes a newline character.
In C, '0'
is an integer whose value represents the digit zero as a character, not the value 0, which would be the null character. Other answers have omitted this, but it's important to note that the C language mandates that the decimal digits have consecutive values, so that if c
is a digit, c-'0'
is the numeric value of that digit, i.e.
'0'-'0' = 0
'1'-'0' = 1
'2'-'0' = 2
.
.
.
'9'-'0' = 9
c
is (likely) a char
, which also has an integer representation and in C it can be converted implicitly. '0'
is the character zero, and a convenient feature of numeric characters is that they are laid out sequentially in their integer representations.
So, now that you know each character has an integer representation and that the number characters are laid out sequentially, you can convert a character to its integer representation using simple subtraction.
'0' - '0' == 0
'1' - '0' == 1
'2' - '0' == 2
/* and so on and so forth */
So if you would like to count the occurrences of digits in a string, you can use this to your advantage:
int n[10]; /* 10 digits */
n['0' - '0'] /* where we store the counts for the character 0, aka n[0] */
n['1' - '0'] /* where we store the counts for the character 1, aka n[1] */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With