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.

3
  • 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. Commented Sep 28, 2019 at 10:34
  • now I edited my post . Still I am facing same problem, please tell me where is the mistake in my program. Commented Sep 28, 2019 at 10:45
  • 1
    I did. See scanf 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. Commented Sep 28, 2019 at 10:49

2 Answers 2

1

you have some problem in your scanf. Try this:

scanf("%s",&e[i].sname);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you you sir/madam. It worked. I forgot to address it. Now it is clear.
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.

1 Comment

@SaiSandeepChenna, OK, you can accept one of answers if solved your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.