Comparison of C/C++ and FreeBASIC
- Source: https://www.freebasic.net/wiki/wikka.php?wakka=TblComparisonC
- Last revised: 2020-08-22
C/C++FreeBASIC
variable declaration
int a;
int a, b, c;vb
dim a as long
dim as long a, b, cuninitialized variable
int a;``dim a as long = any
zero-initialized variable
int a = 0;``dim a as long
initialized variable
int a = 123;``dim a as long = 123
array
int a[4];
a[0] = 1;vb
dim a(0 to 3) as long
a(0) = 1pointer
int a;
int *p;
p = &a
*p = 123;vb
dim a as long
dim p as long ptr
p = @a
*p = 123structure, user-defined type
struct UDT {
int myfield;
}vb
type UDT
myfield as long
end typetypedef, type alias
typedef int myint;``type myint as long
struct pointer
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 = 123function declaration
int foo( void );``declare function foo( ) as long
function body
int foo( void ) {
return 123;
}vb
function foo( ) as long
return 123
end functionsub declaration
void foo( void );``declare sub foo( )
sub body
void foo( void ) {
}vb
sub foo( )
end subbyval parameters
void foo( int param );
foo( a );vb
declare sub foo( byval param as long )
foo( a )byval pointers to parameters
void foo( int *param );
foo( &a );vb
declare sub foo( byval param as long ptr )
foo( @a )byref parameters
void foo( int& param );
foo( a );vb
declare sub foo( byref param as long )
foo( a )statement separator
;
:
<end-of-line>
for loop
vb
for (int i = 0; i < 10; i++) {
...
}vb
for i as long = 0 to 9
...
nextwhile loop
while (condition) {
...
}while condition
...
wenddo-while loop
do {
...
} while (condition);do
...
loop while conditionif block
vb
if (condition) {
...
} else if (condition) {
...
} else {
...
}vb
if condition then
...
elseif condition then
...
else
...
end ifswitch, 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 selectstring literals, zstrings
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!"
comments
// foo
/* foo */' foo
/' foo '/compile-time checks
#if a
#elif b
#else
#endif``#if a
#elseif b
#else
#endif
compile-time target system checks
#ifdef _WIN32``#ifdef __FB_WIN32__
module/header file names
foo.c, foo.h``foo.bas, foo.bi
typical compiler command to create an executable
gcc foo.c -o foo``fbc foo.bas
Back to Table of Contents
Last reviewed by MrSwiss on April 6, 2018 Note: changed Integer to Long (where needed)