0
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct date
{
    int d, m, y;
};
struct student
{
    char name[50];
    int rollno;
    char divi[50];
    float marks;
    char mobile_no[50];
};

struct st2
{
    char name2[40];
    int rollno_1;
    char divi_1[40];
    float marks_1;
    char mobile_no_1[40];
};
struct date_1
{
    int d, m, y;
};

int main()
{

    struct student s;

    printf("Enter the first student information: ");

    printf("\nEnter the name of the student: ");
    scanf("%[^\n]", s.name);

    printf("Enter the roll numner of the student: ");
    scanf("%d", &s.rollno);

    printf("Enter the division of the student: ");
    scanf("%s", s.divi);

    printf("Enter the marks of the student: ");
    scanf("%f", &s.marks);

    printf("Enter the mobile number of the student: ");
    scanf("%s", s.mobile_no);

    struct date d;

    printf("enter the student Date Of Birth: \n");

    printf("DAY: ");
    scanf("%d", &d.d);

    printf("MONTH: ");
    scanf("%d", &d.m);

    printf("YEAR: ");
    scanf("%d", &d.y);

    printf("----------[%s] racord----------\n", s.name);

    printf("| student name is: %s\n", s.name);
    printf("| student roll no is: %d\n", s.rollno);
    printf("| student division is: %s\n", s.divi);
    printf("| student marks is: %f\n", s.marks);
    printf("| student nmobile number is: %s\n", s.mobile_no);
    printf("| student birth date is: %d/%d/%d\n", d.d, d.m, d.y);

    struct st2 f;
    
    printf("Enter the Second student information: ");
    
    printf("\nEnter the name of the student: ");
   
    scanf(" %[^\n]",f.name2); // added space before %[^\n]
    
    printf("Enter the roll number of the student: ");
    scanf("%d", &f.rollno_1);

    printf("Enter the division of the student: ");
    scanf("%s", f.divi_1);

    printf("Enter the marks of the student: ");
    scanf("%f", &f.marks_1);

    printf("Enter the mobile number of the student: ");
    scanf("%s", f.mobile_no_1);

    struct date_1 b;

    printf("enter the student Date Of Birth: \n");

    printf("DAY: ");
    scanf("%d", &b.d);

    printf("MONTH: ");
    scanf("%d", &b.m);

    printf("YEAR: ");
    scanf("%d", &b.y);

    printf("----------[] racord----------\n");

    printf("| student name is: %s\n", f.name2);
    printf("| student roll no is: %d\n", f.rollno_1);
    printf("| student division is: %s\n", f.divi_1);
    printf("| student marks is: %f\n", f.marks_1);
    printf("| student nmobile number is: %s\n", f.mobile_no_1);
    printf("| student birth date is: %d/%d/%d", b.d, b.m, b.y);

    return 0;
}

Output:

Enter the first student information: 
Enter the name of the student: can yaman
Enter the roll numner of the student: 72
Enter the division of the student: a
Enter the marks of the student: 98
Enter the mobile number of the student: 6244738358
enter the student Date Of Birth: 
DAY: 31
MONTH: 5
YEAR: 1993
----------[can yaman] racord----------
| student name is: can yaman
| student roll no is: 72
| student division is: a
| student marks is: 98.000000
| student nmobile number is: 6244738358
| student birth date is: 31/5/1993
Enter the Second student information: 
Enter the name of the student: Enter the roll number of the student:
paddy
  • 60,864
  • 6
  • 61
  • 103
linker
  • 1
  • 1
  • 2
    Most `scanf` format specifiers will skip leading white-space (like newlines from previous inputs). The `%[]` format does *not* skip leading white-space, you need to do it explicitly by adding a leading space in your format string. – Some programmer dude Nov 05 '21 at 07:01
  • 1
    Or better yet, if you want to read full lines use `fgets`. This will also force you to specify the size of the buffer, meaning there's less risks of buffer overflows. – Some programmer dude Nov 05 '21 at 07:01
  • 1
    Why declare two different structures twice to hold the same data? You can declare multiple variables of the same struct type, you don't need a new type every time you want to add an additional variable. – Retired Ninja Nov 05 '21 at 07:16
  • But first statement was successfully run, so what is specific reason – linker Nov 05 '21 at 12:03
  • After reading with `scanf("%d", &d.y);` the `'\n'` is left in the input buffer (`stdin`) unread. Your next call with `scanf()` is `scanf(" %[^\n]",f.name2);` which reads everything up to the next `'\n'` (since `'\n'` is the 1st character waiting to be read -- nothing is read). `scanf()` is so full of pitfalls for the new C programmer, it is recommended to use `fgets()` for all input into a sufficiently sized buffer. Use `sscanf()` to parse data from the buffer or use `strcspn()` to trim the trailing `'\n'` included in the buffer. – David C. Rankin Nov 06 '21 at 01:29
  • Additionally, `scanf()` using the `"%s"` format-specifier is vulnerable to exploit by buffer-overrun just like `gets()`. See [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102). You must include a *width-modifier* to limit the number of characters read to one *less than* the size of the buffer. E.g. with `char buf[10]; you would use `scanf ("%9s", buf);`. You cannot use any input function correctly unless you ***check the return*** to determine in the input succeeded or failed. – David C. Rankin Nov 06 '21 at 01:32

0 Answers0