0
 #include <stdio.h>
 int main(void) 
 {
    // your code goes here
    char *s = "Hello world!"; 
    printf("%d\n", sizeof(s=s+2)); 
    printf("%d\n", sizeof(++s)); 
    printf("%s\n",s);
    printf("%s\n",s=s+3);
    printf("%s\n",s++);
    printf("%s\n",++s);
    printf("%s\n",s++);
    printf("%s\n",s++);
    printf("%s\n",s++);
    printf("%s\n",s++);
    return 0; 
}

I know sizeof() operator takes operand as input(unary operator) and prints it's size.For eg. in case of pointers, it prints it's size say 4(based on machine),for data type it's respected size and so on for structure and union operands.But in above code sizeof(s++) and sizeof(s=s+2) isn't working as i expected.I thought next printf("%s",s) would print the given string after skipping few characters but it didn't. Isn't s incremented inside whensizeof(s=s+2) like in the later printf("%s",++s) statements.

Output
8
8
Hello world!
lo world!
lo world!
world!
world!
world!
orld!
rld!

What is wrong with this?

Sunil Kumar Jha
  • 841
  • 1
  • 8
  • 11
  • 3
    `sizeof` doesn't evaluate it's arguments, it just determines the size of the result. Hence the name. In other words, don't do that. – user3386109 Dec 05 '18 at 05:06
  • @user3386109 but isn't s++ implicitly increments s? – Sunil Kumar Jha Dec 05 '18 at 05:07
  • 10 printf and only 8 lines of output ? – ulix Dec 05 '18 at 05:08
  • i didn't snip first 8 – Sunil Kumar Jha Dec 05 '18 at 05:09
  • I think what user... says is that for any expression `e` of type `t` the expression `e` is completely thrown away, and you just compute `sizeof(t)`. –  Dec 05 '18 at 05:10
  • @dyukha yes exactly – Sunil Kumar Jha Dec 05 '18 at 05:11
  • 1
    Do not use `%d` to format `sizeof` values. Use `%zu`. – Eric Postpischil Dec 05 '18 at 05:11
  • 1
    @user3386109: The operand of `sizeof` is evaluated if it is a variable length array type. – Eric Postpischil Dec 05 '18 at 05:14
  • Is not `sizeof` evaluated at compile time? See [this answer](https://stackoverflow.com/a/2615205/2826535). Then it work as expected. It can not do something at runtime, since there is only a constant in the binary code. – Julo Dec 05 '18 at 05:25
  • @Julo: C 2018 6.5.3.4 2: “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.” – Eric Postpischil Dec 05 '18 at 12:31
  • @Julo: Also the answer you link to is for C++. This is a C question. – Eric Postpischil Dec 05 '18 at 14:30
  • @EricPostpischil as far as I know, there are things that are identical in C and C++. Are you saying, that the keyword `sizeof` is one that has different behaviour in C and C++? It is runtime evaluation in C? I know it is my mistake, since I did not have enough time to answer before I had to left to work. – Julo Dec 05 '18 at 16:23
  • @Julo: I quoted the C standard. That is authoritative. It says if the operand is a variable length array type, it is evaluated. The C++ answer is irrelevant to C, and, no, `sizeof` is not the same in C and C++, because C++ does not have variable length arrays. – Eric Postpischil Dec 05 '18 at 17:14

1 Answers1

1

sizeof(s=s+2) gives the size of the expression (s=s+2). (The “size” of an expression or type is the number of bytes used to represent a value of that type, including padding.)

(s=s+2) is s=s+2.

An assignment expression, besides setting the left operand to a value, has a value itself. That value is the value put into the left operand, and its type is the type of the left operand.

The type of s is char * (pointer to char).

So sizeof(s=s+2) is the size of a char *.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 1
    what do you mean by size of expression?size of variables it uses? – Sunil Kumar Jha Dec 05 '18 at 05:19
  • It'd likely be more appropriate to say "sizeof(s=s+2) gives the size of the type of the expression (s=s+2)" – nos Dec 05 '18 at 09:28
  • I'm only suggesting a wording that is easier for people to understand. The very next sentence in the C standard is " The size is determined from the type of the operand." – nos Dec 05 '18 at 12:57
  • Because the phrase "size of expression" is ambiguous. When someone not well versed in C see "sizeof s = s+1" , and are told sizeof yields the size of that expression, it is not at all clear to everyone what that means - even if it is obvious to you what it has to mean. Does it mean you get the code size of the expression ? Does it mean you get any temporary storage/stack size that expression might use ? – nos Dec 05 '18 at 13:15
  • @nos: I added a note about the meaning of the size of an expression or type. – Eric Postpischil Dec 07 '18 at 13:45