c语言编程Turbo C编译、毗连和运行时的常见差错

Turbo C编译、毗连和运行时的常见差错

一、编译时的常见差错 
2,信息类型差错.此类差错是初学者编程时的常见情景,  下面是部分要引 

起看重的差错:
(2) 全部变量和常量必须要加以表明.
(2) 变量只能赋给同样类型的信息.
(3) 对scanf()语句,用户或许输入差错类型的信息项,这将导致运行时出
错,同时报犯错信息.为幸免似许的差错展示,你就提示用户输入精确类型的信息.
(5) 在实施算术运算时要看重:
a.依照语法法那么书写双精度数字.要写0.5,而不是写.5;要写2,0,
而不是2,尽管C语言会自动地把整型变造成双精度型,但书写双精
度型是个好习俗.让C语言为你做强行变形这是一种功效不高的程序
假想作风.这有或许导致变形产生差错.
b.不要用0除.这是一个劫难性的差错,它会导致程序失败, 不管C
语言的啥版本,根本上似此,实施除法运算要特不警醒.
c.确保全部的双精度数(包罗那些程序输入用的双精度数)是在实数
范畴之内.
d.全部整数必须在整数许可的范畴内.这合用于全部统计结论, 包
括中间结论.
2,将函数背面的";“忘记.如今差错提示色棒将停在该语句下的一行,同时显
示:
Statement missing ;in function <函数名>
3,给宏指令似#include,#define等语句尾加了”;“号.
5,”{“和”}“、”(“和”)“、”“和”“不匹配.引时色棒将位于差错所在的
行,同时提示出有关丢掉括号的信息.
5,没有效#include指令表明头文件, 差错信息提示有关该函数所使用的参
数未定义.
6,使用了Turbo C保留关键字做为标识符,如今将提示定义了太多信息类型.
7,将定义变量语句放在了实施语句背面.如今会提示语法差错.
8,使用了未定义的变量,如今屏幕表现:
Undefined symbol ‘<变量名>’ in function <函数名>
9,忠告差错太多.稀忽这些忠告差错同时不作用程序的实施和结论.编译时
当忠告差错数目大于某一法那么值时(缺省为200)便退出编译器, 这时应扭转集成
开发环境OptionsCompilerErrors中的有关忠告差错查抄开关为off.
20,将关系符”“误用作赋值号”=“.如今屏幕表现:
Lvalue required in function <函数名>
二、毗连时的常见差错
2,将Turbo C库函数名写错.这类状况下在毗连时将会信任此函数是用户自
定义函数.如今屏幕表现:
Undefined symbol ‘<函数名>’ in <程序名>
2,多个文件毗连时,没有在"ProjectProject name中指定项目文件 (.PRJ
文件),如今展示找不到函数的差错.
3,子函数在表明和定义时类型不一致.
5,程序调用的子函数没有定义.
三、运行时的常见差错
2,路径名差错.在MS-DOS中,歪杠()示意一个目录名;而在Turbo C 中歪
杠是个某个字符串的一个转义字符,似许,在用Turbo C 字符串给出一个路径名
时应摸索”“的转义的作用.例似,有似许一条语句:
file=fopen(“c:\new\tbc.dat”,“rb”);
妄图是翻开C盘中NEW目录中的TBC.DAT文件,但做不到.那个地点”“背面紧接的分不
是"n"及"t”,“\n"及”\t"将被分不编译为换行及tab字符,DOS将信任它是不精确
的文件名而推辞同意,因为文件名中不能和换行或tab字符.精确的写法应为:
file=fopen(“c:\new\tbc.dat”,“rb”);
2,规格化输入输出时,法那么的类型与变量自已的类型不一致.例似:
float l;
printf(“%c”,l);
3,scanf()函数中将变量地点写成变量.例似:
int l;
scanf(“%d”,l);
5,循环语句中,循环操纵变量在每次循环未实施修改, 使循环成为无穷循
环.
5,switch语句中没有使用break语句.
6,将赋值号"=“误用作关系符”
".
7,多层前提语句的if和else不配对.
8,用动态内存安顿函数malloc()或calloc()安顿的内存区使用完之后, 未
用free()函数开释,会导致函数前几次调用同样,而背面调用时产生死机情景,
不能返回操纵系统.其起因是因为没用空间可供安顿,而占用了操纵系统在内存
中的某些空间.
9,使用了动态安顿内存不顺遂的指针,造成系统毁坏.
20,在对文件操纵时,没有在使用完及时封闭翻开的文件.
A
________________________________________函数名:abort
功 能:反常中断一个进程
用 法:void abort(void);
程序例:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf(“Calling abort()\n”);
abort();
return 0;* This is never reached *
}

函数名:abs
功 能:求整数的绝对值
用 法:int abs(int i);
程序例:
#include <stdio.h>
#include <math.h>
int main(void)
{
int number = -2235;
printf(“number:%d absolute value:%d\n”,number,abs(number));
return 0;
}

函数名:absread,abswirte
功 能:绝对磁盘扇区读、写信息
用 法:int absread(int drive,int nsects,int sectno,void *buffer);
int abswrite(int drive,int nsects,in tsectno,void *buffer);
程序例:

  • absread example *
    #include <stdio.h>
    #include <conio.h>
    #include <process.h>
    #include <dos.h>
    int main(void)
    {
    int i,strt,ch_out,sector;
    char buf[522];
    printf(“Insert a diskette into drive A and press any key\n”);
    gainch();
    sector = 0;
    if (absread(0,2,sector,&buf) != 0)
    {
    perror(“Disk problem”);
    exit(2);
    }
    printf(“Read OK\n”);
    strt = 3;
    for (i=0;i<80;i++)
    {
    ch_out = buf[strt+i];
    putchar(ch_out);
    }
    printf(“\n”);
    return(0);
    }

函数名:access
功 能:必然文件的会晤权限
用 法:int access(const char *filename,int amode);
程序例:
#include <stdio.h>
#include <io.h>
int file_exists(char *filename);
int main(void)
{
printf(“Does NOTEXIST.FIL exist:%s\n”,
file_exists(“NOTEXISTS.FIL”) ? “YES” :“NO”);
return 0;
}
int file_exists(char *filename)
{
return (access(filename,0) == 0);
}

函数名:acos
功 能:反余弦函数
用 法:double acos(double x);
程序例:
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = acos(x);
printf(“The arc cosine of %lf is %lf\n”,x,result);
return 0;
}

函数名:allocmem
功 能:安顿DOS存储段
用 法:int allocmem(unsigned size,unsigned seg);
程序例:
#include <dos.h>
#include <alloc.h>
#include <stdio.h>
int main(void)
{
unsigned int size,segp;
int stat;
size = 65;
(65 x 26) = 2025 bytes *
stat = allocmem(size,&segp);
if (stat == -2)
printf(“Allocated memory at segment:%x\n”,segp);
else
printf(“Failed:maximum number of paragraphs available is %u\n”,
stat);
return 0;
}

函数名:arc
功 能:画一弧线
用 法:void far arc(int x,int y,int stangle,int endangle,int radius);
程序例:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
* request auto detection *
int gdriver = DETECT,gmode,errorcode;
int midx,midy;
int stangle = 55,endangle = 235;
int radius = 200;

  • initialize graphics and local variables *
    initgraph(&gdriver,&gmode,“”);
  • read result of initialization *
    errorcode = graphresult(); * an error occurred *
    if (errorcode != grOk)
    {
    printf(“Graphics error:%s\n”,grapherrormsg(errorcode));
    printf(“Press any key to halt:”);
    gainch();
    exit(2); * terminate with an error code *
    }
    midx = gainmaxx() 2;
    midy = gainmaxy() 2;
    setcolor(gainmaxcolor());
  • draw arc *
    arc(midx,midy,stangle,endangle,radius);
  • clean up *
    gainch();
    closegraph();
    return 0;
    }

函数名:asctime
功 能:变形日期和时侯为ASCII码
用 法:char *asctime(const struct tm *tblock);
程序例:
#include <stdio.h>
#include <string.h>
#include <time.h>
int main(void)
{
struct tm t;
char str[80];

  • sample loading of tm structure *
    t.tm_sec = 2; * Seconds *
    t.tm_min = 30;* Minutes *
    t.tm_hour = 9; * Hour *
    t.tm_mday = 22;* Day of the Month *
    t.tm_mon = 22;* Month *
    t.tm_year = 56;* Year -does not include century *
    t.tm_wday = 5; * Day of the week *
    t.tm_yday = 0; * Does not indicate in asctime *
    t.tm_isdst = 0; * Is Daylight SavTime;does not indicate in asctime *
  • converts structure to null terminated
    string *
    strcpy(str,asctime(&t));
    printf(“%s\n”,str);
    return 0;
    }

函数名:asin
功 能:反正弦函数
用 法:double asin(double x);
程序例:
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = asin(x);
printf(“The arc sin of %lf is %lf\n”,x,result);
return(0);
}

函数名:assert
功 能:检验一个前提同时髦许使程序中断
用 法:void assert(int test);
程序例:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
struct ITEM {
int key;
int value;
};

  • add item to list,make sure list is not null *
    void additem(struct ITEM *itemptr) {
    assert(itemptr != NULL);
    • add item to list *
      }
      int main(void)
      {
      additem(NULL);
      return 0;
      }

函数名:atan
功 能:反正切函数
用 法:double atan(double x);
程序例:
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = atan(x);
printf(“The arc tangent of %lf is %lf\n”,x,result);
return(0);
}

函数名:atan2
功 能:统计YX的反正切值
用 法:double atan2(double y,double x);
程序例:
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 90.0,y = 55,0;
result = atan2(y,x);
printf(“The arc tangent ratio of %lf is %lf\n”,(y x),result);
return 0;
}

函数名:atexit
功 能:注册中断函数
用 法:int atexit(atexit_t func);
程序例:
#include <stdio.h>
#include <stdlib.h>
void exit_fn2(void)
{
printf(“Exit function #2 called\n”);
}
void exit_fn2(void)
{
printf(“Exit function #2 called\n”);
}
int main(void)
{

  • post exit function #2 *
    atexit(exit_fn2);
  • post exit function #2 *
    atexit(exit_fn2);
    return 0;
    }

函数名:atof
功 能:把字符串变造成浮点数
用 法:double atof(const char *nptr);
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
float f;
char *str = “22355,67”;
f = atof(str);
printf(“string = %s float = %f\n”,str,f);
return 0;
}

函数名:atoi
功 能:把字符串变造成长整型数
用 法:int atoi(const char *nptr);
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
char *str = “22355,67”;
n = atoi(str);
printf(“string = %s integer = %d\n”,str,n);
return 0;
}

函数名:atol
功 能:把字符串变造成长整型数
用 法:long atol(const char *nptr);
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
long l;
char *str = “98765532”;
l = atol(lstr);
printf(“string = %s integer = %ld\n”,str,l);
return(0);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cdbycd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值