Where I can see examples FreeBasic made with the library C

General FreeBASIC programming questions.
lrcvs
Posts: 578
Joined: Mar 06, 2008 19:27
Location: Spain

Where I can see examples FreeBasic made with the library C

Post by lrcvs »

Hi:

Where I can see examples FreeBasic made with the library
C Standard Library Functions ?

Thanks, regards.
caseih
Posts: 2158
Joined: Feb 26, 2007 5:32

Re: Where I can see examples FreeBasic made with the library

Post by caseih »

Sure:

Code: Select all

#include once "crt/stdio.bi"

dim as long i = 5
dim as string a = "Hello, World!"

printf(!"%s\n%d\n",a,i)
I discovered today that prefixing a string with ! means that the compiler should process escape sequences in the string, which is pretty much required for using printf.
lrcvs
Posts: 578
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Where I can see examples FreeBasic made with the library

Post by lrcvs »

HI, caseih:

Thank you very much!

Personally I do not like any "C" (C, C ++, C # ...)
... But my daughter is started studying programming next year and will have to do with "C" ...
... Try to learn (with books) and translate a program in FB libraries in "C" to "C".
... And then I explain it all in "C".

L O L. :
"... Learn to old age" C "... life is very hard ..."

Regards
caseih
Posts: 2158
Joined: Feb 26, 2007 5:32

Re: Where I can see examples FreeBasic made with the library

Post by caseih »

For most of the C standard library, the equivalent .h files are in freebasic's crt/*.bi include files.

For anything that's not already translated into .bi files, you'd have to recreate all the C structs in FB UDTs and make sub or function declarations for the C routines. I used to know of a chart that allowed you to convert between FB types and C types, but I can't find it. Can someone fill in this information? While writing the example program, I remembered that a C "int" which is what "%d" expects is not the same as FB's Integer type on 64-bit systems on Linux.

Except for dynamic strings and arrays, FB is pretty much 1:1 equivalent of C as far as translation goes. C's use of char arrays has always been difficult for the beginner (and the source of many, many security buffer-overruns). But in general, C isn't really that hard to read.

Here's a simple example of a C program and calling it from FB:

Code: Select all

#include <stdio.h>

struct _mystruct {
        int a;
        char b[256];
};

//Make a nice typedef shortcut
typedef struct _mystruct MyStruct;

int my_adder(int a, int b) {
        return a+b;
}

void my_sub(MyStruct *foo) {
        printf("My Struct: %d,%s\n",foo->a,foo->b);
}

Code: Select all

'Make a UDT that matches the one in the C program
type MyStruct
    a as long
    b as zstring*256
end type

'Set up FB declarations for C routines
declare function my_adder cdecl alias "my_adder" (a as long, b as long) as long 
declare sub my_sub cdecl alias "my_sub" (foo as MyStruct ptr)

'Main program
dim c as MyStruct

c.a = 5
c.b = "Hi there"

print "Calling my_adder: "; my_adder(4,6)

print "Calling my_sub:"
my_sub(@c)
You can compile the .c file to an object file using gcc (mingw on Windows). Then compile the basic program like this, assuming the c file is compiled to mylib.o:

Code: Select all

fbc myfile.bas mylib.o
Of course you can link to existing libraries as described in the wiki.
Last edited by caseih on May 01, 2016 22:13, edited 2 times in total.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Where I can see examples FreeBasic made with the library

Post by dkl »

grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Where I can see examples FreeBasic made with the library

Post by grindstone »

caseih wrote:I used to know of a chart that allowed you to convert between FB types and C types, but I can't find it.
Maybe you're talking about swig

Regards
grindstone
caseih
Posts: 2158
Joined: Feb 26, 2007 5:32

Re: Where I can see examples FreeBasic made with the library

Post by caseih »

dkl's link was the one I was thinking of, but I'm sure the swig information is also useful.
lrcvs
Posts: 578
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Where I can see examples FreeBasic made with the library

Post by lrcvs »

Hi, dkl, caseih, grindstone:

Thank you very much to all.!

" FB is pretty much 1:1 equivalent of C +-, as far as translation goes"

From what I've seen in books, it is relatively simple, I find a little difficulty in the statements of the type of variables.

Try to make a table of equivalence between FB and C, to facilitate equivalence and study.

Regards
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Where I can see examples FreeBasic made with the library

Post by MichaelW »

This is a quick translation of the example code from here:

Code: Select all

#include "crt.bi"

dim as double x = 2.387
dim as long n = 3, c

printf( !"Bessel functions for x = %f:\n", x )
printf( !" Kind   Order  Function     Result\n\n" )
printf( !" First  0      _j0( x )     %f\n", _j0( x ) )
printf( !" First  1      _j1( x )     %f\n", _j1( x ) )
for c = 2 to 4
    printf( !" First  %d      _jn( %d, x )  %f\n", c, c, _jn( c, x ) )
next    
printf( !" Second 0      _y0( x )     %f\n", _y0( x ) )
printf( !" Second 1      _y1( x )     %f\n", _y1( x ) )
for c = 2 to 4
    printf( !" Second %d      _yn( %d, x )  %f\n", c, c, _yn( c, x ) )
next

sleep

Code: Select all

Bessel functions for x = 2.387000:
 Kind   Order  Function     Result

 First  0      _j0( x )     0.009288
 First  1      _j1( x )     0.522941
 First  2      _jn( 2, x )  0.428870
 First  3      _jn( 3, x )  0.195734
 First  4      _jn( 4, x )  0.063131
 Second 0      _y0( x )     0.511681
 Second 1      _y1( x )     0.094374
 Second 2      _yn( 2, x )  -0.432608
 Second 3      _yn( 3, x )  -0.819314
 Second 4      _yn( 4, x )  -1.626833
caseih
Posts: 2158
Joined: Feb 26, 2007 5:32

Re: Where I can see examples FreeBasic made with the library

Post by caseih »

If you have a small C program you need help translating, post it here.

Seems to me that almost all of the C standard library calls in the average C program probably have native FB analogs. For example, there's really no reason to use printf as print will do the same job. Same with things like scanf, which for simple things you can just use "input." And dynamic strings make it so you probably don't ever need to use snprintf. Instead of using C file i/o calls, you can use FB statements.

With crt.bi you could probably make an exact transliteration of a C program into FB and it would look virtually identical and have very little practical benefit, in fact generated C code would be almost the same as the original! So I suggest as you go doing your translations that you not bother with crt.bi. Instead come up with equivalent native FB code.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Where I can see examples FreeBasic made with the library

Post by MichaelW »

caseih wrote: For example, there's really no reason to use printf as print will do the same job.
The last time I checked Print Using had a substantial number of problems that printf does not have. And there are a number of other useful CRT functions for which there is no equivalent native FreeBASIC function, and while you can create FreeBASIC equivalents, getting them to the same level of reliability as the CRT code will take substantial time.
lrcvs
Posts: 578
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Where I can see examples FreeBasic made with the library

Post by lrcvs »

Hi !

I found this page on the net. (This in Spanish !!!)


http://www.elguille.info/NET/dotnet/equ ... avbcs1.htm


Simple tables of equivalence between VB.net and C #.

They are very similar to FB and C.
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Where I can see examples FreeBasic made with the library

Post by marcov »

MichaelW wrote:
caseih wrote: For example, there's really no reason to use printf as print will do the same job.
The last time I checked Print Using had a substantial number of problems that printf does not have. And there are a number of other useful CRT functions for which there is no equivalent native FreeBASIC function, and while you can create FreeBASIC equivalents, getting them to the same level of reliability as the CRT code will take substantial time.
If you reason that way, I wonder why you bother with FB at all, and not go to the C/C++ directly.
lrcvs
Posts: 578
Joined: Mar 06, 2008 19:27
Location: Spain

Re: Where I can see examples FreeBasic made with the library

Post by lrcvs »

Hi, marcov:

Thank you very much for your comment.

If the question is for me (lrcvs), I know nothing of "C", I've never worked with "C", I have only worked with Basic, TPascal 7 and Logo.

Now I see how it works "C", sometimes with FB and other directly with "C".

I have to teach my daughter programming in "C".

Greetings
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Where I can see examples FreeBasic made with the library

Post by dodicat »

Ircvs
I think that C is well standardized.
For instance (IF IN WINDOWS) MichaelW's Bessel functions are fetched from msvcrt.dll (#include "crt.bi"), which is Microsoft's Visual C
Linux must have a similar set up but not Microsoft of course.

The include is math.h for these functions.
I have looked around at various math.h for various C compilers, they all seem to have these Bessel routines.

Probably C is a good choice for your Daughter.
This means perhaps that you will be learning it together?
Good Luck!
Here is the C built in sort accessed via a couple of macros:

Code: Select all


'========================= sort setup =====================================
#include "crt.bi"
#define up <,>
#define down >,<
#define values(x) @X(Lbound(X)),(Ubound(X)-Lbound(X)+1),Sizeof(X)
#macro SetSort(Datatype,FnName,b1,b2,dot)
Function FnName Cdecl(n1 As Any Ptr,n2 As Any Ptr) As long
    If *Cptr(Datatype Ptr,n1)dot b1 *Cptr(Datatype Ptr,n2)dot Then Return -1
    If *Cptr(DataType Ptr,n1)dot b2 *Cptr(DataType Ptr,n2)dot Then Return 1
End Function
#endmacro
'======================================================================


dim as double a(1 to 50)
'setsort.
'parameter 1 -- datatype
'parameter 2 -- a unique name
'parameter 3 -- direction
'parameter 4 -- nothing if the array is not a UDT.

SetSort(double,Fndouble,down,)

for n as integer=1 to 50
    a(n)=rnd*1000
    next n

'qsort(@a(Lbound(a)),(Ubound(a)-Lbound(a)+1),Sizeof(a),@Fndouble ) 'standard form

'Simplified form (use values())
qsort(values(a),@Fndouble) 
print "The doubles sorted down"
for n as integer=1 to 50
   print n, a(n)
next n
print
'------------------- UDT example -------------
type vector
    as single x,y,z
end type

sub printout(v() as vector,flag as string)
    print "sorted ";flag
    print"index",".x",".y",".z"
for n as integer=lbound(v) to ubound(v)
    print n,v(n).x,v(n).y,v(n).z
next n
print
end sub


dim as vector v(-10 to 10)
'setsort.
'parameter 1 -- datatype
'parameter 2 -- a unique name
'parameter 3 -- direction
'parameter 4 -- field to sort

SetSort(vector,FnvectorZ,up,.z)
SetSort(vector,FnvectorY,down,.y)
SetSort(vector,FnvectorX,up,.x)

for n as integer=-10 to 10
    v(n)=type<vector>(rnd*50,rnd*50,rnd*50)
next n

qsort(values(v),@FnvectorZ ):printout(v(),".Z up")
qsort(values(v),@FnvectorY ):printout(v(),".Y down")
qsort(values(v),@FnvectorX ):printout(v(),".X up")
sleep


  
Post Reply