By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,633 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,633 IT Pros & Developers. It's quick & easy.

question related to sizeof operator

P: n/a
Consider the following piece of code:

#include <stddef.h>

int main (void)
{

int i, j=1;
char c;
printf("\nsize =%lu\n", sizeof(i+j));
printf("sizeof =%lu\n", sizeof(c+i));
printf("size =%lu\n", sizeof(i++));
}

I wanted to how sizeof is evaluated in each case ?

Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ju**********@yahoo.co.in wrote:
Consider the following piece of code:

#include <stddef.h>

int main (void)
{

int i, j=1;
char c;
printf("\nsize =%lu\n", sizeof(i+j));
printf("sizeof =%lu\n", sizeof(c+i));
printf("size =%lu\n", sizeof(i++));
}

I wanted to how sizeof is evaluated in each case ?


In each of those cases, the result is the same as if you'd written
sizeof (int), since all of those expressions have type int. And no, i is
not increased in the last case.

Would it not be better (more efficient, for a start) for you to get a
book on C instead of continuously asking these basic questions here - or
failing that, to resort to
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/>?

Richard
Nov 14 '05 #2

P: n/a
ju**********@yahoo.co.in wrote:

Consider the following piece of code:

#include <stddef.h>
You misspelled <stdio.h>
int main (void)
{

int i, j=1;
char c;
printf("\nsize =%lu\n", sizeof(i+j));
printf("sizeof =%lu\n", sizeof(c+i));
printf("size =%lu\n", sizeof(i++));
}


sizeof(int) all three times.
Operands of sizeof are not evaluated and produce no side effects.

Try this one:

/* BEGIN new.c */

#include <stdio.h>

int main (void)
{
char array[sizeof (char *) + 1] = "";

putchar('\n');
printf("sizeof array = %lu\n",
(long unsigned)sizeof array);
printf("sizeof (array + 0) = %lu\n",
(long unsigned)sizeof (array + 0));
printf("sizeof (array[0] + array[0]) = %lu\n",
(long unsigned)sizeof (array[0] + array[0]));
printf("sizeof array[0]++ = %lu\n",
(long unsigned)sizeof array[0]++);
printf("array[0] is %d\n",
(int)array[0]);
return 0;
}

/* END new.c */

--
pete
Nov 14 '05 #3

P: n/a
On 2005-06-10 10:27:53 -0400, ju**********@yahoo.co.in said:
Consider the following piece of code:

#include <stddef.h>

int main (void)
{

int i, j=1;
char c;
printf("\nsize =%lu\n", sizeof(i+j));
printf("sizeof =%lu\n", sizeof(c+i));
printf("size =%lu\n", sizeof(i++));
}

I wanted to how sizeof is evaluated in each case ?


This code is equivalent (in all respects, including not incrementing i):

#include <stddef.h>

int main (void)
{
int i, j=1;
char c;
printf("\nsize =%lu\n", sizeof(int));
printf("sizeof =%lu\n", sizeof(int));
printf("size =%lu\n", sizeof(int));
}

--
Clark S. Cox, III
cl*******@gmail.com

Nov 14 '05 #4

P: n/a
In article <11*********************@f14g2000cwb.googlegroups. com>,
<ju**********@yahoo.co.in> wrote:

printf("size =%lu\n", sizeof(i++));

I wanted to how sizeof is evaluated in each case ?


You should also be concerned with how %lu is evaluated.
--
7842++
Nov 14 '05 #5

P: n/a
On 10 Jun 2005 07:27:53 -0700, ju**********@yahoo.co.in wrote:
Consider the following piece of code:

#include <stddef.h>

int main (void)
{

int i, j=1;
char c;
printf("\nsize =%lu\n", sizeof(i+j));
printf("sizeof =%lu\n", sizeof(c+i));
printf("size =%lu\n", sizeof(i++));
}

I wanted to how sizeof is evaluated in each case ?


Unless you happen to know that size_t is defined as unsigned long, you
need to cast the second argument in each of the calls to printf. Even
if it is true on your system, you should still perform the cast so
others, for whom it may not be true, can use your code as they try to
answer your question.
<<Remove the del for email>>
Nov 14 '05 #6

P: n/a
Hi,

In all the cases above the sizeof operator will return the size of the
data type containing the value passed.
So in the 2nd case when "c+i" is passed the data type is converted to
integer by default so the return value is the sizeof(int).
Hence the evaluation of sizeof operator depends on the resultant data
type but not on the value of the parameter.

Sarath.B
IIIt-H

Nov 14 '05 #7

P: n/a
ju**********@yahoo.co.in wrote:

Consider the following piece of code:

#include <stddef.h>

int main (void)
{

int i, j=1;
char c;
printf("\nsize =%lu\n", sizeof(i+j));
printf("sizeof =%lu\n", sizeof(c+i));
printf("size =%lu\n", sizeof(i++));
}

I wanted to how sizeof is evaluated in each case ?

"sizeof" returns the size of the result that the expression
*would* have produced...but the expression is *not* evaluated
and *no* side effects happen either. The expression is what
was once called "dead code".

--
+----------------------------------------------------------------+
| Charles and Francis Richmond It is moral cowardice to leave |
| undone what one perceives right |
| richmond at plano dot net to do. -- Confucius |
+----------------------------------------------------------------+
Nov 14 '05 #8

P: n/a
On Sat, 11 Jun 2005 03:27:19 -0700, Charles Richmond wrote:
ju**********@yahoo.co.in wrote:

Consider the following piece of code:

#include <stddef.h>

int main (void)
{

int i, j=1;
char c;
printf("\nsize =%lu\n", sizeof(i+j));
printf("sizeof =%lu\n", sizeof(c+i));
printf("size =%lu\n", sizeof(i++));
}

I wanted to how sizeof is evaluated in each case ?

"sizeof" returns the size of the result that the expression
*would* have produced...but the expression is *not* evaluated
and *no* side effects happen either. The expression is what
was once called "dead code".


Specifically sizeof gives the size of the TYPE of the result of its
operand. So all it needs to do is determine the type, it isn't necessary
to evaluate an expression operand to do that. However the operand is
evaluated if it is a C99 Variable Length Array.

Lawrence

Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.