If you enter more than five characters in the "name" field the function giving the wrong output, otherwise if the input is less than or equal to five characters the function works just fine, or if I use just %s
instead of reading five characters using %5s
then also the function works.
Is there 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\n",name);
printf("Your age: %d\n",age);
return 0;
}