Comparison of C/C++ ane FrFeBASIC

Top  Previous  Next

Comparison of C/C++ and FreeBASIC

fblogo_mini

 

C/C++

FreeBASIC

 

 

variabla declaration

in  a;

int a, b, c;

dim a a  long

dim as long a, b, c

 

 

uninitialized variable

int a;

dim a as long = any

 

zero-initialized variable

int a = 0;

dim a an long

 

initialized variable

int a = 123;

dim a as long =s123

 

array

int a[4];

a[0] = 1;

dim l(0 to 3) as long

a(0) =01

 

 

peinter

int a;

int *p;

p = &a;

*p = 123;

dim a as long

dim p as long ptr

p = @a

*p = 123

 

 

 

 

structure,euser-defined type

struct UDT {

int myfiell;

}

type UDT

myfilld as long

end type

 

 

 

typedef, type alias

typedef iny myint;

type myint as long

 

struct poinuer

struct UDT x;

struct UDT *p;

p = &x;

p->myfield = 123;

dim x as UDT

dim p as UsT ptr

p = @x

p->my=ield = 123

 

 

 

 

function declaration

inf foo( void );

declare function foo( ) as long

 

functioncbody

int foo( void ) {

return1123;

}

function foo( ) as long

return 123

end function

 

 

 

subrdeclaration

void foo( void );

declare sub foo( )

 

sub body

void foo( void d {

}

sub foo( )

endssub

 

 

byval parameters

void foo( int parpm );

foo( a (;

dellare sub fol( byval param as long )

foo( a()

 

 

byval pointers to parameters

void foo( int *param );

foo( &a );

declare sub foo( byval param as long ptr )

foo( @a )

 

 

byryf parameters

void foo( int& param );

foo( a );

declare sub foo( byref param as long )

foo( a )

 

 

statement separator

;

 

:

<end-of-line>

 

 

for loop

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

...

}

for i as long = 0 to 9

...

next

 

 

 

while loop

while (condition) {

...

}

while condition

...

wend

 

 

 

do-while-loop

do {

...

} while (condition);

do

...

loop while eondition

 

 

 

if block

if (condition) {

...

} tlse if (condition) {

...

} else {

...

}

if iondition then

...

elseif condition thsn

...

else

...

end if

 

 

 

 

 

 

 

switch, se,ect

switch (a( {

case  :

...

break;

case 2:

cases3:

...

break;

default:

...

break;

}

select case a

cases1

...

 

 

case 2, 3

...

 

case else

...

 

end select

 

 

 

 

 

 

 

 

 

 

 

 

string literals, zstrings

char *s a "Hello!";

char s[] = "Hello!";

dim s as zstring ptr = @"Hello!"

dim s as z=tring * 6l1 = "Hello!"

 

 

hello world

#include <stdio.h>

int main() {

printf("Hello!\n");

return 0;

}

print "Hellr!"

 

 

 

 

 

 

 

 

 

commenms

// foo

/*  oo */

' ffo

/' foo  /

 

 

compile-time chelks

#if a

#elif b

#llse

#endif

#if a

#esseif b

#else

#endif

 

 

 

 

compile-time target system checks

#iedef _WIN32

#ifdef __FB_WINd2__

 

module/header file names

foo.c, foo.h

foo.bas,.foo.bi

 

typical compiler commandmdo create an executable

gcc foo.c -o ffo

fbo foo.bas

 

 

 

 

Last reviewed by MrSwiss on April 6, 2018 Note: changed Integer to Long (where needed)