Read C variable in PB [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Read C variable in PB [Resolved]

Post by Kwai chang caine »

Hello at all :D

Now i can play with C, thanks several member of the familly 8)
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 :oops:

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
Last edited by Kwai chang caine on Mon May 31, 2021 12:06 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
firace
Addict
Addict
Posts: 902
Joined: Wed Nov 09, 2011 8:58 am

Re: Read C variable in PB

Post 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 :)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Read C variable in PB

Post by Kwai chang caine »

Waooooouuh too strong FIRACE !!! :shock:
Firace wrote: I am also a beginner
It's too funny to read that :lol:
I have perhaps a chance to not always be a beginner all my life in PB-C then :mrgreen:

Thanks a lot for your precious help 8)
ImageThe happiness is a road...
Not a destination
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Read C variable in PB

Post 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 :?:
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Read C variable in PB

Post 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
juergenkulow
Enthusiast
Enthusiast
Posts: 556
Joined: Wed Sep 25, 2019 10:18 am

Re: Read C variable in PB [Resolved]

Post 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 ...
Last edited by juergenkulow on Sun Jun 20, 2021 10:15 am, edited 1 time in total.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Read C variable in PB [Resolved]

Post by Kwai chang caine »

Juergenkulow apparently you are a C master :shock: 8)
All your codes is always splendids...but always also ununderstandable for my little head :oops:
ImageThe happiness is a road...
Not a destination
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Read C variable in PB [Resolved]

Post 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 !
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Read C variable in PB [Resolved]

Post 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)
:wink:
juergenkulow
Enthusiast
Enthusiast
Posts: 556
Joined: Wed Sep 25, 2019 10:18 am

Re: Read C variable in PB [Resolved]

Post 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
Last edited by juergenkulow on Sun Jun 20, 2021 10:16 am, edited 1 time in total.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Read C variable in PB [Resolved]

Post by Marc56us »

Thank you juergenkulow for this quick answer with the explanations in the comments. :D

This new version of PB will definitely bring us many new possibilities.
:wink:
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Read C variable in PB [Resolved]

Post by Kwai chang caine »

Marco_MonPoto wrote:Now how to do the opposite: access from C to a variable defined by PB?
Image

"Cé ane baune késtion !!!! é j'vous r'merci d'm'l'avoir posée !!!" :mrgreen: 8)

I see i'm not alone to play with the new wonderfull backend :wink: :lol:
Marco_MonPoto wrote:This new version of PB will definitely bring us many new possibilities.
I even believe that you are below the truth, for me ....it's really a daydream, i still can't believe it again :shock: 8)

@juergenkulow
My empire for 1% of your knowledge :shock:

Thanks a lot for completing our collection of communication instructions, between PB and C 8)
You know ....in a new couple .... it's the "good" communication the most important :lol:

Image
ImageThe happiness is a road...
Not a destination
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Read C variable in PB [Resolved]

Post 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...
Post Reply