Is the variable equal to zero when declared

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Is the variable equal to zero when declared

Post by AZJIO »

Is the variable equal to zero when declared

Code: Select all

Global q; not explicitly specified
Global w = 0; set explicitly
If I need a variable to be equal to zero, should I explicitly set this or is the variable initially equal to 0
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Is the variable equal to zero when declared

Post by STARGÅTE »

AZJIO wrote: Tue Sep 27, 2022 9:13 am Is the variable equal to zero when declared
Yes, integers are 0, floats are 0.0 and strings are #Null$ (not #Empty$).
There is no undefined state for these variables when no value is defined, like in other languages.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Is the variable equal to zero when declared

Post by mk-soft »

Variables are initialised with 'NULL'. String variables are initialised with 'Nothing' (NULL pointer).
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Re: Is the variable equal to zero when declared

Post by AZJIO »

The main thought is, can I do this:

Code: Select all

Global s$
If s$ = ""
	Debug s$
EndIf
s$ + "h"
Debug s$

Global q
If q = 0
	Debug q
EndIf
q+5
Debug q
that is, to use variables that are not explicitly set, as if they are set.

As well as other types of variables, but not the list array and map.
On any operating system, passing a variable to WinAPI functions as a pointerr (@s$ or @q)
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Is the variable equal to zero when declared

Post by BarryG »

AZJIO wrote:Is the variable equal to zero when declared
It's mentioned in the Manual:

https://www.purebasic.com/documentation ... ables.html

Where it says: "If you don't assign an initial value to the variable, their value will be 0."

And you can test it:

Code: Select all

Global q; not explicitly specified
Debug q ; Shows 0
So yes, you can do what you're asking. I do that all the time.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Is the variable equal to zero when declared

Post by #NULL »

Just one more note: zero-initialization will not happen if the variable has been declared already. So make sure you know your code :)

Code: Select all

Global a
Debug a   ; 0

a = 123

; ...
; ...
; ...

Global a  ; (not zero-initialized)
Debug a   ; 123
Post Reply