473,795 Members | 3,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sizeof and increment operators

#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.
Nov 14 '05 #1
31 2390
g_********@redi ffmail.com (Anjali M) wrote:
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
Nov 14 '05 #2
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

"Anjali M" <g_********@red iffmail.com> wrote in message
news:99******** *************** ***@posting.goo gle.com...
#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.

Nov 14 '05 #3
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
Anjali M wrote:
#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.

Nov 14 '05 #4
g_********@redi ffmail.com (Anjali M) wrote in message news:<99******* *************** ****@posting.go ogle.com>...
#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
Nov 14 '05 #5
"smitha" <sm*****@cisco. com> wrote in message news:<108909700 7.531247@sj-nntpcache-3>...
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 !!!
Bye,

Smitha

"Anjali M" <g_********@red iffmail.com> wrote in message
news:99******** *************** ***@posting.goo gle.com...
#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.

Nov 14 '05 #6
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.

"Nitin Bhardwaj" <ni************ *@hotmail.com> wrote in message
news:17******** *************** ***@posting.goo gle.com...
"smitha" <sm*****@cisco. com> wrote in message

news:<108909700 7.531247@sj-nntpcache-3>...
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 !!!

Bye,

Smitha

"Anjali M" <g_********@red iffmail.com> wrote in message
news:99******** *************** ***@posting.goo gle.com...
#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.

Nov 14 '05 #7
In <1089097007.531 247@sj-nntpcache-3> "smitha" <sm*****@cisco. com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
Anjali M wrote:

#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!!!
+-------------------------------
Nov 14 '05 #9
Charles Richmond <ri******@plano .net> wrote:
What's the matter??? Can't you *do* the job after you
suck it up from the U. S.???


Got fired, didya?

Richard
Nov 14 '05 #10

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

Similar topics

70
8916
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
9
3193
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a post-increment or post-decrement operator modifies the operand once the entire statement has been executed, not during execution of the statement, so this confused me. An examples given to illustrate the ambiguity is: a = i++; // may increment...
42
2416
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
11
3913
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
13
9842
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are handled with the same overload, but I just want to make sure.
3
3041
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.