复制C代码(C与FB的差异)(VFB教程3-7)

  勇芳 2018-2-19 9899

此处为VisualFreeBasic编程教程(从零开始学或VB进阶)的子章节部分,全部目录点链接。

高深的,有料的,大多是C语言,但C语言与B语言的语法相差甚远,我们必须基本了解下

就可以无障碍的复制C语言的代码来,自己改B语言使用了。

不过有一点好消息,C语言的数据类型,绝大多少可以直接复制来用,不用修改。

变量声明

C/C++
int a;
int a, b, c;
FreeBASIC
dim a as integer
dim as integer a, b, c

未初始化的变量

int a;
------------------------------------
dim a as integer

零初始化变量

int a = 0;
-------------------------------
dim a as integer

初始化变量

int a = 123;
-------------------------------------
dim a as integer = 123

数组

int a[4];
a[0] = 1;
---------------------------------------
dim a(0 to 3) as integer
a(0) = 1

指针

int a;
int *p;
p = &a;
*p = 123;
-------------------------------------------
dim a as integer
dim p as integer ptr
p=@a
*p = 123

结构,用户定义类型

struct UDT {
int myfield;
}
-------------------------------------
type UDT
     myfield as integer
end type

typedef,别名

typedef int myint;
------------------------------
type myint as integer

结构指针

struct UDT x;
struct UDT *p;
p = &x;
p->myfield = 123;
-------------------------------------
dim x as UDT
dim p as UDT ptr
p = @x
p->myfield = 123

函数声明

int foo( void );
----------------------------------------
function foo( ) as integer

函数体

int foo( void ) {
  return 123;
}
---------------------------------
function foo( ) as integer
    return 123
end function

过程声明

void foo( void );
------------------------------
sub foo( )

过程体

void foo( void ) {
}
----------------------------------
sub foo( )
end sub

Byval参数

void foo( int param );
foo( a );
-------------------------------------------
sub foo( byval param as integer )
foo( a )

byref参数

void foo( int *param );
foo( &a );

void foo( int& param );
foo( a );
---------------------------------
sub foo( byref param as integer )
foo( a )

语句分隔符

;   分号
----------------------------------
:   冒号

for循环

for (int i = 0; i < 10; i++) {
...
}
-----------------------------------
for i as integer = 0 to 9     还可以带声明 I 的,省了 DIM i as integer
...
next

while循环

while (condition) {
...
}
-------------------------------
while condition
...
wend

do-while循环

do {
...
} while (condition);
--------------------------------------
do
...
loop while condition

IF ELSE

if (condition) {
...
} else if (condition) {
...
} else {
...
}
-------------------------------------
if condition then
...
elseif condition then
...
else
...
end if

切换,选择

switch (a) {
case 1:
...
break;
case 2:
case 3:
...
break;
default:
...
break;
}
----------------------------------
select case a
case 1
...


case 2, 3
...

case else
...

end select

字符串文字,字符串

char *s = "Hello!";
char s[] = "Hello!";
-----------------------------------------------
dim s as zstring ptr = @"Hello!"
dim s as zstring * 6+1 = "Hello!"

调试输出

#include <stdio.h>
int main() {
printf("Hello!\n");
return 0;
}
-------------------------------
print "Hello!"

注释

// foo
/* foo */
------------------------------------
' foo
/' foo '/

编译时检查

#if a 
#elif b 
#else
#endif
------------------------------------------
#if a 
#elseif b 
#else
#endif

编译时目标系统检查

#ifdef _WIN32
--------------------------------------------
#ifdef __FB_WIN32__

模块/头文件名

foo.c, foo.h
---------------------------------------------
foo.bas, foo.bi

典型的编译器命令创建可执行文件

gcc foo.c -o foo
-----------------------------------
fbc foo.bas









因国家互联网安全管理要求,关闭回帖功能。大家需要留言,请使用【勇芳软件客服】即时联系勇芳点此打开->>勇芳软件客服
返回
联系勇芳
发新帖 搜索 反馈 回顶部