QCC/g++/QNX won't printf/sprintf string correctly with accen

Hello!

Sample program below:

===
#include <stdio.h>
#include <iostream.h>


int main(void)
{
double big = 2.3495939393949595949;

printf(“le gros numéro est %g et l’inverse est %g\n”,big, 1/big);
printf(“the big number is %g and the inverse is %g\n”,big, 1/big);

printf(“petit numéro: %d\n”,1);
printf(“small number: %d\n”,1);

}

(hopefully, your mail reader is able to read french accented
characters…)

–(incorrect) Output under QNX6.1A:

$ g++ output.C -o output
$ ./output
le gros numéro est %g et l’inverse est %g
the big number is 2.34959 and the inverse is 0.425605
petit numéro: %d
small number: 1
$ qcc output.C -o output
$ ./output
le gros numéro est %g et l’inverse est %g
the big number is 2.34959 and the inverse is 0.425605
petit numéro: %d
small number: 1

–(correct) Output under Linux RH7.3:

[rngadam@sonia rngadam]$ g++ output.C -o output
[rngadam@sonia rngadam]$ ./output
le gros numéro est 2.34959 et l’inverse est 0.425605
the big number is 2.34959 and the inverse is 0.425605
petit numéro: 1
small number: 1

–Is there a solution to this?

Thanks,


Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Salut Ricky.

Try compiling with -w8. I’d bet it will complain type don’t match.

big is a double but I thing %g expect a float hence the problem. You’d
have to specify %lg I beleive for double.

Also make sure you link with math lib (-lm)


“Ricky Ng-Adam” <ng-adam.ricky@ireq.ca> wrote in message
news:3D10ED1B.268374D@ireq.ca

Hello!

Sample program below:

===
#include <stdio.h
#include <iostream.h


int main(void)
{
double big = 2.3495939393949595949;

printf(“le gros numéro est %g et l’inverse est %g\n”,big, 1/big);
printf(“the big number is %g and the inverse is %g\n”,big, 1/big);

printf(“petit numéro: %d\n”,1);
printf(“small number: %d\n”,1);

}

(hopefully, your mail reader is able to read french accented
characters…)

–(incorrect) Output under QNX6.1A:

$ g++ output.C -o output
$ ./output
le gros numéro est %g et l’inverse est %g
the big number is 2.34959 and the inverse is 0.425605
petit numéro: %d
small number: 1
$ qcc output.C -o output
$ ./output
le gros numéro est %g et l’inverse est %g
the big number is 2.34959 and the inverse is 0.425605
petit numéro: %d
small number: 1

–(correct) Output under Linux RH7.3:

[rngadam@sonia rngadam]$ g++ output.C -o output
[rngadam@sonia rngadam]$ ./output
le gros numéro est 2.34959 et l’inverse est 0.425605
the big number is 2.34959 and the inverse is 0.425605
petit numéro: 1
small number: 1

–Is there a solution to this?

Thanks,


Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Try escaping the ’ with a backslash, i.e.
printf(“le gros numéro est %g et l’inverse est %g\n”,big, 1/big);

“Ricky Ng-Adam” <ng-adam.ricky@ireq.ca> wrote in message
news:3D10ED1B.268374D@ireq.ca

Hello!

Sample program below:

===
#include <stdio.h
#include <iostream.h


int main(void)
{
double big = 2.3495939393949595949;

printf(“le gros numéro est %g et l’inverse est %g\n”,big, 1/big);
printf(“the big number is %g and the inverse is %g\n”,big, 1/big);

printf(“petit numéro: %d\n”,1);
printf(“small number: %d\n”,1);

}

(hopefully, your mail reader is able to read french accented
characters…)

–(incorrect) Output under QNX6.1A:

$ g++ output.C -o output
$ ./output
le gros numéro est %g et l’inverse est %g
the big number is 2.34959 and the inverse is 0.425605
petit numéro: %d
small number: 1
$ qcc output.C -o output
$ ./output
le gros numéro est %g et l’inverse est %g
the big number is 2.34959 and the inverse is 0.425605
petit numéro: %d
small number: 1

–(correct) Output under Linux RH7.3:

[rngadam@sonia rngadam]$ g++ output.C -o output
[rngadam@sonia rngadam]$ ./output
le gros numéro est 2.34959 et l’inverse est 0.425605
the big number is 2.34959 and the inverse is 0.425605
petit numéro: 1
small number: 1

–Is there a solution to this?

Thanks,


Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Hello Mario!

For, thanks for taking the time to reply!

Mario Charest wrote:

Try compiling with -w8. I’d bet it will complain type don’t match.

===
include <stdio.h>

int main(void)
{
double big = 2.3495939393949595949;

printf(“le gros numéro est %g et l’inverse est %g\n”,big, 1/big);
printf(“the big number is %g and the inverse is %g\n”,big, 1/big);

printf(“petit numéro: %d\n”,1);
printf(“small number: %d\n”,1);

}

$ qcc -w8 output.C -o output —> the option doesn’t exist for g++
$ ./output
le gros numéro est %g et l’inverse est %g
the big number is 2.34959 and the inverse is 0.425605
petit numéro: %d
small number: 1

big is a double but I thing %g expect a float hence the problem. You’d
have to specify %lg I beleive for double.

Notice that the problem is also there for the integer (%d)!!!

and…

GNU printf man page (man 3 printf)

g,G The double argument is converted in style f or e
(or F or E for G conversions). The precision spec­
ifies the number of significant digits. If the
precision is missing, 6 digits are given; if the
precision is zero, it is treated as 1. Style e is
used if the exponent from its conversion is less
than -4 or greater than or equal to the precision.
Trailing zeros are removed from the fractional part
of the result; a decimal point appears only if it
is followed by at least one digit.

Thanks,

Hello!

“Bill Caroselli (Q-TPS)” wrote:

Try escaping the ’ with a backslash, i.e.
printf(“le gros numéro est %g et l’inverse est %g\n”,big, 1/big);

Nope… Notice that the problem also exist for the line without the '!

===
#include <stdio.h>

int main(void)
{
double big = 2.3495939393949595949;

printf(“le gros numéro est %g et l’inverse est %g\n”,big, 1/big);
printf(“the big number is %g and the inverse is %g\n”,big, 1/big);

printf(“petit numéro: %d\n”,1);
printf(“small number: %d\n”,1);

}

$ qcc output.C -o output
$ ./output
le gros numéro est %g et l’inverse est %g
the big number is 2.34959 and the inverse is 0.425605
petit numéro: %d
small number: 1

Thanks!


Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Yes, I see now that the problem actually had nothing to do with the accent
mark.

See what happens when you don’t actually READ the question and just look at
the subject.

“Ricky Ng-Adam” <ng-adam.ricky@ireq.ca> wrote in message
news:3D1101B3.26DB4FAA@ireq.ca

Hello!

“Bill Caroselli (Q-TPS)” wrote:

Try escaping the ’ with a backslash, i.e.
printf(“le gros numéro est %g et l’inverse est %g\n”,big, 1/big);

Nope… Notice that the problem also exist for the line without the '!

===
#include <stdio.h

int main(void)
{
double big = 2.3495939393949595949;

printf(“le gros numéro est %g et l’inverse est %g\n”,big, 1/big);
printf(“the big number is %g and the inverse is %g\n”,big, 1/big);

printf(“petit numéro: %d\n”,1);
printf(“small number: %d\n”,1);

}

$ qcc output.C -o output
$ ./output
le gros numéro est %g et l’inverse est %g
the big number is 2.34959 and the inverse is 0.425605
petit numéro: %d
small number: 1

Thanks!


Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Ricky Ng-Adam <ng-adam.ricky@ireq.ca> wrote:

printf(“le gros numero est %g et l’inverse est %g\n”,big, 1/big);

How have you generated the acute? printf() et al expect a multibyte-
encoded string, so if this is an 8-bit/“code page” character it will
result in an invalid UTF sequence and the lost sync causes the remainder
of the formatting string to be copied verbatim (ie no ‘%’ expansion).
If you encode the acute as “\303\251” (I think) what happens?

“John Garvey” <jgarvey@qnx.com> wrote in message
news:aer1k0$4tg$1@nntp.qnx.com

Ricky Ng-Adam <> ng-adam.ricky@ireq.ca> > wrote:
printf(“le gros numero est %g et l’inverse est %g\n”,big, 1/big);

How have you generated the acute? printf() et al expect a multibyte-
encoded string, so if this is an 8-bit/“code page” character it will
result in an invalid UTF sequence and the lost sync causes the remainder
of the formatting string to be copied verbatim (ie no ‘%’ expansion).
If you encode the acute as “\303\251” (I think) what happens?

John nailed it, remove the é and it will work (just tried it)

.

The problem is not with qcc or g++ or printf.

Redirect you program output to a file and then open the file with an
editor that supports multibyte characteres. Every thing was ouputed
correctly. The problem is the terminal.

Michel

Ricky Ng-Adam <ng-adam.ricky@ireq.ca> wrote:
: Is there a table of UTF sequence somewhere?

The Photon Programmer’s Guide has an appendix on Unicode and UTF-8 that
might help. Here’s what it says:

For Unicode character values, see /usr/include/photon/PkKeyDef.h.
For more information about Unicode, see the Unicode Consortium’s
website at www.unicode.org.

The Neutrino libraries have some functions that should be useful, and the
Photon libraries have even more.

I hope this helps you.


Steve Reid stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems

Hello John!

John Garvey wrote:

Ricky Ng-Adam <> ng-adam.ricky@ireq.ca> > wrote:
printf(“le gros numero est %g et l’inverse est %g\n”,big, 1/big);

How have you generated the acute? printf() et al expect a multibyte-
encoded string, so if this is an 8-bit/“code page” character it will
result in an invalid UTF sequence and the lost sync causes the remainder
of the formatting string to be copied verbatim (ie no ‘%’ expansion).
If you encode the acute as “\303\251” (I think) what happens?

===
le gros numéro est 2.34959 et l’inverse est 0.425605
the big number is 2.34959 and the inverse is 0.425605
petit numéro: 1
small number: 1
10

Is there a table of UTF sequence somewhere?

Thanks,

Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Hello Mario!

Mario Charest wrote:

John nailed it, remove the é and it will work (just tried it)

Yes, I knew from the beginning that it was the accented characters that
screwed things up; that it was I included both the english and french
output to show that only the french output was bad.

But I do WANT the accented characters displayed correctly, just as they
are on Solaris, Linux and Windows…

Thanks,

Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Hello!

Michel Belanger wrote:

The problem is not with qcc or g++ or printf.

Redirect you program output to a file and then open the file with an
editor that supports multibyte characteres. Every thing was ouputed
correctly. The problem is the terminal.

I’m not sure I understand; does your multibyte editor automagically
substitute the %g for the correct value? Because the accented character
does display correctly in my (local or remote!) terminal, its just that
it screws up the %g substitution!

Or maybe you are talking in combinaison with John Garvey tips on
substituting the accented character for UTF \303\251… Althought I
would like to know what multibyte editor you are using under QNX6 so
that it displays correctly, it still doesn’t solve the problem that I
want both the accented characters and numbers displayed correctly IN the
terminal… Is there anything I can set the TERM environnement variable
to so that it works?

Thanks!

Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Steve Reid wrote:

Ricky Ng-Adam <> ng-adam.ricky@ireq.ca> > wrote:
: Is there a table of UTF sequence somewhere?

The Photon Programmer’s Guide has an appendix on Unicode and UTF-8 that
might help. Here’s what it says:

For Unicode character values, see /usr/include/photon/PkKeyDef.h.
For more information about Unicode, see the Unicode Consortium’s
website at > www.unicode.org> .

Ah AH! Now we are making progress!! But the problem is still there…

I simplified the example program so that nobody would get confused by
the %g and double number :wink::

===
#include <stdio.h>

#define Pk_LATIN1

#include </usr/include/photon/PkKeyDef.h>

int main(void)
{
printf(“petit num%cro: %d\n”, Pk_eacute, 1);
printf(“petit num\x0e9ro: %d\n”, 1);
}

$ qcc output.C -o output
$ ./output
petit numéro: 1
petit numéro: %d

Under Linux RH7.3:

===
#include <stdio.h>

int main(void)
{
printf(“petit num\x0e9ro: %d\n”, 1);
}

[rngadam@sonia2 rngadam]$ g++ output.C -o output
[rngadam@sonia2 rngadam]$ ./output
petit numéro: 1

Pk_eacute is (In a #ifdef Pk_LATIN1):

#define Pk_eacute 0x0e9

Now, the problem can be restated as: why does the unicode multibyte
characters screws up the string building under QNX6???

Thanks,

Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Ricky Ng-Adam wrote:

Hello!

Michel Belanger wrote:

The problem is not with qcc or g++ or printf.

Redirect you program output to a file and then open the file with an
editor that supports multibyte characteres. Every thing was ouputed
correctly. The problem is the terminal.


I’m not sure I understand; does your multibyte editor automagically
substitute the %g for the correct value? Because the accented character
does display correctly in my (local or remote!) terminal, its just that
it screws up the %g substitution!

This as nothing to do with my editor, it as nothing to do with the %g.

You can’t output multibyte characters to a terminal !It does not mather
if you use unicode encoding, cyrillic encoding or anything else that is
multibyte. It does not matter if TERM is set to qansi-m or qnx. It does
not matter if you use the GNU libs or the Dinkum libs. You can’t output
multibyte characters to a terminal !

  1. redirect your programs output to a file
    program > file.txt

2 ) open the file using ped.
ped file.txt

As you can see your program is working correctly, your accent is there.
It is the terminal that is unable to display your multibyte characters
output.

You might be able to work around it by creating a .cs ( character set )
file for the terminal. There is suppose to be a ptermcs utility to do
this ( it is in the docs ).

Michel

John Garvey <jgarvey@qnx.com> wrote:

Ricky Ng-Adam <> ng-adam.ricky@ireq.ca> > wrote:
printf(“le gros numero est %g et l’inverse est %g\n”,big, 1/big);

How have you generated the acute? printf() et al expect a multibyte-
encoded string, so if this is an 8-bit/“code page” character it will
result in an invalid UTF sequence and the lost sync causes the remainder
of the formatting string to be copied verbatim (ie no ‘%’ expansion).

It is true that since the C standard defines the printf() format in
terms of multibyte characters, printf() is not officially required to do
anything sensible when given a format that’s not a valid multibyte
sequence.

On the other hand, I think it would make sense for an OS whose own
console driver doesn’t understand multibyte characters, to have an
implementation of printf() more tolerant to format strings that aren’t
valid multibyte. Just copying all the bytes after an invalind sequence
is the safest way out in the sense that it’ll never cause a crash; but
it also never is what the programmer meant.

If you encode the acute as “\303\251” (I think) what happens?

In a pterm, this gets displayed as an ‘A’ followed by a space; other
than that, the string is formatted correctly. The same happens on a
text-mode console.


Wojtek Lerch QNX Software Systems Ltd.

Hello Michel!

Michel Belanger wrote:

This as nothing to do with my editor, it as nothing to do with the %g.
You can’t output multibyte characters to a terminal !It does not mather
if you use unicode encoding, cyrillic encoding or anything else that is
multibyte. It does not matter if TERM is set to qansi-m or qnx. It does
not matter if you use the GNU libs or the Dinkum libs. You can’t output
multibyte characters to a terminal !

Arrgh… I think I’ve been demonstrating from the very first message
that the accented characters displays CORRECTLY in the terminal as is.
Re-read my first message and tell me where I said anything to the
contrary.

The display of accented characters are NOT THE PROBLEM!!! The problem is
that printf screws up the substitution of %g, %d or whatever AFTER an
accented character or multibyte character…

I don’t know if your mailer screws up with accents or not, but in the
following, the first output line has a correctly substituted %d, then
two accent that DISPLAY PERFECTLY and then a %d that is not
substituted!!! If the output of the program on your machine doesn’t
display the accents on your terminal, then your configuration has
ANOTHER problem which is not mine…

===
#include <stdio.h>

#define Pk_LATIN1

#include </usr/include/photon/PkKeyDef.h>

int main(void)
{
printf(“pre: %d petit numéro problématique: %d\n”, 1, 1);
printf(“petit num%cro: %d\n”, Pk_eacute, 1);
printf(“petit num\x0e9ro: %d\n”, 1);
}

$ qcc output.C -o output
$ ./output
pre: 1 petit numéro problématique: %d
petit numéro: 1
petit numéro: %d

And you can’t tell me that this problem is to be expected, since in
every single other OS I have here (Windows, Linux, Solaris) the first
line WORKS FINE!

Hopefully this clears things up,


Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Hello John!

John Garvey wrote:

How have you generated the acute? printf() et al expect a multibyte-
encoded string, so if this is an 8-bit/“code page” character it will
result in an invalid UTF sequence and the lost sync causes the remainder
of the formatting string to be copied verbatim (ie no ‘%’ expansion).
If you encode the acute as “\303\251” (I think) what happens?

If I print the accented char as a number, I get -23… If it were
multibytes, I couldn’t put it as a char like this ‘é’ could I?

===
#include <stdio.h>

#define Pk_LATIN1

#include </usr/include/photon/PkKeyDef.h>

int main(void)
{
printf(“char %c code %d char again: %c number: %d\n”, ‘é’, ‘é’, -23,
1);
printf(“pre: %d petit numéro problématique: %d\n”, 1, 1);
printf(“petit num%cro: %d\n”, ‘é’, 1);
printf(“petit num%cro: %d\n”, Pk_eacute, 1);
printf(“petit num\x0e9ro: %d\n”, 1);
}
$ qcc output.C -o output
$ ./output
char é code -23 char again: é number: 1
pre: 1 petit numéro problématique: %d
petit numéro: 1
petit numéro: 1
petit numéro: %d

Again, equivalent program in Linux:

===
#include <stdio.h>

int main(void)
{
printf(“char %c code %d char again: %c number: %d\n”, ‘é’, ‘é’, -23,
1);
printf(“pre: %d petit numéro problématique: %d\n”, 1, 1);
printf(“petit num%cro: %d\n”, ‘é’, 1);
printf(“petit num\x0e9ro: %d\n”, 1);
}
[rngadam@sonia rngadam]$ g++ output.C -o output
[rngadam@sonia rngadam]$ ./output
char é code -23 char again: é number: 1
pre: 1 petit numéro problématique: 1
petit numéro: 1
petit numéro: 1

Thanks,

Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada

Ricky Ng-Adam wrote:

Hello Michel!

Michel Belanger wrote:

This as nothing to do with my editor, it as nothing to do with the %g.
You can’t output multibyte characters to a terminal !It does not mather
if you use unicode encoding, cyrillic encoding or anything else that is
multibyte. It does not matter if TERM is set to qansi-m or qnx. It does
not matter if you use the GNU libs or the Dinkum libs. You can’t output
multibyte characters to a terminal !


Arrgh… I think I’ve been demonstrating from the very first message
that the accented characters displays CORRECTLY in the terminal as is.
Re-read my first message and tell me where I said anything to the
contrary.

That is exactly what I ran and I am getting the number correctly printed

in the terminal ! And by redirecting to a file I got everything ! Then I
went a big futher and I added japenese and korean charaters to the
string , and I still got the number correctly printed. The only way I am
not getting the number correctly printed is : printf(“petit num\x0e9ro:
%d\n”, 1), witch is not in the first message.

The display of accented characters are NOT THE PROBLEM!!! The problem is
that printf screws up the substitution of %g, %d or whatever AFTER an
accented character or multibyte character…

I got the number correctly printed!



I don’t know if your mailer screws up with accents or not, but in the
following, the first output line has a correctly substituted %d, then
two accent that DISPLAY PERFECTLY and then a %d that is not
substituted!!! If the output of the program on your machine doesn’t
display the accents on your terminal, then your configuration has
ANOTHER problem which is not mine…

And you can’t tell me that this problem is to be expected, since in
every single other OS I have here (Windows, Linux, Solaris) the first
line WORKS FINE!

Hopefully this clears things up,

Yes it does. I just lost 2 1/2 hours trying to get you a .cs file hoping

it would allow you to print both the accent and the number correctly.
But I guess you won’t need it !

Michel

Add
setlocale(LC_CTYPE,“C-TRADITIONAL”);

The default multibyte encoding is utf-8 (default for C99). When an invalid
utf-8 encoding is found the current printf multibyte parser does not sync up
after the invalid utf-8 characters. Adding this setlocale() call changes the
multibyte encoding to a direct mapping. Before C99 the default multibyte
encoding was not defined and implementations seemed to ignore it passing
everything through as-is. As the other OS’s start to implement C99, they may
stop working as well. If we make the syncing up after an invalid utf-8
character work better, your example may start working. I will see what we
can do for the future.

And you can’t tell me that this problem is to be expected, since in
every single other OS I have here (Windows, Linux, Solaris) the first
line WORKS FINE!

Hopefully this clears things up,


Ricky Ng-Adam, stagiaire laboratoire de robotique
(450) 652-8499 x 2757, local BR0.50
Hydro-Québec, Montréal, Canada