1828586 Members
2446 Online
109982 Solutions
New Discussion

printf on itanium2

 
SOLVED
Go to solution
Wanda Canade'
Advisor

printf on itanium2

we are migrating C programs from PA-RISC with operating system unix11.00 to itanium2 with operating system unix 11.23.
on itanium2 this printf:
printf("NOME <%s> <%s> <%s> <%s>\n", argv[0], ((argc == 4) ? argv[1], argv[2], argv[3] : "def1", "def2", "def3"));

produce memory fault
Can help me?
thanks in advance
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: printf on itanium2

I should hope that it produces a memory fault. You seem to have a misunderstanding of the comma operator in C and unlike in C++ it can't be overloaded. Essentially with commas the left assignments are thrown away and only the rightmost counts.

Try something like this:

int main(int argc, char *argv[])
{
int i = -1;
int j = -2;

i,j = (1,2,3);
(void) printf("i = %d j = %d\n",i,j);
return(i);
}

Without testing this, if C works like i think it does, the output should be:
i = -1 j = 3

In your case, only 1 of the 3 pointers gets a value, the remaining arguments are whatever is on the stack (garbage).

If it ain't broke, I can fix that.
Wanda Canade'
Advisor

Re: printf on itanium2

thanks for help
bye
Tom Henning
Trusted Contributor

Re: printf on itanium2

I did a wee bit more than Clay did (I was wondering about this, I also read Usenet comp.lang.c) and tried this code:

#include
#include

int main(void)
{
int i = 0;
int j = 1;
int k = 2;
int l = 3;

printf("%d %d %d %d\n",
((i == 0) ? k, j, l, i : l, k, i, j));

return (EXIT_SUCCESS);
}

I compiled this one a rp2470 under 11i with the ANSI C HP compiler and it compiled okay, but the resultant output was '1 2097088492 134217759 3'. About what I would expect.

The same code, when compiled under gcc witht he -Wall flag on returns, on compile:
$ gcc -Wall one.c -o one
one.c: In function `main':
one.c:12: warning: left-hand operand of comma expression has no effect
one.c:12: warning: left-hand operand of comma expression has no effect
one.c:12: warning: left-hand operand of comma expression has no effect
one.c:12: warning: left-hand operand of comma expression has no effect
one.c:12: warning: left-hand operand of comma expression has no effect
one.c:12: warning: left-hand operand of comma expression has no effect
one.c:12: warning: too few arguments for format

Actually, a few less errors than I expected, but.....

Wanda, I think you have been extremely lucky if the code you posted actually returned the expected results when compiled with a C compiler. I would recommend that all of the constructs like the one you posted be revised, personnally I would use something on the order of:

printf("NOME <%s> ", argv[0]);
if (argc == 4)
printf("<%s> <%s> <%s>\n", argv[1], argv[2], argv[3]);
else
printf(" \n");

But then, the above is personnal coding style, of which arguments can lead to long threads in which nothing much is actually said or accomplished. It is also untested, and written off the top oif my head, so I might jhave made typo's in it.

What is it that possesses otherwise sane individuals to change something just because it has not been changed in a while?
Hein van den Heuvel
Honored Contributor

Re: printf on itanium2

>> Wanda, I think you have been extremely lucky if the code you posted actually returned the expected results when compiled with a C compiler.

Hmmm, I would have called that being 'UNlucky'.
It is so much easier to be told your code is broken when it is first written, and not much later when there is a sea filled with red herrings to disguise the problem.

fwiw,
Hein.