Shop OBEX P1 Docs P2 Docs Learn Events
C: simple print line seems to be changed by later command — Parallax Forums

C: simple print line seems to be changed by later command

John KauffmanJohn Kauffman Posts: 653
edited 2015-03-21 19:21 in Propeller 1
I can't figure out why a print() result is different when it has/not a following command. Nothing else in the programs but frame of include, main, etc.

/*
Blank Simple Project.c

// !!! run either program #1 or program #2. REM out other.

*/
#include "simpletools.h" // Include simple tools


int main() // Main function
{

// Program #1
int a = 1, b = 2;
print("a = %d, b = %d\n", a); // error missing arg for second %d
/* display result:
a = 1, b = 1960 // as expected for the error: 1960
*/

/*
// Program #2
int a = 1, b = 2;
print("a = %d, b = %d\n", a); // error missing arg
print("a = %d, b = %d\n", a, b); // no error
/* display result:
a = 1, b = 0 // !!! why not 1960 like in program #1?
a = 1, b = 2
*/
}

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2015-03-21 18:17
    print("a = %d, b = %d\n", a) will produce unpredictable results. The value printed for b will be whatever happens to be on the stack after the value for a.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2015-03-21 19:21
    Got it, thanks Dave.
Sign In or Register to comment.