We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# coding question?

on
Medium Priority
286 Views
hi, could someone explain to me what is the meaning of " ndigit[c-'0']++" in my following code?
thanks

-----------code--------------------
#include <stdio.h>

main()
{
int c, i, nwhite, nother, nline,ndigit[10];

nwhite = nother = 0;
nline=1;
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while ((c = getchar()) != EOF) {
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':

ndigit[c-'0']++;------------------------------------------->??
break;
case ' ':
case '\t':
nwhite++;
break;
case '\n':
nline++;
break;
default:
nother++;
break;
}
}
printf("digits =");
for (i = 0; i < 10; i++)
printf(" %d", ndigit[i]);
printf(", white space = %d, line# = %d, other = %d\n", nwhite, nline, nother);
return 0;
}
Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
If the character set used is ASCII, in which the character values '0' ... '9' are contiguous, It counts the number of times c=='0' in ndigit[0], the number of times c=='1' in ndigit[1], ..., the number of times c=='9' in ndigit[9],

Commented:
I still confused about the format [c-'0'], why use c minus zero ?
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Assuming you are using the ASCII character set
'0'-'0'==0
'1'-'0'==1
'2'-'0'==2

Commented:
thanks ozo

I understand it conts the number of times like c=='0' in ndigit[0], why can't I just use c like ndigit[c] without -'0'?
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
in ASCII, '0'==48 and '9'==57, so you would have to declare int ndigit['9'+1] instead of int ndigit[10]
the for loops would also need to be changed to for (i = '0'; i <= '9'; i++)

Commented:
thans,
So, at begining c is declare as integer, when goes to switch (c), c are converted to character. that is why we need use c-'0'-----> am I correct about this?

does case must use character? like case '0' ? can I use case 0 instead?
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
There is no conversion.  getchar() returns an int, and '0' is an integer constant.
If your character set is ASCII, '0'==48
you can use case 0: which is the same as case '\0':
or case 48: which is the same as case case '0': (assuming ASCII)

other ASCII character values
0 nul    1 soh    2 stx    3 etx    4 eot    5 enq    6 ack    7 bel
8 bs     9 ht    10 nl    11 vt    12 np    13 cr    14 so    15 si
16 dle   17 dc1   18 dc2   19 dc3   20 dc4   21 nak   22 syn   23 etb
24 can   25 em    26 sub   27 esc   28 fs    29 gs    30 rs    31 us
32 sp    33  !    34  "    35  #    36  \$    37  %    38  &    39  '
40  (    41  )    42  *    43  +    44  ,    45  -    46  .    47  /
48  0    49  1    50  2    51  3    52  4    53  5    54  6    55  7
56  8    57  9    58  :    59  ;    60  <    61  =    62  >    63  ?
64  @    65  A    66  B    67  C    68  D    69  E    70  F    71  G
72  H    73  I    74  J    75  K    76  L    77  M    78  N    79  O
80  P    81  Q    82  R    83  S    84  T    85  U    86  V    87  W
88  X    89  Y    90  Z    91  [    92  \    93  ]    94  ^    95  _
96  `    97  a    98  b    99  c   100  d   101  e   102  f   103  g
104  h   105  i   106  j   107  k   108  l   109  m   110  n   111  o
112  p   113  q   114  r   115  s   116  t   117  u   118  v   119  w
120  x   121  y   122  z   123  {   124  |   125  }   126  ~   127 del

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
hi, ozo

thank you I got it.
as you said,  getchar() returns an int, and assign to c. So how about
case ' '
case '\t'
case '\n':
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
that should be
case ' ':
case '\t':
case '\n':

' ' is sp  space
'\t' is ht  horizontal tab
'\n' is nl  newline
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile