conditional expression

C

chandanlinster

consider the following program:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
int i;
double f;

printf("size = %u", sizeof((0)?f:i)); /* 0 is zero */

putchar('\n');
exit(0);
}

output:
size = 8

since 'i' is supposed to be an "int", on my 32-bit system I expected
the size to be 4 bytes. Why does this happen?
 
C

Chris Dollin

chandanlinster said:
consider the following program:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
int i;
double f;

printf("size = %u", sizeof((0)?f:i)); /* 0 is zero */

putchar('\n');
exit(0);
}

output:
size = 8

since 'i' is supposed to be an "int", on my 32-bit system I expected
the size to be 4 bytes. Why does this happen?

Because on your system `sizeof (double)` is 8 (and you were unlucky with
the printf formatting).

The type of `(whatever) ? f : i` is `double`; it doesn't matter what the
value of `whatever` is.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

chandanlinster said:
consider the following program:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
int i;
double f;

printf("size = %u", sizeof((0)?f:i)); /* 0 is zero */

putchar('\n');
exit(0);
}

output:
size = 8

since 'i' is supposed to be an "int", on my 32-bit system I expected
the size to be 4 bytes. Why does this happen?

The sizeof operator is a bit special, it's arguments are not evaluated.

Given an expression, sizeof computes the size of the result type of that
expression (without evaluating the expression).

In this case, the 2. operand of ?: is an int, the 3. is a double so the
conversion rules would state the result type to be a double - that's the
type you compute the size of.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Nils said:
The sizeof operator is a bit special, it's arguments are not evaluated.

Let's drop the plurals, it's an unary operator :-|
 
A

Aman JIANG

Because 'sizeof' is a compile-time operator,
and it be just a constant in run-time.
 
A

Aman JIANG

consider this program:

int a, b;
a = 0;
b = sizeof(a++);
printf ("%d %d\n", a, b);

guess the output...
Yes:
0 4
on a 32-bit machine, integer 'b' is 4 commonly.
 
C

Chris Dollin

Frederick said:
Nils O. Selåsdal posted:


While we're being pedantic, it's an operand, not an argument.

Aren't operands the arguments of operators?

(I have no argument with calling operands "arguments". Maybe that's
because I think of operators as functions with magic syntax and
sometimes magic implementations.)
 
R

Robert Gamble

Chris said:
Aren't operands the arguments of operators?

Technically, no:

"An operand is an entity on which an operator acts."

Argument: "expression in the comma-separated list bounded by the
parentheses in a function call expression, or a sequence of
preprocessing tokens in the comma-separated list bounded by the
parentheses in a function-like macro invocation"

Robert Gamble
 
D

Default User

Aman said:
Because 'sizeof' is a compile-time operator,
and it be just a constant in run-time.


Except for variable-length arrays in C99 compatible compilers.




Brian
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top