Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

c - What is wrong with using %var after format specifier in a scanf call?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Read Modern C and see this C reference

Your scanf statement should be (using the & prefix "address-of" operator):

 scanf("%d,%d",&a,&b);

and your code is incomplete, since scanf can fail. You should read its documentation.

Consider coding a test against such failure using:

if (scanf("%d,%d", &a, &b) < 2) {
   perror("scanf failure");
   exit(EXIT_FAILURE);
}

Read also the documentation of your C compiler, e.g. GCC, and of your debugger, e.g. GDB. You could compile your code with gcc -Wall -Wextra -g to get all warnings and debug information, and later use gdb to debug your executable and understand its runtime behavior.

Your code is missing a #include <stdio.h> before your main.

The printif is wrong. You want to use printf (and read its documentation).

Consider studying for inspiration the source code of some existing free software, such as GNU make or GNU bash.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...