首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

■◆输出带颜色和重新定位光标两段小程序编译有关问题◆■

2012-02-10 
■◆输出带颜色和重新定位光标两段小程序编译问题◆■以下是我在Linux0.01内核分析与操作系统设计一书中看到的

■◆输出带颜色和重新定位光标两段小程序编译问题◆■
以下是我在Linux0.01内核分析与操作系统设计一书中看到的例子程序,程序一是输出带颜色的HELLO WORLD功能,程序二是重新定位光标的功能,但编译有错误,本人新手,请各位指教。
程序一:
/* hello_s.c */
#defineBLACK 0
#defineBLUE 1
#defineGREEN 2
#defineCYAN 3
#defineRED 4
#defineMAGENTA 5
#defineBROWN 6
#defineLIGHTGRAY 7
#defineDARKGRAY 8
#defineLIGHTBLUE 9
#defineLIGHTGREEN 10
#defineLIGHTCYAN 11
#defineLIGHTRED 12
#defineLIGHTMAGENTA 13
#defineYELLOW 14
#defineWHITE 15
#defineBLINK 28

void write_string(char *pstring, int color)
{
  char far *pvideo = (char far *)0xB8000000;
  while(*pstring)
  {
  *pvideo=*pstring;
  pstring++;
  pvideo++;
  *pvideo=color;
  pvideo++;
  }
}

void main(){
  write_string("Hello,world!", RED);
}

编译错误:
C:\c>gcc hello_s.c -o hel.exe
hello_s.c: In function `write_string':
hello_s.c:22: syntax error before '*' token
hello_s.c:25: `pvideo' undeclared (first use in this function)
hello_s.c:25: (Each undeclared identifier is reported only once
hello_s.c:25: for each function it appears in.)
hello_s.c: In function `main':
hello_s.c:33: warning: return type of `main' is not `int'


程序二:
/* Compiled by DJGPP */
#include <stdio.h>
#include <pc.h>

void update_cursor(int row, int col)
{
  USHORT position=(row*80) + col;
  /* cursor LOW port to VGA INDEX register */
  outb(0x3D4, 0x0F);
  outb(0x3D5, (UCHAR)(position&0xFF));
  /* cursor HIGH port to vga INDEX register */
  outb(0x3D4, 0x0E);
  outb(0x3D5, (UCHAR)((position>>8)&0xFF));
}

void main(){
  clrscr();
  update_cursor(10, 10);
}

编译错误:
C:\c>gcc cursor.c -o cur.exe
cursor.c: In function `update_cursor':
cursor.c:7: `USHORT' undeclared (first use in this function)
cursor.c:7: (Each undeclared identifier is reported only once
cursor.c:7: for each function it appears in.)
cursor.c:7: parse error before "position"
cursor.c:10: `UCHAR' undeclared (first use in this function)
cursor.c:10: `position' undeclared (first use in this function)
cursor.c: In function `main':
cursor.c:16: warning: return type of `main' is not `int'


[解决办法]
程序一需要改动的地方:
char far *pvideo = (char far *)0xB8000000; 
改为
char *pvideo = (char *)0xB8000000L; 

void main(){ 
write_string("Hello,world!", RED); 
}
改为
int main(){ 
write_string("Hello,world!", RED); 
return 0;

 
程序二
在程序头部加入
typedef unsigned short USHORT
或者在相应头文件中加入

void main(){ 
clrscr(); 
update_cursor(10, 10); 
}
改为
int main(){ 
clrscr(); 
update_cursor(10, 10); 
return 0;
}

热点排行
Bad Request.