I have created this simple program that takes input from 3 employees. First, it asks for the size of the employee's id. Then it dynamically allocates the size to a character array. Suppose I give the size 4 to the array. Here, I understand we can enter 3 characters as the last one would be '\0'. However, during runtime, I can enter characters more than I initially assumed it would allow to. Why is this happening? Is it expected to get an error?
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int input;
    char *ptr;
    for (int i = 1; i <= 3; i++)
    {
        printf("\nEmployee %d\n", i);
        printf("    Enter the number of characters in your eID: ");
        scanf("%d", &input);
        ptr = (char *)malloc((input + 1) * sizeof(char));
        printf("    Enter your eID: ");
        scanf("%s", ptr);
        printf("    Employee eID entered: %s", ptr);
        free(ptr);
    }
    return 0;
}
Output:
Employee 1
    Enter the number of characters in your eID: 4
    Enter your eID: 12345
    Employee eID entered: 12345
Employee 2
    Enter the number of characters in your eID: 3
    Enter your eID: 1234
    Employee eID entered: 1234
Employee 3
    Enter the number of characters in your eID: 2
    Enter your eID: 123
    Employee eID entered: 123
 
    