Home>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Book_data {
char title [50];
int price;
struct Book_data * next;
} Book_data;
Book_data * allocNode (char * title, int price, Book_data * next) {
Book_data * Node = (Book_data *) malloc (sizeof (Book_data));
memcpy (Node->title, title, sizeof (Node->title));
Node->price = price;
Node->next = next;
return Node;
}
int main () {
Book_data * top = NULL;
char title [50];
int price;
char c;
while (1) {
printf ("Book title price y/N:");
if (scanf ("% s% d% c", title,&price,&c)! = 3)
puts ("Erorr");
top = allocNode (title, price, top);
if (c =='N')
break;
}
Book_data * node = top;
for (;node;node = node->next) {
printf ("% s", node->title);
printf ("% d Yen \ n", node->price);
}
return 0;
}
Since I learned about scanf () in the previous question, I wrote a program using scanf and a list.
In the previous answerer
The return value of scanf is! = N (n = 1,2 ...) I wrote it because it was like this.
Please tell me the meaning of writing! = 3.
Also, please point out any problems with the source code.
-
Answer # 1
-
Answer # 2
Please tell me the meaning of writing! = 3.
scanf () returns the number of items read as the return value.
In the case of "scanf ("% s% d% c ", title,&price,&c)", there are 3 input values, so if the return value is not 3, it is probably an error as reading failed.Reference: C language function dictionary
Related articles
- about the c language scanf function
- about the difference between scanf ("% d% d", ~~) and scanf ("% d% d", ~~) in c
- about warnings at the time of compiler
- c - about getting time with arduino
- about initialization of c language array
- about how to store a c language txt file in an array
- about checking command line arguments
- about the error when converting a floating point number in c language to an integer
- c language: about the location of random number expressions
- about the program that sorts and displays command line arguments
- about float type operation of c language
- about the display of the contents of the matrix
- about combinations and modulo operations
- about binary search tree structure
- about c language structure, typedef
- about c language pointers
- python - i'm worried about the direction of studying machine learning
- c - about fortran data
- about the comparison function of c language bsearch function
Trends
Try entering non-numeric characters in the price, such as "Algorithm Book ¥ 980 y".
What happens? It would be ridiculous.
Therefore, you have to check whether the input was successful as follows.
Or