string in C returning symbols instead of characters

Asked

Viewed 42 times

0

I have this simple code in C language that gets an input value and returns the value on the screen. Instead of showing the string on the screen, it shows symbols like bars. When I do the procedure with integer numbers, it shows the value correctly, the error only occurs when I do with strings.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int main(void)
{
    char nome[61];

    printf("Digite seu nome: ");
    scanf_s("%s", nome);

    printf("O nome armazenado foi: %s", nome);

    //getch();
    return 0;

}
  • 1

    scanf_s? use scanf only

  • 1

    Here generates access violation exception when recording.

  • when using scanf it shows an error. Erro C4996: 'scanf': esta função ou variável pode não ser segura. Considere usar scanf_s em vez disso. Para desativar a suspensão de uso, use _CRT_SECURE_NO_WARNINGS. Consulte a ajuda online para obter detalhes. Inseri #define _CRT_SECURE_NO_WARNINGS before includes and worked. Thanks!

  • 1

    The scanf_s is a variant of Microsoft to specify amount of input read and avoid buffer overflow Attack. This means that your reading is wrong because the size of the reading is missing.

1 answer

0

Hello! Use the gets function().

#include<stdio.h>

#include<stdlib. h> #include<conio. h>

int main(void) { char name[61];

printf("Digite seu nome: ");
gets(nome);

printf("O nome armazenado foi: %s", nome);

return 0;

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.