Sizeof and increment operators

A

Anjali M

#include <stdio.h>

int main()
{
int number = 10;
printf("Number is: %d\n", number);

printf("Sizeof of number is: %d\n", sizeof(number++));

printf("Number now is: %d\n", number);

return 0;
}


The post-increment operator doesnt seem to work here. The value of the
number remains the same even after the call to the sizeof operator.
Can someone clarify this please?

Thanks,
Anjali.
 
R

Richard Bos

printf("Sizeof of number is: %d\n", sizeof(number++));
The post-increment operator doesnt seem to work here. The value of the
number remains the same even after the call to the sizeof operator.
Can someone clarify this please?

From the last public draft of the C99 Standard, 6.5.3.4#2:

# The sizeof operator... If the type of the operand is a variable length
# array tye, the operand is evaluated; otherwise, the operand is not
# evaluated and the result is an integer constant.

Richard
 
S

smitha

Hi,

The sizeof operator just returns the size of the type or expressions,

so i guess,it is not defined to allow airthmetic operations within it.

Bye,

Smitha
 
S

Sumukh

HI anjali,

Sizeof is an *operator* in C.
The operand expected in sizeof is either an identifier that is a
unary-expression, or a type-cast expression (that is, a type specifier
enclosed in parentheses). The unary-expression cannot represent a
bit-field object, an incomplete type, or a function designator.

A unary expression is an expression whose valuation is computed by
applying its (unary) operator to the valuation of its operand. There are
three kinds of unary operators:
(1) the logical complement unary operator "!",
(2) the plus unary operator "+", and
(3) the minus unary operator "-".

Hope this helps

sumukh
 
N

Nitin Bhardwaj

#include <stdio.h>

int main()
{
int number = 10;
printf("Number is: %d\n", number);

printf("Sizeof of number is: %d\n", sizeof(number++));

printf("Number now is: %d\n", number);

return 0;
}


The post-increment operator doesnt seem to work here. The value of the
number remains the same even after the call to the sizeof operator.
Can someone clarify this please?

Thanks,
Anjali.

Hi Anjali,

The problem is not that of the ++ operator...but the 'sizeof'
operator.
The C Standard states that the operand of sizeof can be either a type
name or an expression.

e.g. sizeof (int) OR sizeof (a+b)

If it is an expression,then it will not be evaluated; See section
A.7.4.8 of K&R2 - [The C Programming Language 2/ed - Kernighan &
Ritchie ], Appendix A.

For the sake of convenience I'm reproducing the contents verbatim
here :

A.7.4.8 Sizeof Operator
The sizeof operator yields the number of bytes required to store an
object of the type of its operand. The operand is either an
expression,***_which is not evaluated_**, or a parenthesized type
name. When sizeof is applied to a char, the result is 1; when applied
to an array, the result is the total number of bytes in the array.
When applied to a structure or union, the result is the number of
bytes in the object, including any padding required to make the object
tile an array: the size of an array of n elements is n times the size
of one element. The operator may not be applied to an operand of
function type, or of incomplete type, or to a bit-field. The result is
an unsigned integral constant; the particular type is
implementation-defined.

Hence your expresion number++ never gets evaluated.So the value of
number doesn't increase.

HTH
Nitin
 
N

Nitin Bhardwaj

smitha said:
Hi,

The sizeof operator just returns the size of the type or expressions,

so i guess,it is not defined to allow airthmetic operations within it.

Wrong, the operand of sizeof operator is either type-names OR
expressions - which also includes arithmetic expressions.If it were
not defined for arithmetic expressions then the compiler must have
complained !!

You can even include a function call in the argument to sizeof !!!
 
S

smitha

yes... sizeof is an operator and not a function. In a function call each
argument will be evaluated but this is not the case in sizeof.
 
D

Dan Pop

In said:
The sizeof operator just returns the size of the type or expressions,

Without evaluating them (except for VLAs in C99).
so i guess,it is not defined to allow airthmetic operations within it.

It is perfectly defined and allowed, they just won't be performed: the
compiler will merely figure out the type of the result.

Dan
 
J

Joona I Palaste

Charles Richmond said:
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???

You are incredibly rude. What makes you so sure the OP sucked a job from
the US? What makes you so sure the OP has a job as a programmer at all?
This might just as well be a question from a genuine C newbie. And
besides, even if he did suck up the job from the US, what business is
that of yours? This is a technical newsgroup. If you need to vent your
patriotism, go to a politics newsgroup.
 
C

Charles Richmond

Anjali said:
#include <stdio.h>

int main()
{
int number = 10;
printf("Number is: %d\n", number);

printf("Sizeof of number is: %d\n", sizeof(number++));

printf("Number now is: %d\n", number);

return 0;
}

The post-increment operator doesnt seem to work here. The value of the
number remains the same even after the call to the sizeof operator.
Can someone clarify this please?
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???

--
+-------------------------------
| Charles and Francis Richmond
| richmond at plano dot net
| Re-Defeat Bush!!!
+-------------------------------
 
N

Nitin Bhardwaj

Charles Richmond said:
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???


It seems frm yr language that u hate Indians !!

Did u lost ur job to an Indian, buddy?

Cough up yr technical skills to match-up to ours & take on the
competition instead of making political transgressions on a technical
group like c.l.c!!
 
R

Richard Bos

It seems frm yr language that u hate Indians !!

Did u lost ur job to an Indian, buddy?

Cough up yr technical skills to match-up to ours & take on the
competition instead of making political transgressions on a technical
group like c.l.c!!

Ow! I know the English did some pretty awful things in India, but do you
_have_ to take revenge by murdering their language? Leave that to the
USAnians, like Mr. Richmond ;->

Richard
 
D

Dan Pop

In said:
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???

How many job assignments like this have you seen? This is not only
a newbie question, it is a perfectly legitimate newbie question.

Dan
 
A

Anjali M

Charles Richmond said:
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???

I don't know what you have got against me. I asked a very valid
question on this newsgroup and I _dont_ have to take this crap from
you. If you can be rude, so can _I_. So, better be careful before you
say something like this again.
 
A

Anjali M

Charles Richmond said:
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???

I don't know what you have got against me. I asked a very valid
question on this newsgroup and I _dont_ have to take this crap from
you. If you can be rude, so can _I_. So, better be careful before you
say something like this again.
 
A

Anjali M

Charles Richmond said:
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???

I don't know what you have got against me. I asked a very valid
question on this newsgroup and I _dont_ have to take this crap from
you. If you can be rude, so can _I_. So, better be careful before you
say something like this again.
 
A

Anjali M

Charles Richmond said:
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???

I don't know what you have got against me. I asked a very valid
question on this newsgroup and I _dont_ have to take this crap from
you. If you can be rude, so can _I_. So, better be careful before you
say something like this again.
 
A

Anjali M

Charles Richmond said:
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???

I don't know what you have got against me. I asked a very valid
question on this newsgroup and I _dont_ have to take this crap from
you. If you can be rude, so can _I_. So, better be careful before you
say something like this again.
 
A

Anjali M

Charles Richmond said:
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???

I don't know what you have got against me. I asked a very valid
question on this newsgroup and I _dont_ have to take this crap from
you. If you can be rude, so can _I_. So, better be careful before you
say something like this again.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top