question related to sizeof operator

J

junky_fellow

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 ?
 
R

Richard Bos

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
 
P

pete

Consider the following piece of code:

#include <stddef.h>

You misspelled said:
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 */
 
C

Clark S. Cox III

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));
}
 
B

Barry Schwarz

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>>
 
S

Sarath

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
 
C

Charles Richmond

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 |
+----------------------------------------------------------------+
 
L

Lawrence Kirby

"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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top