C language Structure array assignment problem __c language

Source: Internet
Author: User
Tags square root
C language Structure array assignment problem Reward Points: 50-resolution Time: 2007-5-16 12:19 Structure Array Assignment problem I encountered this problem when I used the structure array, and I did not understand the reason for a long time, the procedure is as follows:

Main ()
{
struct ABC
{
char name;
int A;
float B;
};
struct ABC stu[3];
int i;
printf ("Please input:/n");
for (i=0;i<3;i++)
scanf ("%c%d%f", &stu[i].name,&stu[i].a,&stu[i].b);
printf ("You just input:/n");
for (i=0;i<3;i++)
printf ("%-5c%-10d%-10.2f", stu[i].name,stu[i].a,stu[i].b);
}

This is a test program that, when assigned to an array of structures, is output to verify that the assignment is correct. However, when the program executes without displaying the result, it exits. The error message reported is: scanf:floating point formats not linked. To change an array of structures to only one structure variable, the procedure is as follows:

Main ()
{
struct ABC
{
char name;
int A;
float B;
};
struct ABC Stu;
printf ("Please input:/n");
scanf ("%c%d%f", &stu.name,&stu.a,&stu.b);
printf ("%5c%10d%10.2f", stu.name,stu.a,stu.b);
}

Compile execution to get the expected results. I would like to verify that the problem is not a loop, but also to change the program to the following: Do not require input data, but in the program to the structure of the variable array of variables to the initial value, and then use the loop output, the program is as follows:

Main ()
{
struct ABC
{char name;
int A;
float B;
};
struct ABC Stu[3] = {{' A ', 12,34},{' B ', 56,78},{' C ', 90,12}};
int i;
for (i=0;i<3;i++)
printf ("/n%-5c%-10d%-10.2f", stu[i].name,stu[i].a,stu[i].b);
}

Compile execution to get the correct result.
In the following changes:
Main ()
{
struct ABC
{
char name;
int A;
float B;
};
struct ABC stu[3];
int i;
printf ("Please input:/n");
scanf ("%c%d%f", &stu[0].name,&stu[0].a,&stu[0].b);
printf ("You just input:/n");
printf ("%-5c%-10d%-10.2f", stu[0].name,stu[0].a,stu[0].b);
}
can be smoothly and correctly executed;
There is no such problem if the members of the structure have no float type;

Finally, it is a problem to assign values in a cyclic way to an array of structures with a float type member. But ask what would have such a problem. Please help the master to solve. Thank you.. Questioner: Hugh Hugh-Assistant two best answer in fact your program is correct. Only the TC is concerned with the float (floating-point number) bug.
Scanf:floating point formats not linked
There is a way to solve this problem, first #include <math.h>
And then in the first sentence of main, call
sqrt (0); Ask for the square root of 0, but discard the result
The effect of this sentence is only to allow TC to open floating-point number support

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.