Operating System - HP-UX
1833723 Members
4122 Online
110063 Solutions
New Discussion

Ansi C compiler inconsistency ?

 
Johan_52
Advisor

Ansi C compiler inconsistency ?

The following program

int main()
{
long int l = 0;
int i = 0;
short int s = 0;

i = (int) (l & 0xffffffff);
s = (short int) (l & 0xffff);
s = (short int) (i & 0xffff);
return 0;
}

reports a warning 'line 7, warning 740: Casting to a smaller size may truncate value' when compiling it with the command
cc -Aa -DD64 +w1

Why do I get this warning when casting a long to an int and not when casting the same long to a short, or casting an int to a short ?

And second is there a way to circumvent this warning other then specifying +W740 in the cc command ?

I'm trying to eliminate as much warnings as possible from my code, but I don't see a way to get rid of this one.

Thanks

Johan
6 REPLIES 6
Paddy_1
Valued Contributor

Re: Ansi C compiler inconsistency ?

I just compiled your program on a N-Class HP-UX
and it ran just fine with no complaints.

Could you check again please..
The sufficiency of my merit is to know that my merit is NOT sufficient
Johan_52
Advisor

Re: Ansi C compiler inconsistency ?

Checked again but it definitely returns the warning described in my original post.

For the record, I see this behavior on: HP-UX 11.0
with C compiler
cpp.ansi: HP92453-01 A.11.01.20 HP C Preprocessor (ANSI)
ccom: HP92453-01 A.11.01.20 HP C Compiler

Paddy_1
Valued Contributor

Re: Ansi C compiler inconsistency ?

Could you please run the following program and post the output

int main()
{
long l=-1;
unsigned int i=1;
if( l > i ) printf("ilp32");
else printf("lp64");
}

The sufficiency of my merit is to know that my merit is NOT sufficient
Johan_52
Advisor

Re: Ansi C compiler inconsistency ?

It compiles with the following warning:

cc: "p1.c", line 6: warning 734: Different types treated as signed for >.
And some warnings about printf and not returning a value

It outputs:
lp64

Johan
Paddy_1
Valued Contributor

Re: Ansi C compiler inconsistency ?

I guessed as much because mine returns ilp32.
To compile it immediately please find a compiler or a different machine where you would get ilp32.

The output of the program is to be used :
ilp32 means -1 is promoted to uint32.
lp64 means both are promoted to signed 64.

I got a dig more to find out if this is a problem,
The sufficiency of my merit is to know that my merit is NOT sufficient
Johan_52
Advisor

Re: Ansi C compiler inconsistency ?

I don't have any other 64 bit compilers, but compiling without the +DD64 option, or with gcc (2.95.3) or on an old HP-UX 10.20 system all generate a binary that prints : ilp32

BTW there is a mistake in my original post the cc command should state +DD64 and not -DD64. Obviously I used the +DD64 switch.

Seems to me that your code snipped shows a problem in the environments where ilp32 is returned. This problem can be eliminated through a typecast 'if( l > (long) i ) '
but that's not the point.

In my opinion the fact that you use a typecast, indicates that there is a potential problem, which you are aware of and accept. If you can't accept the consequences you have to reprogram IMO.
That's why I'm surprised that I get a warning on the long to int typecast in my original post.