此處為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