#include<stdio.h>
#include<stdlib.h>
int main() {
int i, n;
printf("Enter length of employee id:\n");
scanf("%d", & n);
char * arr = (char * ) malloc((n + 1) * sizeof(char));
printf("Enter employee id:\n");
// scanf("%s", arr);
for (i = 0; i < n; i++) {
printf("Enter %dth character of employee id:\n", i + 1);
scanf("%c", (arr + i));
}
for (i = 0; i < n; i++) {
printf("%c", *(arr + i));
}
}
In the given code I want to get input of each character of employee id one by one by one using a for loop and the scanf function. Somehow this isn't working. I know the input can be taken in the form of a string(that is why i have allocated (n + 1) terms while allocating the memory) but I want to know why isn't working in this case.
The output I am getting for n = 3 is:
Enter length of employee id:
3
Enter employee id:
Enter 1th character of employee id:
Enter 2th character of employee id:
d
Enter 3th character of employee id:
d
I cannot input the first and third character, why is this?