473,289 Members | 1,959 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,289 software developers and data experts.

question related to sizeof operator

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
8 2494
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rick | last post by:
Hi again, I once saw that it was possible to define operators in C++ or something so I was thinking, is it possible to store and use operators in C? For example, first I read out a formula char...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
10
by: Jeroen | last post by:
Hi guys, Just another question. Suppose I have 2 classes (incomplete code): class A { A(const B& b); A& operator = (const A& a); }; class B {
7
by: linq936 | last post by:
Hi, I am puzzled on this C puzzle: How to interpret this C statement: sizeof (int) * p Is that the size of integer type multiplies p or the size of whatever p points to in integer type? I...
11
by: Richard Tobin | last post by:
Please excuse me if this has already been covered. Given char x; is sizeof(x)
3
by: news.inode.at | last post by:
Sorry for this stupid question, but i am lost. If i write an stringlib with += overload operators (no i do not, but my thing is much more complicated) , and i have to precalculate the strlen() --...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...

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.