0

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.

  • 1
    Read what you're supposed to provide to `%d` as a format specifier to `scanf`. Hint: it isn't an `int`, 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 let `main` lie. It claims to return an `int` but doesn't. – WhozCraig Sep 28 '19 at 10:34
  • now I edited my post . Still I am facing same problem, please tell me where is the mistake in my program. – Sai Sandeep Chenna Sep 28 '19 at 10:45
  • 1
    I did. See [`scanf`](https://en.cppreference.com/w/c/io/fscanf) to better understand how what you're providing to the looped `scanf` call as an output argument isn't valid. The chart on that page will tell you `int *` is expected for scanf with a `%d` format specifier, whereas you're providing `int`. As a result, your program invokes *undefined behavior*. The same problem exists for the `srollno` member, only that one is potentially *more* obvious. – WhozCraig Sep 28 '19 at 10:49

2 Answers2

1

you have some problem in your scanf. Try this:

scanf("%s",&e[i].sname);
yupe
  • 117
  • 1
  • 10
1

I perform some little change on your code. This is working version of code.

Code

#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++)
  {

    printf("Name: ");
    scanf("%s", e[i].sname);
    printf("roolNumber: ");   
    scanf("%d", &e[i].srollno);
    
    
    for (j=0;j<=2;j++)
    {
    
        printf("Mark %d: ",j);
        scanf("%d", &e[i].smarks[j]);
    }
  }

  printf("\n\nprinting \n\n");
  for (i=0;i<=2;i++)
  {

    printf("Name: %s\n", e[i].sname);
    printf("roolNumber: %d\n", e[i].srollno);

    for (j=0;j<=2;j++)
    {
        printf("Mark: %d\n", e[i].smarks[j]);

    }
    printf("\n");
  }
}

Compile and Run

gcc -Wall source.c -o source

./source

I suggest to use printf() before try to scanf(), this makes User Interface better.

scanf() char array don't need & (address) operator. see this

char str[10];
scanf("%s", str);
printf("%s\n", str);

Work exactly like

char str[10];
scanf("%s", &str[0]);
printf("%s\n", str);

Because str is pointer to its first elementstr[0]. As stated in link we can write printf("%d\n", str==&str[0]); and always the result is one that show str and &str[0] are identical.

Community
  • 1
  • 1
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31