How to Use scanf statements to input multiple values in C Language

Source: Internet
Author: User
Today, using community variables to write a small program requirement: There are several personnel data, including students and teachers. Student Data includes name, number, gender, occupation, and class. Instructor data includes name, number, gender, occupation, and position. The same table must be used for processing. After compilation, run the program and find that the output has a problem. The problem occurs in the scanf statement through debugging. The original scanf statement is scanf ("% d % S % C ", & person [I]. num, & person [I]. name, & person [I]. sex, & person [I]. job. the job value is & person [I]. sex overwrites M. 1. Try to add a space in the middle of % d % S % C, and the output result returns normally. 2. You want to add a comma between % d % S % C, and the output result is still incorrect. However, when writing a simple scanf function, both methods 1 and 2 can output normal results. I wonder if this problem is caused by the use of struct variables or community variables. Do you have any special requirements on the input/output format when using struct or community variables? Please help me. Source code (corrected): # include <stdio. h> struct student {int num; char name [20]; char sex; char job; Union {int CLAS; char position [20];} category;} person [2]; int main () {int I; for (I = 0; I <2; I ++) {printf ("Please enter the date of person: \ n "); scanf ("% d % S % C", & person [I]. num, & person [I]. name, & person [I]. sex, & person [I]. job); If (person [I]. job ='s) {printf ("Enter the student's class: \ n"); scanf ("% d", & person [I]. category. CLAS);} else if (person [I]. job = 'T') {printf ("Enter the instructor's position: \ n"); scanf ("% s", & person [I]. category. position);} elseprintf ("incorrect input job type");} printf ("\ n"); for (I = 0; I <2; I ++) {If (person [I]. job ='s) printf ("the student's information is as follows: % 6D % 10 S % 4C % 4C % 10d \ n", person [I]. num, person [I]. name, person [I]. sex, person [I]. job, person [I]. category. CLAS); elseprintf ("the instructor information is as follows: % 6D % 10 S % 4C % 4C % 10s \ n", person [I]. num, person [I]. name, person [I]. sex, person [I]. job, person [I]. category. position);} return 0 ;}
Let me help.

The entered content has reached the length limit. You can also enter 9999 words to insert an image, delete an image, insert a map, delete a map, and insert a video or video map.

You can also reply if you do not log on.

References:
Submit answers
Cancel

Satisfactory answer

There is a very serious error. You enter two scanf characters in the character array ("% d % S % C", & person [I]. num, & person [I]. name, & person [I]. sex, & person [I]. job); scanf ("% s", & person [I]. category. position); remove the & symbol before the character array input in the two rows. The input to the character array is the address, do not add & change to scanf ("% d % S % C", & person [I]. num, person [I]. name, & person [I]. sex, & person [I]. job); scanf ("% s", person [I]. category. position); there are no other errors when you remember to input them, the values are separated by a space.
Question
Person [I]. Num is a member of the struct array. To assign values to members, add? I wrote the statement as follows but the output result is correct. I wonder if there are any internal risks? I just started to learn the C language. I hope you can help me introduce it.
Answer
When using the scanf () function, the essence is that you give scanf an address. This function will put the data in the input buffer at the specified address, such as int, Char, double, long, float are the basic types. When allocating space, you only need to give them a space. Therefore, when using scanf () for input, you need to add the & get address symbol, in order to assign values to them. However, char *, char [], these are the first addresses of a region, and they are the addresses themselves. Therefore, when scanf is used, do not add them, you do not need to add only person [I] in the input. name, person [I]. category. position, and the rest are basic types. Therefore, you must add
Question
I understand, as I previously wrote, there may be no problem during running, but the person [I] stored in the memory. category. position data is only the first character of the input string, right? This debugging process can be seen (the debugging program just learned today ).
Related Article

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.