473,326 Members | 2,168 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,326 software developers and data experts.

conditional expression

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?

Sep 6 '06 #1
10 2233
chandanlinster wrote:
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.
Sep 6 '06 #2
chandanlinster wrote:
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.

--
Chris "seeker" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/

Sep 6 '06 #3
Nils O. Selåsdal wrote:
chandanlinster wrote:
>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.
Let's drop the plurals, it's an unary operator :-|
Sep 6 '06 #4
Thanks everybody!!!!!!!!!!!

Sep 6 '06 #5
Nils O. Selåsdal posted:
>The sizeof operator is a bit special, it's arguments are not evaluated.

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

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

--

Frederick Gotham
Sep 6 '06 #6
Because 'sizeof' is a compile-time operator,
and it be just a constant in run-time.

Sep 6 '06 #7
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.

Sep 6 '06 #8
Frederick Gotham wrote:
Nils O. Selåsdal posted:
>>The sizeof operator is a bit special, it's arguments are not evaluated.

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

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.)

--
Chris "early pdp11 fortran" Dollin
Meaning precedes definition.

Sep 6 '06 #9
Chris Dollin wrote:
Frederick Gotham wrote:
Nils O. Sel?sdal posted:
>The sizeof operator is a bit special, it's arguments are not evaluated.

Let's drop the plurals, it's an unary operator :-|
While we're being pedantic, it's an operand, not an argument.

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

Sep 6 '06 #10
Aman JIANG wrote:
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
Sep 6 '06 #11

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

Similar topics

8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
62
by: Reinhold Birkenfeld | last post by:
Hi, after Guido's pronouncement yesterday, in one of the next versions of Python there will be a conditional expression with the following syntax: X if C else Y which is the same as today's...
2
by: John Kreps | last post by:
(acc 2002) I've got six unbound text boxes on a subform that has a white background. Each of those six boxes has an expression that when true, will change its background from white to another...
1
by: GGerard | last post by:
Hello Is there a way to use a variable in the Conditional Formatting of a Textbox? Example : I want the background of a textbox in a continuous form to change color when the value of...
11
by: Dan Noland | last post by:
A friend of mine asked me a C question that I couldn't answer. Could you guys weigh in? ---------- Forwarded message ---------- Date: Sun, 21 Nov 2004 21:08:27 -0500 From: Rick Kennell...
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null :...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
4
by: Colin J. Williams | last post by:
It would be helpful if the rules of the game were spelled out more clearly. The conditional expression is defined as X if C else Y. We don't know the precedence of the "if" operator. From the...
15
by: Dan Henry | last post by:
I have run across functions in the Linux kernel's MTD driver that have me scratching my head a bit. The functions have the general form: extern int bar(size_t len, size_t *retlen, unsigned char...
15
by: Nicholas M. Makin | last post by:
I was just thinking that I understood the conditional operator when I coded the following expecting it to fail: int a= 10, b= 20, c= 0; ((a < b) ? a : b) = c; // a=0 a=20; b= 10; ((a < b) ? a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.