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

154
Visualizações
Strings and Arrays in C language
#include <stdio.h>
int main() 
{ 
    printf("%d", "123456"[1]);
    return 0;
}

The expected output is 123456, but it actually outputs 50. Why is that?

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

0

"123456"[1] gives the character '2'. That character's ascii value is 50.

To print the full number do: printf ("%d", 123456); or printf ("%s", "123456");

over 4 years ago · Santiago Trujillo Relatório

0

You are trying to print an integer using %d but you have provided a char instead. To get your expected output, either you have to provide an integer as 2nd argument to printf or use %s as format specifier.

printf ("%d", 123456); or printf ("%s", "123456");

Now, explanation about the output 50 you are getting:

Here, "123456" is a const char *, so when you tried to fetch value from a particular index using [1], it takes the index 1 of your const char * which is char 2.

index -> 0 1 2 3 4 5

input -> 1 2 3 4 5 6

Now, as you were printing using format specifier %d (integer), it printed the ASCII value of char 2, which is 50.

over 4 years ago · Santiago Trujillo Relatório

0

If you need the exact string then there's no need of format specifiers you can simply do

printf("123456");

You are getting output as 50 because in C String are considered as array of char and it can typecast values according to format specifiers too.

So here "123456" becomes an array. char at 1 index is 2. and integer value of char "2" i.e ascii value of 2 is 50. Hence the output.

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