SCANF probably not reading the correct value
Asked 07 September, 2021
Viewed 1.9K times
  • 60
Votes

If you enter more than five characters in the "name" field the function giving wrong output,otherwise if the input is less than or equal to five character the function works just fine or if I use just '%s' instead of reading five characters using '%5s' then also the function works. Any fix available (I want to use '%5s') ?

#include<stdio.h>
int main(void)
{
  char name[6];
  int age;
  printf("Enter your name: ");
  scanf("%5s",name);
  printf("Enter your age: ");
  scanf("%d",&age);
  printf("Your name: %.4s
",name);
  printf("Your age: %d
",age);
  return 0;
}

0 Answer