Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
What does s[i] - '0' mean?

The following code is from K&R textbook, page number 71:

val =10.0*val+s[i] -'0'

What does s[i] -'0' mean here?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

It seems that s is a character array or a pointer to the first element of a character array. And element s[i] contains a character that represents a digit as for example '5' . To convert this character that for example in ASCII has internal code equal to 53 (while internal code of character '0' is 48) there is used expression

s[i] -'0'

that is equivalent to

53 - 48

and equal to number 5

According to the C Standard (5.2.1 Character sets)

3...In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

So in ASCII there is a relation

Character Code value

    '0' - 48
    '1' - 49
    '2' - 50
    '3' - 51
    '4' - 52
    '5' - 53
    '6' - 54
    '7' - 55
    '8' - 56
    '9' - 57

For example in EBCDIC internal codes of characters that represent digits are 240 - 249 ( 0 - 9 ) or in hexadecimal notation F0 - F9.

So it is a standard way to get numeric digit from a character independing on used character set.

over 4 years ago · Santiago Trujillo Relatório

0

It converts an int in char form into an actual int.

For example, if s[i] is '9' then s[i] - '0' will produce 9.

over 4 years ago · Santiago Trujillo Relatório

0

Probably the code is used to convert a string with decimal digits into the represented number (e.g. "1234" into 1234).

s[i] is the current digit, s[i]-'0' is the numerical value of the current digit (e.g. '9' becomes 9).

The rest of the C code is just how positional numerical systems works.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda