A question regarding Q20.1 from c-faq.com

C

CBFalconer

Old said:
How is it relevant whether they're the same size or not?

Well, if they are not the same size, it will be hard to stuff both
types into the storage for the smaller type. :) I suspect some
quoting is missing.
 
O

Old Wolf

If they're different sizes, then they can't be compatible.

OP's trying to assign a pointer-to-double to a double.
Types don't have to be the same size in order for one
to be assignable to the other; for example, you can
assign a char to a double.
 
M

Mark McIntyre

Peter said:
If they're different sizes, then they can't be compatible.

#include <stdio.h>
int main(void)
{
char c = 'A';
long double x = c;
printf("%d %d %d\n", sizeof 'A', sizeof c, sizeof x);
return 0;
}
$ ./a.out
4 1 12
 
P

Peter Nilsson

Old Wolf said:
OP's trying to assign a pointer-to-double to a double.

In that case, you may as well ask Ian why he made the first
statement. As it was, it looked like you were questioning
the value of the second statement in the context of the
first.
Types don't have to be the same size in order for one
to be assignable to the other; ...

True, but no one said they did.
 
P

Peter Nilsson

#include <stdio.h>
int main(void)
{
char c = 'A';
long double x = c;
printf("%d %d %d\n", sizeof 'A', sizeof c, sizeof x);
return 0;}

$ ./a.out
4 1 12

Is there a point to this broken code?
 
G

Geoff

Is there a point to this broken code?

What's broken about it? It compiles without errors or warnings of any
kind.

It shows that it is possible to store one arithmetic type in another
without upsetting the compiler even though they are different sizes.
 
P

Peter Nilsson

Geoff said:
What's broken about it?

%d expects and int, not size_t.
It compiles without errors or warnings of any kind.

I hope that's not your criteria for what makes a correct
program. [P.S. gcc -Wall diagnoses the error.]
It shows that it is possible to store one arithmetic type
in another without upsetting the compiler

You mean _a_ compiler.
even though they are different sizes.

Which has what to do with my statement above?
[cf my reply to Old Wolf.]
 
W

Walter Roberson

What's broken about it? It compiles without errors or warnings of any
kind.

sizeof returns size_t, but the printf format used is %d (which is for
int).

printf("%d %d %d\n", (int) sizeof 'A', (int) sizeof c, (int) sizeof x);
 
R

Richard Heathfield

Geoff said:
What's broken about it?

Type mismatches between format specifier and subsequent arguments to
printf.
It compiles without errors or warnings of any kind.

All that means is that it doesn't contain any syntax errors or constraint
violations. It doesn't mean it's correct C code.

<snip>
 
G

Geoff

%d expects and int, not size_t.
size_t is unsigned int?
It compiles without errors or warnings of any kind.

I hope that's not your criteria for what makes a correct
program. [P.S. gcc -Wall diagnoses the error.]

Not all versions of gcc. Maybe yours. This would make it
implementation dependent, would it not? Syntactically correct is not
necessarily logically correct, but no one said it was. Nor does
"broken" make any meaningful distinction between logical correctness
and syntactically correct. As a demonstration of the meaninglessness
of saying "different sizes are incompatible" it is a fine example.
You mean _a_ compiler.

Yep. Actually three different ones.
even though they are different sizes.

Which has what to do with my statement above?
[cf my reply to Old Wolf.]

McIntyre can defend his own demonstration if he wants to. I don't see
the relevance of "incompatible size" to the original bug of trying to
modify a pointer from a float.
 
I

Ian Collins

Geoff said:
size_t is unsigned int?
In might be, it all depends on the system. On most systems I use, it's
unsigned long. On 64 bit systems with with 32 bit long, it's probably
unsigned long long.
 
R

Richard Heathfield

Geoff said:

size_t is unsigned int?

size_t is an unsigned integer type. It could be unsigned char, unsigned
short int, unsigned int, unsigned long int, or (if you have C99) even
unsigned long long int.
It compiles without errors or warnings of any kind.

I hope that's not your criteria for what makes a correct
program. [P.S. gcc -Wall diagnoses the error.]

Not all versions of gcc. Maybe yours. This would make it
implementation dependent, would it not?

The implementation gets the choice of whether to diagnose this (and it does
not have to document its choice). The behaviour of the program, however,
is undefined, whether or not a diagnosis occurs.

<snip>
 
G

Geoff

sizeof returns size_t, but the printf format used is %d (which is for
int).

printf("%d %d %d\n", (int) sizeof 'A', (int) sizeof c, (int) sizeof x);

Ah! Well casting makes everything alright then. Why go to all that
trouble? Why not just use %i or even better, %u?

And why has anyone not noticed that sizeof 'A' returned 4, not 1?
 
R

Richard Heathfield

Geoff said:

And why has anyone not noticed that sizeof 'A' returned 4, not 1?

The value yielded by sizeof 'A' depends on your implementation. It may be
1, 2, 3, 4, or indeed any positive integer.
 
P

Peter Nilsson

Ah! Well casting makes everything alright then.

[Not really. The program still doesn't bare any relation to
my statement that in order to be compatible types, two
types must have the same size. Note that compatible type is
a concept that is explicitly defined in the C standard.
Suitability of operands in assignment is a separate issue.]
Why go to all that trouble? Why not just use %i or even
better, %u?

Because they are also wrong. %u will only work if size_t
happens to be an unsigned int, but on the virtual C machine
you cannot assume this.

C99 added a length modifier of z, so size_t can be printed
(and scanned) with %zu. But for C99, a strictly conforming
program must cast the value.
And why has anyone not noticed that sizeof 'A' returned
4, not 1?

Because there are very few hosted systems where sizeof(int)
is 1. Indeed, it's held that such systems are not conforming.
[In C++, 'A' has character type; in C, it has type int.]
 
C

CBFalconer

Geoff said:
(Walter Roberson) wrote:
.... snip ...


Ah! Well casting makes everything alright then. Why go to all
that trouble? Why not just use %i or even better, %u?

Because it's wrong.
And why has anyone not noticed that sizeof 'A' returned 4, not 1?

Because it's right. Most machines will make it 2 or 4.
 
K

Keith Thompson

Geoff said:
[...]
sizeof returns size_t, but the printf format used is %d (which is for
int).

printf("%d %d %d\n", (int) sizeof 'A', (int) sizeof c, (int) sizeof x);

Ah! Well casting makes everything alright then. Why go to all that
trouble? Why not just use %i or even better, %u?

"%i" does the same thing as "%d". "%u" is correct only if size_t
happens to be unsigned int.
And why has anyone not noticed that sizeof 'A' returned 4, not 1?

I'm sure it was noticed; nobody mentioned it because it's
unremarkable. See question 8.9 in the comp.lang.c FAQ,
<http://www.c-faq.com/>.
 
S

santosh

Peter said:
Ah! Well casting makes everything alright then.

[Not really. The program still doesn't bare any relation to
my statement that in order to be compatible types, two
types must have the same size. Note that compatible type is
a concept that is explicitly defined in the C standard.
Suitability of operands in assignment is a separate issue.]
Why go to all that trouble? Why not just use %i or even
better, %u?

Because they are also wrong. %u will only work if size_t
happens to be an unsigned int, but on the virtual C machine
you cannot assume this.

C99 added a length modifier of z, so size_t can be printed
(and scanned) with %zu. But for C99, a strictly conforming
program must cast the value.

But for C89, a strictly...

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,811
Messages
2,569,693
Members
45,477
Latest member
IsidroSeli

Latest Threads

Top