wprintf and operator precedence issues with SH

bridged with qdn.public.qnxrtp.sh4
Post Reply
Sudarshan Rao

wprintf and operator precedence issues with SH

Post by Sudarshan Rao » Thu May 08, 2003 6:14 pm

#1 wscanf does not read long values correctly

#2 operator precedence or a compiler bug for operatuions like

temp |= value[pos + 1] << 16;

The following code can run on QNX/Sh and also windows.

#include <stdio.h>
#include <wchar.h>
#include <stdint.h>
#include <vector>

int main() {
char buff[256];
unsigned long k = 4207677439ul, j;
sprintf(buff, "%lu", k);
puts(buff);
sscanf(buff, "%lu", &j);
printf("J = %lu\n\n", j);
j=3;

wchar_t Buff[256];
wchar_t Fmt[] = { '%', 'l', 'u', '\0' };

swprintf(Buff, 256, Fmt, k);
printf("%ls\n", Buff);
swscanf(Buff, Fmt, &j);
printf("J = %lu\n", j);

std::vector<uint8_t> value;
value.push_back(0x52);
value.push_back(0x42);
value.push_back(0x52);
value.push_back(0x32);
uint32_t temp = 0, m_value;
unsigned pos = 0;

temp |= value[pos + 3];
temp |= value[pos + 2] << 8;
temp |= value[pos + 1] << 16;
temp |= value[pos] << 24;
printf("\nm_value is %lu\n", m_value);

temp = 0;
temp |= (value[pos + 3]);
temp |= (value[pos + 2] << 8);
temp |= (value[pos + 1] << 16);
temp |= (value[pos] << 24);
m_value = temp;
printf("\nm_value is %lu\n", m_value);
}

Result:


4207677439
J = 4207677439

4207677439
J = 4294967295

m_value is 1882540172

m_value is 1380078130

Guest

Re: wprintf and operator precedence issues with SH

Post by Guest » Fri May 09, 2003 5:43 pm

surao@hotmail.com sed in <b9e5l4$frg$1@inn.qnx.com>:
uint32_t temp = 0, m_value;
unsigned pos = 0;

temp |= value[pos + 3];
temp |= value[pos + 2] << 8;
temp |= value[pos + 1] << 16;
temp |= value[pos] << 24;
printf("\nm_value is %lu\n", m_value);
m_value isn't initialised, so the value can be anything.

--
kabe

Post Reply

Return to “qdn.public.qnxrtp.sh4”