473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is this printf okay?

If sizeof(int) = sizeof(long), is this undefined behavior or is it okay?

#include <stdio.h>
int main(void)
{
int i = 5;
long l = 7;
printf("i = %ld\n", i);
printf("l = %d\n", l);
return 0;
}

--
Jerry
Jan 8 '08 #1
5 2751
Jerry Newsome wrote:
If sizeof(int) = sizeof(long), is this undefined behavior or is it okay?

#include <stdio.h>
int main(void)
{
int i = 5;
long l = 7;
printf("i = %ld\n", i);
printf("l = %d\n", l);
return 0;
}
Undefined behavior. Even if the two types have the same size, there
might be padding bits in either type, and they might not be the same
bits. There might be trap representations in one type, that are not trap
representations in the other type. One of them could be two's
complement, the other might use sign-and-magnitude. It's also possible
that one is bigendian and the other is little-endian.

Of course, in the real world none of those complications is very
likely, so your code will probably work as you expect on such
implementations, unless the compiler deliberately rejects it.. But why
do it the wrong way? Doing it the right way is just as easy and a lot
more portable.
Jan 8 '08 #2
On Tue, 08 Jan 2008 03:46:41 +0000, James Kuyper wrote:
Jerry Newsome wrote:
>If sizeof(int) = sizeof(long), is this undefined behavior or is it
okay?

#include <stdio.h>
int main(void)
{
int i = 5;
long l = 7;
printf("i = %ld\n", i);
printf("l = %d\n", l);
return 0;
}

Undefined behavior. Even if the two types have the same size, there
might be padding bits in either type, and they might not be the same
bits. There might be trap representations in one type, that are not trap
representations in the other type. One of them could be two's
complement, the other might use sign-and-magnitude. It's also possible
that one is bigendian and the other is little-endian.

Of course, in the real world none of those complications is very
likely, so your code will probably work as you expect on such
implementations, unless the compiler deliberately rejects it.. But why
do it the wrong way? Doing it the right way is just as easy and a lot
more portable.
Hi James,

Thanks for your response.

The motivation for my question was a result of me coming across real-
world C90 code that did stuff like this:

typedef long signed_32;

signed_32 s32 = 5;
printf("x = %ld\n", s32);

and then it was changed to the following because type long on the new
platform/compiler was 64 bits instead of 32 bits (and type int was 32
bits):

typedef int signed_32;

signed_32 s32 = 5;
printf("x = %ld\n", s32);/*whoops, conversion specifier not updated*/

Nevertheless, your response raises some interesting questions.

If sizeof(int) = sizeof(long), then, according to the C Standard:

Can type int have a trap representation whereas type long does not, or
vice versa?

Can type int be two's complement whereas type long is sign-and-magnitude,
or vice versa?

Can type int be Little Endian whereas type long is Big Endian, or vice
versa?

If any of the answers are yes, can you, or anyone else, give me an
example of a real-world compiler that answers yes to any of the above?

Thanks
--
Jerry
Jan 9 '08 #3
On Tue, 08 Jan 2008 03:46:41 +0000, James Kuyper wrote:
Jerry Newsome wrote:
>If sizeof(int) = sizeof(long), is this undefined behavior or is it
okay?

#include <stdio.h>
int main(void)
{
int i = 5;
long l = 7;
printf("i = %ld\n", i);
printf("l = %d\n", l);
return 0;
}

Undefined behavior. Even if the two types have the same size, there
might be padding bits in either type, and they might not be the same
bits. There might be trap representations in one type, that are not trap
representations in the other type. One of them could be two's
complement, the other might use sign-and-magnitude. It's also possible
that one is bigendian and the other is little-endian.

Of course, in the real world none of those complications is very
likely, so your code will probably work as you expect on such
implementations, unless the compiler deliberately rejects it.. But why
do it the wrong way? Doing it the right way is just as easy and a lot
more portable.
Hi James,

Thanks for your response.

The motivation for my question was a result of me coming across real-
world C90 code that did stuff like this:

typedef long signed_32;

signed_32 s32 = 5;
printf("x = %ld\n", s32);

and then it was changed to the following because type long on the new
platform/compiler was 64 bits instead of 32 bits (and type int was 32
bits):

typedef int signed_32;

signed_32 s32 = 5;
printf("x = %ld\n", s32);/*whoops, conversion specifier not updated*/

Nevertheless, your response raises some interesting questions.

If sizeof(int) = sizeof(long), then, according to the C Standard:

Can type int have a trap representation whereas type long does not, or
vice versa?

Can type int be two's complement whereas type long is sign-and-magnitude,
or vice versa?

Can type int be Little Endian whereas type long is Big Endian, or vice
versa?

If any of the answers are yes, can you, or anyone else, give me an
example of a real-world compiler that answers yes to any of the above?

Thanks
--
Jerry Citizen C

Jan 9 '08 #4
Citizen C wrote:
....
If sizeof(int) = sizeof(long), then, according to the C Standard:

Can type int have a trap representation whereas type long does not, or
vice versa?

Can type int be two's complement whereas type long is sign-and-magnitude,
or vice versa?

Can type int be Little Endian whereas type long is Big Endian, or vice
versa?
Yes, the standard says nothing to prohibit any of those possibilities.
If any of the answers are yes, can you, or anyone else, give me an
example of a real-world compiler that answers yes to any of the above?
Not me; I'm familiar with only a tiny fraction of the world's compilers,
and none of the ones I know have any of those characteristics. In fact,
few of them have sizeof(int)==sizeof(long), so the rest of the question
becomes moot.
Jan 9 '08 #5
James Kuyper <ja*********@verizon.netwrites:
Citizen C wrote:
...
>If sizeof(int) = sizeof(long), then, according to the C Standard:

Can type int have a trap representation whereas type long does not,
or vice versa?

Can type int be two's complement whereas type long is
sign-and-magnitude, or vice versa?

Can type int be Little Endian whereas type long is Big Endian, or
vice versa?

Yes, the standard says nothing to prohibit any of those possibilities.
>If any of the answers are yes, can you, or anyone else, give me an
example of a real-world compiler that answers yes to any of the
above?

Not me; I'm familiar with only a tiny fraction of the world's
compilers, and none of the ones I know have any of those
characteristics. In fact, few of them have sizeof(int)==sizeof(long),
so the rest of the question becomes moot.
On old Cray vector machines (T90 et al), sizeof(int) and sizeof(long)
are both (with CHAR_BIT=8). I *think* that int has padding bits, but
long doesn't.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 9 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: scrodchunk | last post by:
I'm having a weird problem with printing bytes in hex format. I have a bunch of print statements that are working okay, then using the identical formatting later in my code I get some thing where...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
3
by: nandh_dp | last post by:
When the below program is compiled and executed, #include <stdio.h> #define MASK 0xFFFFFFULL main() { unsigned long long a; unsigned long b, c;
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
3
by: pranab.salian | last post by:
I need to compile some newer code in Borland TC 3.0. Here's the snippet.. /* CODE */ /* // --------------------------------------------------------------- // Shift register implementation:...
7
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. ...
29
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
20
by: Sanchit | last post by:
I want to know how does printf (stdio library function) works? Does this depand on complier (I am using gcc on Linix) Does it uses some buffer in which it stores all what needed to be printed...
27
by: sophia | last post by:
Dear all, why in the following program #include<stdio.h> #include<stdlib.h> int main(void) {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.