From the C99 Standard (the emphasis is mine).
From the C99 Standard (the emphasis is mine) 6.5.3.4/2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer.
If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
25 "If the type of the operand is a variable length array type, the operand is evaluated" wow! I never realized that – Kos Nov 22 at 11:13 3 what do you mean by variable length array type? That does mean the operand is an array?
The code in this case is not an array. Can you clear things up for me? – Neigyl R.
Noval Nov 22 at 11:19 20 A variable length array is an array declared with the size being a value unknown during compilation, for instance if you read N from stdin and make int arrayN. This is one of C99 features, unavailable in C++. – Kos Nov 22 at 11:23 9 @LegendofCage, in particular this would mean that in something like sizeof(int++x) (really, really a bad idea, anyhow) the ++ could be evaluated.
– Jens Gustedt Nov 22 at 12:01 2 See example of VLA in use at http://ideone.com/Q89QP. – pmg Nov 22 at 13:29.
Sizeof is a compile-time operator, so at the time of compilation sizeof and its operand get replaced by the result value. The operand is not evaluated (except when it is a variable length array) at all; only the type of the result matters. Short func(short x) { // this function never gets called!
Printf("%d", x); // this print never happens return x; } int main() { printf("%d", sizeof(func(3))); // all that matters to sizeof is the // return type of the function. Return 0; } Output: 2 as short occupies 2 bytes on my machine. Changing the return type of the function to double: double func(short x) { // rest all same will give 8 as output.
5 Only sometimes - it's compile time if possible. – Martin Beckett Nov 22 at 17:09 1 it's single clear explanation at this page that points out why – shbk Nov 22 at 17:24 2 -1, as this is in contradiction with the acccepted (and correct) answer, and does not cite the standard. – Sam Hocevar Nov 23 at 10:36.
Sizeof(foo) tries really hard to discover the size of an expression at compile time: 6.5.3.4: The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer.
If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant. In short: variable length arrays, run at runtime. (Note: Variable Length Arrays are a specific feature -- not arrays allocated with malloc(3).) Otherwise, only the type of the expression is computed, and that at compile time.
Even though this was slightly after the accepted answer in submission time, it was close enough, and the answer probably took long enough to answer, and is correct, so you get a vote from me. – Paul Wagland Nov 29 at 8:59.
Sizeof is a compile-time builtin operator and is not a function. This becomes very clear in the cases you can use it without the parenthesis: (sizeof x) //this also works.
– Gaotter Nov 22 at 13:56 2 @Gaotter: yes, the extra parenthesis are ignored: codepad. Org/fhdjYhzV – André Paramés Nov 22 at 15:19 THIS is interesting. – surfasb Nov 23 at 6:25 But how is this an answer to the question?
– phresnel Nov 23 at 12:15 @phresnel: This is just to make it clear that sizeof is "weird" and is not subject to the rules of normal functions. I edited the post anyway to remove the possible confusion with normal runtime operators like (+) and (-) – missingno Nov 23 at 12:25.
The execution cannot happen during compilation. So ++i/i++ will not happen. Also sizeof(foo()) will not execute the function but return correct type.
The execution cannot happen during compilation. " what do you mean? – curiousguy Nov 23 at 0:58 Compilation will only create object code... The object code will be executed only when the user executes the binary.As sizeof happens at compile time assuming i++ will increment is wrong.
– rakesh Nov 23 at 7:35 "As sizeof happens at compile time" you mean: "as sizeof is a compile time constant expression"? – curiousguy Nov 23 at 8:18 Like "#define" happens during pre-processing, similarly sizeof will happen at compile time. During compilation all the type information is available so sizeof is evaluated then and there during compilation and value is replaced.
As already mentioned by @pmg "From the C99 Standard" before. – rakesh Nov 23 at 9:51 "sizeof will happen at compile time" for something that is not a variable length array – curiousguy Nov 23 at 23:49.
As the operand of sizeof operator is not evaluated, you can do this: int f(); //no definition, which means we cannot call it int main(void) { printf("%d", sizeof(f()) ); //no linker error return 0; } Online demo : ideone.com/S8e2Y That is, you don't need define the function f if it is used in sizeof only. This technique is mostly used in C++ template metaprogramming, as even in C++, the operand of sizeof is not evaluated. Why does this work?
It works because the sizeof operator doesn't operate on value, instead it operates on type of the expression. So when you write sizeof(f()), it operates on the type of the expression f(), and which is nothing but the return type of the function f. The return type is always same, no matter what value the function would return if it actually executes.In C++, you can even this: struct A { A(); //no definition, which means we cannot create instance!
Int f(); //no definition, which means we cannot call it }; int main() { std::cout.
X++ as far as I know (in programming concepts) does gives off the value first before incrementing . You should have tried ++x so it would increment the variable first before giving off the value.
I'm not the one who downvoted you, but I think this is because you didn't bother to try it yourself. Trying it with both expressions gives the same result. – OmarOthman Nov 24 at 9:02.
I may be wrong, because I'm not a c++ programmer. But it looks like you have used the postfix increment x++, you should use the prefix ++x if you want to see a change in the output. Go into debug mode, the number should still change to 6.
Or test it by putting the ++ in front of x. Once you have done that, consider checking some tutorials, I would recommend some, but since I'm not a c++ programmer I don't consider my suggestions valid.
3 Won't work: as the other answers explain, the expression isn't evaluated. Therefore the debugger won't see anything. Also, the question is about C.
C++ has similar but not identical rules for sizeof. – MSalters Nov 22 at 22:58 1 @Blue: you can test small snippets at ideone, for example ideone. Com/VhS33 – pmg Nov 22 at 23:12.
If you replace this code: printf("%d and ", sizeof(x++)); by : printf("%d and ", sizeof(++x)); then you will have the expected result. Sizeof(x++) means: operate sizeof(x) then increase x. Sizeof(++x) means: increase x, operate sizeof(x).
I'm not the one who downvoted you, but I think this is because you didn't bother to try it yourself. Trying it with both expressions gives the same result. – OmarOthman Nov 24 at 9:03 Welcome to Stack Overflow; while -4 might seem like a harsh welcome, please just use this opportunity to learn that it is always a good idea to (a) test your theories before before posting, if possible (b) clearly label guesses as guesses (c) include references to language specifications, format specifications, API specifications, etc., when they make sense (d) make simple examples to demonstrate issues.
I hope this helps you have an enjoyable time at Stack Overflow! :) – sarnold Dec 3 at 23:28.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.