Skip to content

C/C++ 与 FreeBASIC 对比


C/C++FreeBASIC

变量声明

int a;
int a, b, c;
vb
dim a as long
dim as long a, b, c

未初始化变量

int a;``dim a as long = any

零初始化变量

int a = 0;``dim a as long

已初始化变量

int a = 123;``dim a as long = 123

数组

int a[4];
a[0] = 1;
vb
dim a(0 to 3) as long
a(0) = 1

指针

int a;
int *p;
p = &a
*p = 123;
vb
dim a as long
dim p as long ptr
p = @a
*p = 123

结构体、用户定义类型

struct UDT {
int myfield;

}
vb
type UDT
myfield as long

end type

typedef、类型别名

typedef int myint;``type myint as long

结构体指针

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

函数声明

int foo( void );``declare function foo( ) as long

函数体

int foo( void ) {
return 123;

}
vb
function foo( ) as long
return 123

end function

子程序声明

void foo( void );``declare sub foo( )

子程序体

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

按值传递参数

void foo( int param );
foo( a );
vb
declare sub foo( byval param as long )
foo( a )

按值传递参数的指针

void foo( int *param );
foo( &a );
vb
declare sub foo( byval param as long ptr )
foo( @a )

按引用传递参数

void foo( int& param );
foo( a );
vb
declare sub foo( byref param as long )
foo( a )

语句分隔符

;

:

<行尾>

for 循环

vb
for (int i = 0; i < 10; i++) {
...

}
vb
for i as long = 0 to 9
...

next

while 循环

while (condition) {
...

}
while condition
...

wend

do-while 循环

do {
...

} while (condition);
do
...

loop while condition

if 块

vb
if (condition) {
...

} else if (condition) {
...

} else {
...

}
vb
if condition then
...

elseif condition then
...

else
...

end if

switch、select

switch (a) {
case 1:
...
break;

case 2:
case 3:
...
break;

default:
...
break;

}
vb
select case a
case 1
...

case 2, 3
...

case else
...

end select

字符串字面量、zstring

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

Hello world

#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

返回目录

最后由 MrSwiss 于 2018 年 4 月 6 日审阅。注:在需要的地方将 Integer 更改为 Long

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系