Skip to main content
Third iteration. Added some context.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

NoteNote

This answer was merged from a duplicate, which explains the late date.

OriginalOriginal

Except for variable length arrays (VLA) sizeof does not evaluate its arguments. We can see this from the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 2 which says:

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.

A comment (now removed) asked whether something like this would evaluate at run-time:

sizeof(char[x++]); 

and indeed it would, something like this would also work (see them both live):

sizeof(char[func()]); 

since they are both variable length arrays. Although, I don't see much practical use in either one.

Note, variable length arrays are covered in the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

In C11C11, the answer changes for the VLA case, in certain cases it is unspecified whether the size expression is evaluated or not. From section 6.7.6.2 Array declarators which says:

[...]Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.

For example, in a case like this (see it live):

sizeof(int (*)[x++]) 

Note

This answer was merged from a duplicate, which explains the late date.

Original

Except for variable length arrays (VLA) sizeof does not evaluate its arguments. We can see this from the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 2 which says:

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.

A comment (now removed) asked whether something like this would evaluate at run-time:

sizeof(char[x++]); 

and indeed it would, something like this would also work (see them both live):

sizeof(char[func()]); 

since they are both variable length arrays. Although, I don't see much practical use in either one.

Note, variable length arrays are covered in the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

In C11, the answer changes for the VLA case, in certain cases it is unspecified whether the size expression is evaluated or not. From section 6.7.6.2 Array declarators which says:

[...]Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.

For example, in a case like this (see it live):

sizeof(int (*)[x++]) 

Note

This answer was merged from a duplicate, which explains the late date.

Original

Except for variable length arrays (VLA) sizeof does not evaluate its arguments. We can see this from the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 2 which says:

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.

A comment (now removed) asked whether something like this would evaluate at run-time:

sizeof(char[x++]); 

and indeed it would, something like this would also work (see them both live):

sizeof(char[func()]); 

since they are both variable length arrays. Although, I don't see much practical use in either one.

Note, variable length arrays are covered in the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

In C11, the answer changes for the VLA case, in certain cases it is unspecified whether the size expression is evaluated or not. From section 6.7.6.2 Array declarators which says:

[...]Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.

For example, in a case like this (see it live):

sizeof(int (*)[x++]) 
Second iteration. Removed historical information (that is what the revision history is for)—the question should be as if it was written right now; see e.g. <https://meta.stackexchange.com/a/131011>.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Note

This answer was merged from a duplicate, which explains the late date.

Original

Except for variable length arrays (VLA) sizeof does not evaluate its arguments. We can see this from the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 2 which says:

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.

A comment  (now removed) asked whether something like this would evaluate at run-time:

sizeof(char[x++]); 

and indeed it would, something like this would also work (Seesee them both live):

sizeof(char[func()]); 

since they are both variable length arrays. Although, I don't see much practical use in either one.

Note, variable length arrays are covered in the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Update

In C11, the answer changes for the VLA case, in certain cases it is unspecified whether the size expression is evaluated or not. From section 6.7.6.2 Array declarators which says:

[...]Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.

For example, in a case like this (see it live):

sizeof(int (*)[x++]) 

Note

This answer was merged from a duplicate, which explains the late date.

Original

Except for variable length arrays (VLA) sizeof does not evaluate its arguments. We can see this from the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 2 which says:

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.

A comment(now removed) asked whether something like this would evaluate at run-time:

sizeof(char[x++]); 

and indeed it would, something like this would also work (See them both live):

sizeof(char[func()]); 

since they are both variable length arrays. Although, I don't see much practical use in either one.

Note, variable length arrays are covered in the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Update

In C11 the answer changes for the VLA case, in certain cases it is unspecified whether the size expression is evaluated or not. From section 6.7.6.2 Array declarators which says:

[...]Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.

For example in a case like this (see it live):

sizeof(int (*)[x++]) 

Note

This answer was merged from a duplicate, which explains the late date.

Original

Except for variable length arrays (VLA) sizeof does not evaluate its arguments. We can see this from the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 2 which says:

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.

A comment  (now removed) asked whether something like this would evaluate at run-time:

sizeof(char[x++]); 

and indeed it would, something like this would also work (see them both live):

sizeof(char[func()]); 

since they are both variable length arrays. Although, I don't see much practical use in either one.

Note, variable length arrays are covered in the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

In C11, the answer changes for the VLA case, in certain cases it is unspecified whether the size expression is evaluated or not. From section 6.7.6.2 Array declarators which says:

[...]Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.

For example, in a case like this (see it live):

sizeof(int (*)[x++]) 
Introduced abbr. "VLA", etc.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Note

This answer was merged from a duplicate, which explains the late date.

Original

Except for variable length arrays (VLA) sizeof does not evaluate its arguments. We can see this from the draft C99C99 standard section 6.5.3.4 The sizeof operator paragraph 2 which says:

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.

A comment(now removed) asked whether something like this would evaluate at run-time:

sizeof( char[x++] ) ; 

and indeed it would, something like this would also work (See them both live):

sizeof( char[func()] ) ; 

since they are both variable length arrays. Although, I don't see much practical use in either one.

Note, variable length arrays are covered in the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Update

In C11 the answer changes for the VLA case, in certain cases it is unspecified whether the size expression is evaluated or not. From section 6.7.6.2 Array declarators which says:

[...]Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.

For example in a case like this (see it live):

sizeof( int (*)[x++] ) 

Note

This answer was merged from a duplicate, which explains the late date.

Original

Except for variable length arrays sizeof does not evaluate its arguments. We can see this from the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 2 which says:

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.

A comment(now removed) asked whether something like this would evaluate at run-time:

sizeof( char[x++] ) ; 

and indeed it would, something like this would also work (See them both live):

sizeof( char[func()] ) ; 

since they are both variable length arrays. Although, I don't see much practical use in either one.

Note, variable length arrays are covered in the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Update

In C11 the answer changes for the VLA case, in certain cases it is unspecified whether the size expression is evaluated or not. From section 6.7.6.2 Array declarators which says:

[...]Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.

For example in a case like this (see it live):

sizeof( int (*)[x++] ) 

Note

This answer was merged from a duplicate, which explains the late date.

Original

Except for variable length arrays (VLA) sizeof does not evaluate its arguments. We can see this from the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 2 which says:

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.

A comment(now removed) asked whether something like this would evaluate at run-time:

sizeof(char[x++]); 

and indeed it would, something like this would also work (See them both live):

sizeof(char[func()]); 

since they are both variable length arrays. Although, I don't see much practical use in either one.

Note, variable length arrays are covered in the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Update

In C11 the answer changes for the VLA case, in certain cases it is unspecified whether the size expression is evaluated or not. From section 6.7.6.2 Array declarators which says:

[...]Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.

For example in a case like this (see it live):

sizeof(int (*)[x++]) 
added 138 characters in body
Source Link
Shafik Yaghmour
  • 159.8k
  • 44
  • 466
  • 778
Loading
added 448 characters in body
Source Link
Shafik Yaghmour
  • 159.8k
  • 44
  • 466
  • 778
Loading
added 108 characters in body
Source Link
Shafik Yaghmour
  • 159.8k
  • 44
  • 466
  • 778
Loading
Post Merged (destination) from stackoverflow.com/questions/21995680/…
added 44 characters in body
Source Link
Shafik Yaghmour
  • 159.8k
  • 44
  • 466
  • 778
Loading
added 491 characters in body
Source Link
Shafik Yaghmour
  • 159.8k
  • 44
  • 466
  • 778
Loading
added 59 characters in body
Source Link
Shafik Yaghmour
  • 159.8k
  • 44
  • 466
  • 778
Loading
deleted 127 characters in body
Source Link
Shafik Yaghmour
  • 159.8k
  • 44
  • 466
  • 778
Loading
added 341 characters in body
Source Link
Shafik Yaghmour
  • 159.8k
  • 44
  • 466
  • 778
Loading
Source Link
Shafik Yaghmour
  • 159.8k
  • 44
  • 466
  • 778
Loading