* Vs ++ precedence in C

前端 未结 4 1224
名媛妹妹
名媛妹妹 2021-01-24 03:44

I am not able to understand the output of the following C code :

#include
main()
{
   char * something = \"something\";
   printf(\"%c\", *someth         


        
4条回答
  •  無奈伤痛
    2021-01-24 04:12

    // main entrypoint
    int main(int argc, char *argv[])
    {
        char * something = "something";
    
        // increment the value of something one type-width (char), then
        //  return the previous value it pointed to, to be used as the 
        //  input for printf.
        // result: print 's', something now points to 'o'.
        printf("%c", *something++);
    
        // print the characer at the address contained in pointer something
        // result: print 'o'
        printf("%c", *something);
    
        // increment the address value in pointer something by one type-width
        //  the type is char, so increase the address value by one byte. then
        //  print the character at the resulting address in pointer something.
        // result: something now points at 'm', print 'm'
        printf("%c", *++something);
    
        // increment the value of something one type-width (char), then
        //  return the previous value it pointed to, to be used as the 
        //  input for printf.
        // result: print 's', something now points to 'o'.
        printf("%c", *something++);
    }
    

    Result:

    somm
    

提交回复
热议问题