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

317
Visualizações
Passing not null terminated string to printf results in unexpected value

This C program gives a weird result:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
   char str1[5] = "abcde";
   char str2[5] = " haha";

   printf("%s\n", str1);
   return 0;
}

when I run this code I get:

abcde haha

I only want to print the first string as can be seen from the code.
Why does it print both of them?

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

0

"abcde" is actually 6 bytes long because of the null terminating character in C strings. When you do this:

char str1[5] = "abcde";

You aren't storing the null terminating character so it is not a proper string.

When you do this:

char str1[5] = "abcde";
char str2[5] = " haha";
printf("%s\n", str1);

It just happens to be that the second string is stored right after the first, although this is not required. By calling printf on a string that isn't null terminated you have already caused undefined behavior.

Update:

As stated in the comments by clcto this can be avoided by not explicitly specifying the size of the array and letting the compiler determine it based off of the string:

char str1[] = "abcde";

or use a pointer instead if that works for your use case, although they are not the same:

const char *str1 = "abcde";
over 4 years ago · Santiago Trujillo Relatório

0

Both strings str1 and str2 are not null terminated. Therefore the statement

 printf("%s\n", str1);  

will invoke undefined behavior.
printf prints the characters in a string one by one until it encounters a '\0' which is not present in your string. In this case printf continues past the end of the string until it finds a null character somewhere in the memory. In your case it seems that printf past the end of string "abcde" and continues to print the characters from second string " haha" which is by chance located just after first string in the memory.

Better to change the block

   char str1[5] = "abcde";
   char str2[5] = " haha";  

to

 char str1[] = "abcde";
 char str2[] = " haha";  

to avoid this problem.

over 4 years ago · Santiago Trujillo Relatório

0

Technically, this behavior is not unexpected, it is undefined: your code is passing a pointer to a C string that lacks null terminator to printf, which is undefined behavior.

In your case, though, it happens that the compiler places two strings back-to-back in memory, so printf runs into null terminator after printing str2, which explains the result that you get.

If you would like to print only the first string, add space for null terminator, like this:

char str1[6] = "abcde";

Better yet, let the compiler compute the correct size for you:

char str1[] = "abcde";
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