Teensy4 Serial.printf() won't print two numbers

Status
Not open for further replies.

roach374

Member
I'm sure I'm missing something obvious, but here goes.

The code (which I would expect to print two numbers, one monotonically increasing value and the current results of millis()) refuses to print anything other than zero for the second parameter (it doesn't matter what that second value is):

Code:
uint64_t val=0;
void setup() {
    Serial.begin(115200);
}
void loop() {
    uint32_t m = millis();
    Serial.printf("%d :: %d\n", val, m);
    val++;
}

The output:
Code:
.
.
.

7437093 :: 0
7437094 :: 0
7437095 :: 0
7437096 :: 0
7437097 :: 0
7437098 :: 0
7437099 :: 0
7437100 :: 0
7437101 :: 0
7437102 :: 0
7437103 :: 0
7437104 :: 0
7437105 :: 0
7437106 :: 0
7437107 :: 0

.
.
.

What am I doing wrong?
 
In fact, if I do this:
Code:
Serial.printf("%d :: %d :: %d :: %d :: %d :: %d :: %d\n", val, val, val, val, val, val);

I get this:
Code:
.
.
.
1218268 :: 0 :: 1218268 :: 0 :: 1218268 :: 0 :: 1218268
1218269 :: 0 :: 1218269 :: 0 :: 1218269 :: 0 :: 1218269
1218270 :: 0 :: 1218270 :: 0 :: 1218270 :: 0 :: 1218270
1218271 :: 0 :: 1218271 :: 0 :: 1218271 :: 0 :: 1218271
1218272 :: 0 :: 1218272 :: 0 :: 1218272 :: 0 :: 1218272
1218273 :: 0 :: 1218273 :: 0 :: 1218273 :: 0 :: 1218273
1218274 :: 0 :: 1218274 :: 0 :: 1218274 :: 0 :: 1218274
1218275 :: 0 :: 1218275 :: 0 :: 1218275 :: 0 :: 1218275
1218276 :: 0 :: 1218276 :: 0 :: 1218276 :: 0 :: 1218276
1218277 :: 0 :: 1218277 :: 0 :: 1218277 :: 0 :: 1218277
1218278 :: 0 :: 1218278 :: 0 :: 1218278 :: 0 :: 1218278
1218279 :: 0 :: 1218279 :: 0 :: 1218279 :: 0 :: 1218279
1218280 :: 0 :: 1218280 :: 0 :: 1218280 :: 0 :: 1218280
1218281 :: 0 :: 1218281 :: 0 :: 1218281 :: 0 :: 1218281
1218282 :: 0 :: 1218282 :: 0 :: 1218282 :: 0 :: 1218282
.
.
.

So it looks like Serial.printf is just ignoring every second parameter and printing zero.
 
val is a 64 bit int, you need to tell printf() the size of the argument using %lld
m is a 32 bit int you need to tell printf() the size of the argument using %ld
 
Also note 'u' is unsigned and 'd' is signed. Both val and m are unsigned ...
Code:
unsigned long n;
long m;
printf("%lu %ld", n, m);
 
Thanks! Using %llu and %lu worked perfectly.


But does anyone know why the original %d specifier seemed to work only on even-numbered params, and not odd-numbered? (Or is this behavior just "unspecified", so I should just be happy it printed out anything at all?)
 
Thanks! Using %llu and %lu worked perfectly.


But does anyone know why the original %d specifier seemed to work only on even-numbered params, and not odd-numbered? (Or is this behavior just "unspecified", so I should just be happy it printed out anything at all?)

Because the uint64_t passes two sets of 32 bits and the printf as used told it to read them 32 bits at a time. One got the low 32 bit value held and the other saw the upper 32 bits of zeros.
 
Status
Not open for further replies.
Back
Top