Page 1 of 1
Read C variable in PB [Resolved]
Posted: Mon May 31, 2021 11:17 am
by Kwai chang caine
Hello at all
Now i can play with C, thanks several member of the familly
I search to passing variables betwenn C and PB
This simple code works
Code: Select all
OpenConsole()
! printf("My first hello world of C !\n\n");
! char chaine[4];
! chaine[0] = 'K';
! chaine[1] = 'C';
! chaine[2] = 'C';
! chaine[3] = '\0';
! printf("%s\n", chaine);
CloseConsole()
Great compiler C wrote:My first hello world of C !
KCC
But this one not
Code: Select all
OpenConsole()
! printf("My first hello world of C !\n\n");
! char chaine[4];
! chaine[0] = 'K';
! chaine[1] = 'C';
! chaine[2] = 'C';
! chaine[3] = '\0';
! printf("%s\n", chaine);
Debug PeekS(@chaine)
CloseConsole()
How can i catch in PB the value of "chaine" ?
Have a good day
Re: Read C variable in PB
Posted: Mon May 31, 2021 11:34 am
by firace
Hi KCC,
this works for me:
Code: Select all
OpenConsole()
! char v_chaine[] = "KCC";
! printf("%s\n", v_chaine);
Debug PeekS(@chaine, 3, #PB_Ascii)
CloseConsole()
There's probably a better way but I am also a beginner with this stuff

Re: Read C variable in PB
Posted: Mon May 31, 2021 11:40 am
by Kwai chang caine
Waooooouuh too strong FIRACE !!!
Firace wrote: I am also a beginner
It's too funny to read that
I have perhaps a chance to not always be a beginner all my life in PB-C then
Thanks a lot for your precious help

Re: Read C variable in PB
Posted: Mon May 31, 2021 3:49 pm
by Marc56us
firace wrote: Mon May 31, 2021 11:34 am
Code: Select all
OpenConsole()
! char v_chaine[] = "KCC";
! printf("%s\n", v_chaine);
Debug PeekS(@chaine, 3, #PB_Ascii)
CloseConsole()
It works, but I don't understand how
@chaine can link to string
v_chaine 
Re: Read C variable in PB
Posted: Mon May 31, 2021 4:08 pm
by Keya
Marc56us wrote: Mon May 31, 2021 3:49 pm
It works, but I don't understand how
@chaine can link to string
v_chaine
Because internally PB prepends the prefix "v_" to variables before compilation (and "p_" to pointers, etc)
This is explained in the
PB helpfile Inline Assembly section, as it also applies there when working with PB at its lowest level
Use the /COMMENTED compiler flag to see
Re: Read C variable in PB [Resolved]
Posted: Mon May 31, 2021 5:45 pm
by juergenkulow
Code: Select all
; printf under PB_C
CompilerIf #PB_Compiler_ExecutableFormat<>#PB_Compiler_Console
CompilerError "Exec-Format must be Console in Compiler-Option"
CompilerEndIf
Define *string=UTF8("Hello visible universe. ")
Define *string2=UTF8("Value is now: %lld ")
Define *string3=UTF8("CPU-Time: %lld ")
Define value.q=42
OpenConsole()
! printf(p_string);
! printf(p_string2,++v_value);
! long long myrdtsc(void) { asm volatile ("rdtsc\n\t" "shl $32,%rdx\n\t" "or %rdx ,%rax"); }
! printf(p_string3,myrdtsc());
Input()
CloseConsole()
FreeMemory(*string)
FreeMemory(*string2)
FreeMemory(*string3)
; Hello visible universe. Value is now: 43 CPU-Time: 3181639470065
Delete: CompilerIf #PB_Processor_C<>#PB_Compiler_Processor ...
Re: Read C variable in PB [Resolved]
Posted: Mon May 31, 2021 5:56 pm
by Kwai chang caine
Juergenkulow apparently you are a C master
All your codes is always splendids...but always also ununderstandable for my little head

Re: Read C variable in PB [Resolved]
Posted: Tue Jun 01, 2021 8:21 pm
by Olli
It stays a_MyArray and p.v_myVariableInTheProcedure to be tested, and you will have done the complete tour of the links between PB and C !
Re: Read C variable in PB [Resolved]
Posted: Tue Jun 01, 2021 8:29 pm
by Marc56us
Now how to do the opposite: access from C to a variable defined by PB?
Code: Select all
A$ = "Hello World"
! printf("%s", A$ ??? );
Input()
PokeS ? strcpy(v_A[], A$ ?) ?
(My goal is to try to use the
sscanf,
sprintf etc functions afterwards)

Re: Read C variable in PB [Resolved]
Posted: Wed Jun 02, 2021 2:32 am
by juergenkulow
Code: Select all
; PB_C sscanf
EnableExplicit
Define *a_utf8string=UTF8("4711471274713")
Define value.q
! extern int sscanf(const char *s, const char *template,...);
; First find declaration of sscanf.
! sscanf(p_a_utf8string, "%lld" , &v_value);
; C Scan String function with the parameters
; PureBasic Pointer to a UTF8-String *a_utf8string, p_a_utf8string
; Scanformat for quad, 8 Byte number in c long long %lld
; Adress of PureBasic value, &v_value
SetClipboardText("Value is: "+Str(value))
MessageRequester("Value",Str(value))
FreeMemory(*A_UTF8String)
; Value is: 4711471274713
Please ask your questions.
Delete: CompilerIf #PB_Processor_C<>#PB_Compiler_Processor
Re: Read C variable in PB [Resolved]
Posted: Wed Jun 02, 2021 6:06 am
by Marc56us
Thank you juergenkulow for this quick answer with the explanations in the comments.
This new version of PB will definitely bring us many new possibilities.

Re: Read C variable in PB [Resolved]
Posted: Wed Jun 02, 2021 4:21 pm
by Kwai chang caine
Re: Read C variable in PB [Resolved]
Posted: Fri Jun 04, 2021 2:35 pm
by Olli
Juergenkulow wrote:! long long myrdtsc(void) { asm volatile ("rdtsc\n\t" "shl $32,%rdx\n\t" "or %rdx ,%rax"); }
I do not find the words to describe how it is beautiful...