Comparison of C/C++ and FreeBASIC
 

C/C++
FreeBASIC
变量声明
int a;
int a, b, c;
dim a as integer
dim as integer a, b, c

未初始化的变量
int a;
dim a as integer = any
零初始化变量
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
整数昏暗p I将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 );
declare function foo( ) as integer
功能体
int foo( void ) {
return 123;
}
function foo( ) as integer
return 123
end function

子声明
void foo( void );
declare sub foo( )
子体
void foo( void ) {
}
sub foo( )
end sub

副作用参数
void foo( int param );
foo( a );
declare sub foo( byval param as integer )
foo( a );

byref参数
void foo( int *param );
foo( &a );

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




语句分隔符
;

:
最终的线
for循环
for (int i = 0; i < 10; i++) {
...
}
for i as integer = 0 to 9
...
next

while循环
while (condition) {
...
}
while condition
...
wend

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

如果阻塞
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