default initial value in FORTRAN code


I defined a lot of variables in my fortran code. All the variables are set to zero or very very close to 0 by default after I define it except one variable. That exceptional variable is set to “NAN”. (when I print out the vale of that variable, it is “NAN”). I thought the variables in FORTRAN code are set to 0 by default. Why is this particular variable exceptional?

I used pgf95 to compile my code “pgf95 -c -g -r8 main.f”

I defined a lot of variables in main.f like
double precision ::
& ph_o31d, ph_o33p, ph_no2, ph_no3o2, ph_no3o,
& ph_hno2, ph_hno3, ph_hno4, ph_h2o2, ph_ch2or,
& ph_ch2om, ph_ch3cho, ph_ch3coch3, ph_ch3coc2h5, ph_hcocho,
& ph_ch3cocho, ph_hcochest, ph_ch3o2h, ph_ch3coo2h, ph_ch3ono2,
& ph_hcochob, ph_macr, ph_n2o5, ph_o2, ph_pan,
& ph_acet, ph_mglo, ph_hno4_2, ph_n2o, ph_pooh,
& ph_mpan, ph_mvk, ph_etooh, ph_prooh, ph_onitr,
& ph_acetol, ph_glyald, ph_hyac, ph_mek, ph_open,
& ph_gly, ph_acetp, ph_xooh, ph_isooh, ph_alkooh,
& ph_mekooh, ph_tolooh, ph_terpooh

ph_acetp is set to NAN while the rest are 0

how to make compiler initialize variables to 0 by default?


Dear Xiaoming,

the standard of fortran programming language says the default value of a fortran variable is UNDEFINED, a polite way to say rubbish. You have not to rely on the 0 initialization you often experience. Moreover, when dealing with parallel codes, you will often have a non zero initialization. It is possible to specify an initialization value in the declaration of a variable, e.g.

real :: a = 0.0

but you have to be careful when such an initialization is performed in a procedure because in that case the initialization is performed only the first time you call the procedure and, in the next, the variable assumes the save attribute.

I do not know, and perhaps I would not like compiler automatic initilization to 0.

I hope this is correct and can help you,

bye, Francesco