* Vs ++ precedence in C

前端 未结 4 1183
名媛妹妹
名媛妹妹 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:25

    It is pretty simple.

    char * something = "something";
    

    Assignment of pointer.

    printf("%c\n", *something++);//equivalent to *(something++)
    

    Pointer is incremented but the value before increment is dereferenced ans it is post-increment.

    printf("%c\n", *something);//equivalent to *(something)
    

    Pointer is now pointing to 'o' after increment in the previous statement.

    printf("%c\n", *++something);//equivalent to *(++something)
    

    Pointer is incremented to point to 'm' and dereferenced after incrementing the pointer as this is pre-increment.

    printf("%c\n", *something++);//equivalent to *(something++)
    

    Same as the first answer. Also notice '\n' at the end of every string in printf. It makes the output buffer flush and makes the line print. Always use a \n at the end of your printf.

    You may want to look at this question as well.

提交回复
热议问题