------------------------------------------------------------------------
<> 本意:接收字符串.
写成代码:void main()
{
char *str;
scanf("%s",str);
printf("string is: %s\n",str);
}
符合愿意代码:char *str=NULL;
str=malloc(*sizeof(char) );
scanf( "%s\n", str );
点评:指针需要你手动给它分配空间,并手动指向该空间如果没有,指针指向哪里,是不确定的
也就是说,你scanf得到的数据存放到哪里是不一定的因此,偶尔有运行正常是你运气好
错误才是正常的
-----------------------------------------------------------------------
<> 本意:接收输入的a,b值.
写成代码:int a,b;
scanf("%d%d",a,b);
符合愿意代码:int a,b;
scanf("%d%d",&a,&b);
点评:这是不合法的。Scanf函数的作用是:按照a、b在内存的地址将
a、b的值存进去。“&a”指a在内存中的地址。
------------------------------------------------------------------------
<> 本意:在Input字符串后输入数值.
写成代码:int num;
Scanf("Input %d", & num);
实际应输入:Input 或者 Input1234
符合愿意代码:int num;
printf("Input";
scanf("%d",&num);
------------------------------------------------------------------------
<> 本意:接收填入的数据.
写成代码:#include <stdio.h>
main()
{
int num;
printf("please input the student's score: ";
scanf("%d",&num); if((num<)||(num>))
{
printf("The score put isnt fine. please run and input again.";
}
else if(num>)
{
printf("The grade is A.";
} else if((num>)&&(num<))
{
printf(..................
.............
}
.............. }
实际应输入:这个程序是没错,不过如果有人要存心捣乱, 输入时不是输入数字,而是其
他的什么字符,那么congratulations,这个程序崩溃掉了.
符合愿意代码:#include <stdio.h>
main()
{
int num,int result=;
printf("please input the student's score: "; while(result==)
{
fflush(stdin); /* 清空输入缓冲区. */
if(result!=)printf("lease input a digital score: ";
result=scanf("%d",&num);
}
............................
}
点评:scanf函数执行成功时的返回值为成功读取的变量数,如果第一个变量的读取既告失败则返回值为0.
我们可以通过判断scanf函数执行的返回值, 可以制止用户不正确地输入,从而控制程序的流程.
另 :#include <stdio.h>
int main()
{
char b[];
scanf("%s", b);
printf("%s\n", b);
}
如果输入的字符比较多例如10个,就会seg fault,可见scanf是不安全的,没有检查缓冲区。
同样,把上面的scanf换成gets,效果是一样的。(而且编译的时候有warning,不让用gets)。
fgets是安全的,这样用法:
fgets(b, , stdin);
注意,这样子其实只读了一个字符,第二个字符是0。所以如果输入是sad,则b[]='s', b[]=.
由此可见,读入字符串时,fgets更安全。
------------------------------------------------------------------------
<> 本意:接收带空格等的字符串.
写成代码:#include <stdio.h>
void main(){
char c[];
scanf("%s", c);
printf("%s", c);
}
输入:welcome to come here
输出:welcome
符合愿意代码:换用gets();
点评:因为输入终端的buffer把空白字符(包括空格。\t等)当成字符串分隔符,
你用过命令行参数就明白了,main函数的参数是 int main(int,char*[])
------------------------------------------------------------------------
<> 本意:接收规定精度的数据.
写成代码:scanf("%7.2f",&a);
实际应输入:
符合愿意代码:
点评:这样做是不合法的,输入数据时不能规定精度。
------------------------------------------------------------------------
<> 本意:正确输入a,b的值.
写成代码:#include <stdio.h>
int main()
{
int a,b,c; /*计算a+b*/
scanf("%d,%d",&a,&b);
c=a+b;
printf("%d+%d=%d",a,b,c);
}
现象:一旦输入了错误的类型,程序不是死锁,就是得到一个错误的结果。
符合愿意代码:#include <stdio.h>
int main()
{
int a,b,c; /*计算a+b*/
while(scanf("%d,%d",&a,&b)!=)fflush(stdin);
c=a+b;
printf("%d+%d=%d",a,b,c);
}
点评:scanf()函数执行成功时的返回值是成功读取的变量数,也就是说,
你这个scanf()函数有几个变量,如果scanf()函数全部正常读取,
它就返回几。但这里还要注意另一个问题,如果输入了非法数据,
键盘缓冲区就可能还个有残余信息问题。
------------------------------------------------------------------------
<> 本意:可以连续多次接受数据.
写成代码:#include <stdio.h>
int main()
{
int a;
char c;
do
{
scanf("%d",&a);
scanf("%c",&c);
printf("a=%d c=%c\n",a,c);
/*printf("c=%d\n",c);*/
}while(c!='N');
}
现象:scanf("%c",&c);这句不能正常接收字符
符合愿意代码:#include <stdio.h>
int main()
{
int a;
char c;
do
{ scanf("%d",&a);
fflush(stdin);
scanf("%c",&c);
fflush(stdin);
printf("a=%d c=%c\n",a,c);
}while(c!='N');
}
点评:我们用printf("c=%d\n",c);将C用int表示出来,启用printf("c=%d\n",c);这一句,
看看scanf()函数赋给C到底是什么,结果是 c= ,ASCII值为10是什么?换行即\n.
对了,我们每击打一下"Enter"键,向键盘缓冲区发去一个“回车”(\r),一个“换行"
(\n),在这里\r被scanf()函数处理掉了(姑且这么认为吧^_^),而\n被scanf()函数
“错误”地赋给了c.
另:fflush(FILE *stream)函数,其主要功能:可将所有缓冲区数据写入指定的流文件将
清空缓冲区。
------------------------------------------------------------------------
<> 本意:接收float型数值.
写成代码:#include "stdio.h"
main()
{
int i=;
struct BOOK
{
char bookName[];
float bookPrice;
};
struct BOOK book[];
for(i=;i<;i++)
{
scanf("%f",&book[i].bookPrice);
}
}
现象: 编译通过,但运行时报错(TC3.):
scanf : floating point formats not linked.
Abnormal program termination 。
符合愿意代码:#include "stdio.h"
main()
{
int i=;
float t=;
struct BOOK
{
char bookName[];
float bookPrice;
};
struct BOOK book[];
for(i=;i<;i++)
{
scanf("%f",&t);
book[i].bookPrice=t;
}
}
相关参考资料:
Description:
This document explains why you might be getting the error
FLOATING POINT FORMATS NOT LINKED : ABNORMAL PROGRAM TERMINATION
and tells you how to resolve it. The problems and solutions
below apply to ALL versions of Turbo C, Turbo C++, and Borland
C++, except where noted.
What are floating point formats?
Floating point formats are a collection of formatting information
used to manipulate floating point numbers in certain runtime
library functions such as scanf() and atof().
When will this be fixed?
There are no current plans to fix this because it is not a bug.
The intent is to avoid linking the floating point formats (about
1K of overhead) when they are not required. The tradeoff of this
feature is that the programmer must explicitly request that the
floating point formats to be linked in for some programs which
manipulate floats in a limited and specific fashion.
How do I resolve the error message?
Since you can get the error in a number of different ways, check
the following list of potential causes to find out how to resolve
the error. These are listed in order of most common to least
common causes.
. CAUSE: Floating point set to . Your have your
floating point option set to None when it should be set to
either Emulation or 80x87.
FIX: Set Floating Point to or <80x87>. In the
Integrated Development Environment (IDE), this is either
under Options | Compiler | Advanced Code Generation or
Options | Compiler | Code Generation | More, depending upon
which compiler you have. With the command line compiler, use
the appropriate -f switch.
. CAUSE: Misordered libraries when executing TLINK
(Cx.LIB listed before EMU.LIB will cause the error.)
FIX: This possibility usually occurs only when you are using
the command line compiler and are explicitly calling TLINK
separately from BCC or TCC. When executing TLINK, change the
order of the libraries to
[user libs] [GRAPHICS.LIB] EMU.LIB MATHx.LIB Cx.LIB
(libraries in brackets are optional)
Note: There is a misprint in the Borland C++ Tools &
Utilities Guide on page that displays the wrong order for
libraries on the TLINK command line. The ordering shown in
the manual is exactly what will cause floating point formats
not linked.
. CAUSE: Either the compiler is overoptimizing, or the
floating point formats really do need to be linked in because
your program manipulates floats in a limited and specific
fashion. Under certain obscure conditions, the compiler will
ignore floating point usage in scanf(). (e.g., trying to
read into a float variable that is part of an array contained
in a structure.)
FIX: If you have Borland C++ 3.0 or later, read Part A. If
you have Borland C++ 2.0 or any Turbo C or Turbo C++
compiler, read Part B. This fix is the only fix that will
solve a "RINTF : Floating point formats not linked" error
message occurring with inline assembly.
Part A (BC++ 3.0 or later):
Add the following to one source module:
extern _floatconvert;
#pragma extref _floatconvert
The README and HELPME!.DOC files that shipped with
Borland C++ 3.0 incorrectly say that only
#pragma extref _floatconvert
is required in order to resolve the FPFNL error. If you
do not include the "extern _floatconvert;" line you will
get the error "Undefined symbol _floatconvert." You will
also get the same undefined symbol if the "extern
_floatconvert" comes after the #pragma line instead of
before. Note that the #pragma line does not have a
semicolon at the end of the line. If you put a semicolon
there, you will get the error "Bad pragma directive
syntax."
The README that shipped with Borland C++ 3.1 says that
extern void _floatconvert();
#pragma extref _floatconvert
This should work, as well. It doesn't really matter
whether _floatconvert is a variable or a function; it
only matters that it is some symbol that the linker will
recognize.
The HELPME!.DOC for BC++ 3.1 has the correct two lines to
add.
Part B (BC++ 2.0 or TC or TC++):
Add the following force_fpf() function to one source
module. It is not necessary to call this function; just
include it in one of your modules.
static void force_fpf()
{
float x, *y; /* Just declares two variables */
y = &x; /* Forces linkage of FP formats */
x = *y; /* Suppress warning message about x */
}
. CAUSE: Forgetting to put the address operator & on the scanf
variable expression. For example,
float foo;
scanf("%f", foo);
FIX: Change the code so that the & operator is used where it
is needed. For example, the above code should be
float foo;
scanf("%f", &foo);
. CAUSE: A bug in Turbo C 2.0 when using scanf()
FIX: Obtain and apply the patches in TC2PAT.ARC. This file
can be downloaded from the Languages / C++ / Patches section
on DLBBS (--).
. CAUSE: A bug in Turbo C 2.01 when using atof() or strtod()
FIX: Obtain and apply the patches in TC21PT.ARC. This file
can be downloaded from the Languages / C++ / Patches section
on DLBBS (--).
. CAUSE: You are trying to create a Phar Lap DOS Extender
application with the Integrated Development Environment
(IDE).
FIX: Phar Lap includes an executable called BCC286.EXE with
their DOS Extender. This program calls Borland's command-
line compiler (BCC) and command-line linker (TLINK). Since
the linker in the IDE is different than the linker at the
command line, you cannot create Phar Lap DOS Extender
applications in the IDE and expect them to run properly. If
you try to do so, you might get a floating point formats not
linked error message. The fix is to use the command line
tools, BCC and TLINK, instead of the IDE.
Keywords: FPFNL , APT
DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.
------------------------------------------------------------------------
其他:
在scanf函数中,我们可以使用 %c来读取一个字符,使用 %s 读取一个字符串. 但是读取字
符串时不忽略空格,读字符串时忽略开始的空格,并且读到空格为止,因此我们只能读取一个单
词,而不是整行字符串.因此一般使用fgets来读取一个字符串.其实scanf函数也可完成这样的
功能,而且还更强大.
这里主要介绍一个参数,%[ ] ,这个参数的意义是读入一个字符集合. [ ]是个集合的标
志,因此%[ ]特指读入此集合所限定的那些字符, 比如 %[A-Z] 是输入大写字母,一旦遇到不在
此集合的字符便停止. 如果集合的第一个字符是" ^ ", 这说明读取不在" ^ " 后面集合的字
符,既遇到" ^ " 后面集合的字符便停止.注意此时读入的字符串是可以含有空格的.
Eg. 输入一个字符串, 这个字符串只含有小写字符.遇到第一个不是小写字符时停止.
scanf("%[a-z],str);
Eg. 想输入一个字符串, 遇到 "." 停止,可设计如下:
scanf("%[^.]", str);
使用这个参数,你可以完成许多强大的功能呦!

引用:

scanf()常犯错误

scanf()常犯错误的更多相关文章

  1. Python 新手常犯错误

    Python 新手常犯错误(第二部分) 转发自:http://blog.jobbole.com/43826/ 作用域 在这篇文章里,我们来关注作用域在Python被误用的地方.通常,当我们定义了一个全 ...

  2. Python开发最常犯错误总结10种

    不管是在学习还是工作过程中,人都会犯错.虽然Python的语法简单.灵活,但也一样存在一些不小的坑,一不小心,初学者和资深Python程序员都有可能会栽跟头.本文是Toptal网站的程序员梳理的10大 ...

  3. Python 新手常犯错误(第二部分)

    转发自:http://blog.jobbole.com/43826/ 在之前几个月里,我教一些不了解Python的孩子来慢慢熟悉这门语言.渐渐地,我发现了一些几乎所有Python初学者都会犯的错误,所 ...

  4. Python 新手常犯错误(第一部分)

    转载自:http://blog.jobbole.com/42706/ 在之前几个月里,我教一些不了解Python的孩子来慢慢熟悉这门语言.渐渐地,我发现了一些几乎所有Python初学者都会犯的错误,所 ...

  5. 使用DX绘制3D物体时新手常犯错误,看不见物体时可以一一排查

    1.镜头不对: 物体不在镜头范围内,检查视图矩阵,世界矩阵,投影矩阵. 2.颜色全黑: 打开光照情况下,MATERIAL全为0, 或,在没有打开光照情况下,颜色值为0,造成全黑.检查当前Materia ...

  6. Python 新手常犯错误(第一部分)转载

    觉得这篇文章针对python的默认参数写的不错,翻译的也不错,故转载下. 原文链接: Amir Rachum   翻译: 伯乐在线- 伯乐在线读者译文链接: http://blog.jobbole.c ...

  7. android常犯错误记录

    错误:Error:Error: Found item Attr/border_width more than one time 这个容易,属性相同了,按照提示查询一下找出来删了就行了,注意大小写很容易 ...

  8. 扩展欧几里得算法(extended Euclidean algorithm)的一个常犯错误

    int exGcd(int x,int y,int& a,int& b) //ax+by=gcd(x,y) { ; b=; return x; } int res=exGcd(y,x% ...

  9. 类模板语法知识体系梳理(包含大量常犯错误demo,尤其滥用友元函数的错误)

    demo 1 #include <iostream> #include <cstdio> using namespace std; //template <typenam ...

随机推荐

  1. UVAlive 2326 Moving Tables(贪心 + 区间问题)

    The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in ...

  2. ecshop标签大全 各个页面常用标签大全

    先从index.php主页开始 页面关键字 {$keywords } 页面标题 {$page_title} 产品分类 父分类列表 {foreach from=$categories item=cat ...

  3. openstack之Nova

    一.Nova简介及其核心组件: Nove在openstack中提供计算服务: Nova核心模块: Controller Nova-api:提供API,包括命令行API; Nova-schedule: ...

  4. Razor强类型视图下的文件上传

    域模型Users.cs using System;using System.Collections.Generic;using System.Linq;using System.Web; namesp ...

  5. ASP.NET上传文件的三种基本方法

    ASP.NET依托.net framework类库,封装了大量的功能,使得上传文件非常简单,主要有以下三种基本方法. 方法一:用Web控件FileUpload,上传到网站根目录. Test.aspx关 ...

  6. string.Format()字符串格式化

    Format()基本语法:     {索引[,对齐][:格式字符串]}     ·索引:表示引用的对象列表中的第n个对象参数.     ·对齐(可选):设置宽度与对齐方式,该参数为带符号的整数.正数为 ...

  7. Xcode - 详解真机测试步骤

    第一种从iOS9.0之后推出的免费开发者账号 1.注册开发者 * 注册Apple ID * 使用Apple ID登录苹果开发者中心,注册成为开发者 * 此过程为免费,只是为了让普通的Apple ID具 ...

  8. BZOJ 2748 音量调节

           这道题我开始做时想用搜索来做,但是失败了,后来仔细一想发现这就是一个背包问题,之后一切就简单多了.        代码如下: #include<cstdio> #includ ...

  9. BZOJ 100题留念

  10. MYSQL 好文章集锦

    比较细致的讲解MySQL数据库的数据结构以及实现原理 MySQL索引背后的数据结构及算法原理   MySQL的InnoDB索引原理详解 MySQL索引原理及慢查询优化 持续更新,快乐学习.