[opensuse] C help - compile errors on simple function - need help
Guys, I have a function I just added to a little program the does some atmospheric calculation and it is giving me errors that variables I have just defined in the function are unused. My guess is that I'm screwing up the last 'if' statement because it isn't seeing the P (pressure) calculation. I know this is probably very basic and I'm violating some rule, but this is a case where I could stare at it and google for hours over something simple, so I thought I would ask for help curing my CRI. The build line and the errors are: 19:08 alchemy:~/dev/prg/ccpp/src-c/prj/test> gcc -o atm -Wall -lm -std=c99 atmtable.c atmtable.c: In function ‘fn_atmPress’: atmtable.c:173: warning: unused variable ‘M’ atmtable.c:172: warning: unused variable ‘g0’ atmtable.c:171: warning: unused variable ‘R’ atmtable.c:170: warning: unused variable ‘hb’ atmtable.c:169: warning: unused variable ‘h’ atmtable.c:168: warning: unused variable ‘Lb’ atmtable.c:167: warning: unused variable ‘Tb’ atmtable.c:166: warning: unused variable ‘Pb’ atmtable.c:182: warning: unused variable ‘M’ atmtable.c:181: warning: unused variable ‘g0’ atmtable.c:180: warning: unused variable ‘R’ atmtable.c:179: warning: unused variable ‘hb’ atmtable.c:178: warning: unused variable ‘h’ atmtable.c:177: warning: unused variable ‘Lb’ atmtable.c:176: warning: unused variable ‘Tb’ atmtable.c:175: warning: unused variable ‘Pb’ The function declaration is: double fn_atmPress(double arry[], int SIflag) { /* Comment Block SIflag=1 for Metric, anything else for English arry [hb, h, Pb, Tb, Lb] Metric units: Pb = Static pressure (pascals) Tb = Standard temperature (K) Lb = Standard temperature lapse rate -0.0065 (K/m) in ISA h = Height above sea level (meters) hb = Height at bottom of layer b (meters; e.g., h1 = 11,000 meters) R = Universal gas constant for air: 8.31432 N·m /(mol·K) g0 = Gravitational acceleration (9.80665 m/s2) M = Molar mass of Earth's air (0.0289644 kg/mol) English units: Pb = Static pressure (inches of mercury, inHg) Tb = Standard temperature (K) Lb = Standard temperature lapse rate (K/ft) h = Height above sea level (ft) hb = Height at bottom of layer b (feet; e.g., h1 = 36,089 ft) R = Universal gas constant; using feet, kelvins, and (SI) moles: 8.9494596×10-4 lb·ft2/(lbmol·K·s2) g0 = Gravitational acceleration (32.17405 ft/s2) M = Molar mass of Earth's air (28.9644 lb/mol) */ // Initialize all variable to 0.0 double P = 0.0; double Pb = 0.0; double Tb = 0.0; double Lb = 0.0; double h = 0.0; double hb = 0.0; double R = 0.0; double g0 = 0.0; double M = 0.0; // Test SIflag and set variables for Metric or English units if (SIflag == 1) { double Pb = arry[3]; double Tb = arry[4]; double Lb = arry[5]; double h = arry[2]; double hb = arry[1]; double R = 8.31432; double g0 = 9.80665; double M = 0.0289644; } else { double Pb = arry[3]; double Tb = arry[4]; double Lb = arry[5]; double h = arry[2]; double hb = arry[1]; double R = 8.9494596e-4; double g0 = 32.17405; double M = 28.9644; } // testing output printf("\nUsing the following values for pressure calc:\n\n"); printf(" Pb = %13.6f\n", Pb); printf(" Tb = %13.6f\n", Tb); printf(" Lb = %13.6f\n", Lb); printf(" h = %13.6f\n", h); printf(" hb = %13.6f\n", hb); printf(" R = %13.6f\n", R); printf(" g0 = %13.6f\n", g0); printf(" M = %13.6f\n", M); // Test Lapse Rate and perform pressure calculation accordingly if (Lb == 0.0) P = Pb * exp( (-g0*M*(h-hb))/(R*Tb) ); else P = Pb * pow( Tb/(Tb+Lb*(h-hb)), (g0*M)/(R*Lb) ); return P; } I have also tried with - if ( islessgreater(Lb,0.0)) but I get the same errors. The == operator shouldn't throw an error with NaN anyway. See: http://www.gnu.org/s/libc/manual/html_node/FP-Comparison-Functions.html P.S. CRI = cranal rectal inversion :p -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Guys,
I have a function I just added to a little program the does some atmospheric calculation and it is giving me errors that variables I have just defined in the function are unused. My guess is that I'm screwing up the last 'if' statement because it isn't seeing the P (pressure) calculation.
I know this is probably very basic and I'm violating some rule, but this is a case where I could stare at it and google for hours over something simple, so I thought I would ask for help curing my CRI.
The build line and the errors are:
Note that these are compiler *warnings*, not errors. But, they are pointing to a real error in your function. Methinks you would do well to pick up a book on C programming or a do a little more research and reading on-line. That being said....
19:08 alchemy:~/dev/prg/ccpp/src-c/prj/test> gcc -o atm -Wall -lm -std=c99 atmtable.c atmtable.c: In function ‘fn_atmPress’: atmtable.c:173: warning: unused variable ‘M’ atmtable.c:172: warning: unused variable ‘g0’ */
// Initialize all variable to 0.0 double P = 0.0; double Pb = 0.0; double Tb = 0.0; double Lb = 0.0; double h = 0.0; double hb = 0.0; double R = 0.0; double g0 = 0.0; double M = 0.0;
Here, you declare function local variables, including M, g0, etc. Note: function local variables have scope for the entire body of the function. If you looked at the line numbers of the warnings, I'm sure that you'd see that they are the lines numbers of the below declarations, not those above.
// Test SIflag and set variables for Metric or English units if (SIflag == 1) { double Pb = arry[3]; double Tb = arry[4]; double Lb = arry[5]; double h = arry[2]; double hb = arry[1]; double R = 8.31432; double g0 = 9.80665; double M = 0.0289644; } else { double Pb = arry[3]; double Tb = arry[4]; double Lb = arry[5]; double h = arry[2]; double hb = arry[1]; double R = 8.9494596e-4; double g0 = 32.17405; double M = 28.9644; }
Here, however, you declare a *new* set of variables with a more limited scope *that happen to have the same names*. The scope of these variables is only within the enclosing braces "{" and "}". Thus, after the final "}", your newly scoped variables don't exist. From the compiler's point of view, your code created some new variables, initialized them, and then never used them. Hense the warnings. The "double Pb = arry[3];" declares the new variable; which is most certainly wrong. Incidentally, it looks like you're mixing tabs and spaces in your code, which could make edits more difficult in the future. How to solve the real problem? Easy, don't declare the new set of variables, just use the ones that you declared at the top of the function (which is I'm sure what you actually intended to do). The above block should look more like this: if ( SIflag == 1 ) { Pb = arry[3]; ... } else { Pb = arry[3]; ... } Even better, you should recognize that the initializations of Pb, Tb, etc. are all shared, so cleaner code would look like this: Pb = arry[3]; ... if ( SIflag == 1 ) { R = 8.31432; ... } else { R = 8.9494596e-4; ... } FYIW, you may want to look into other programming languages that are easier to learn and don't have as many "gotchas". I'm personally a fan of Python. Hope this helps -Nick -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 09/22/2010 08:24 PM, Nick LeRoy wrote:
Here, however, you declare a *new* set of variables with a more limited scope *that happen to have the same names*. The scope of these variables is only within the enclosing braces "{" and "}". Thus, after the final "}", your newly scoped variables don't exist. From the compiler's point of view, your code created some new variables, initialized them, and then never used them. Hense the warnings. The "double Pb = arry[3];" declares the new variable; which is most certainly wrong.
(smacks self) Thank you! -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (2)
-
David C. Rankin
-
Nick LeRoy