# include <stdio.h>
# include <stdlib.h>
#include <string.h>
void main (void)
{
int size, i,j;
printf("enter no. of employee\n");
scanf("%d", &j);
printf("size of employee id\n");
scanf("%d",&size);
char *m[j];
for (i=0; i<j;i++) {
m[i] = (char*) malloc(size*sizeof(char));
}
for (i=0; i<j;i++) {
printf("Enter employee id of %d\n ",i+1);
getchar();
gets(m[i]);
}
printf("employee id are\n ");
for (i=0; i<j;i++){
puts(m[i]);
}
}
i made a programme that would dynamically take input from user about no. of employee and their id size then print it however my output lack first character every time when printing and i haven't been able to spot the error
For starters according to the C Standard the function
mainwithout parameters shall be declared likeThe function
getsis unsafe and is not supported by the C Standard, Instead use standard C function eitherscanforfgets.As for your problem then the first character is lost due to this call
within the for loop.
You could write instead.
And you should free all the allocated memory before exiting the program
Also the program will be much more safer if you will check the result of these calls of
scanfand
at least that the entered values are greater than 0.