Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the real use of using n[c-'0']?

Tags:

c++

arrays

c

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.

like image 859
Ant's Avatar asked Sep 13 '11 14:09

Ant's


People also ask

What is the purpose of '\ 0 character in C?

'\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.

What is the meaning of '\ 0?

“\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.

Why do we subtract 0 from 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.

What does \n do in C?

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.


2 Answers

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
like image 113
R.. GitHub STOP HELPING ICE Avatar answered Oct 01 '22 18:10

R.. GitHub STOP HELPING ICE


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] */
like image 36
user7116 Avatar answered Oct 01 '22 18:10

user7116