I wanted to create a program in c that reads the student name, roll number, marks of 3 subjects. when I run the program it is showing no errors, but the problem is whenever I try to input information it is taking only 2 inputs. Anyone please check the program and state the error in my program.
#include <stdio.h>
struct student
{
char sname[20];
int srollno;
int smarks[3];
};
int main ()
{
struct student e[3];
int i,j;
for (i=0;i<=2;i++)
{
scanf ("%s",e[i].sname);
scanf ("%d",e[i].srollno);
for (j=0;j<=2;j++)
{
scanf ("%d",e[i].smarks[j]);
}
}
}
it is taking only two inputs.
%das a format specifier toscanf. Hint: it isn't anint, but it leads to one. Also, your loop will breach your array, as it reads four elements (0..3 inclusive). That's undefined behavior. Unrelated, don't letmainlie. It claims to return anintbut doesn't.scanfto better understand how what you're providing to the loopedscanfcall as an output argument isn't valid. The chart on that page will tell youint *is expected for scanf with a%dformat specifier, whereas you're providingint. As a result, your program invokes undefined behavior. The same problem exists for thesrollnomember, only that one is potentially more obvious.