Search code examples
cputchar

Can anyone explain what the code putchar('/' //*/ 1) mean


I was doing the exercise of the K&R2. When i was reading the code by Ben Pfaff in this page http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_23 I coudn't understand what the single code putchar('/' //*/ 1) mean. While in my compiler, it is a syntax error. So can anyone explain this to me.


Solution

  • If you read the comments at the beginning of the solution it explains why you're seeing that error:

    It also contains examples of a comment that ends in a star and a comment preceded by a slash. Note that the latter will break C99 compilers and C89 compilers with // comment extensions.

    In a compiler that does not support // style comments, this:

    putchar('/' //**/ 
            1) 
    

    Is equivalent to:

    putchar('/'/1)
    

    Which is legal -- though odd -- expression (remember that in C a char is a numeric type, so '/'/1 is the same as /). This happens because the sequence /**/ is an empty comment.

    In a modern compiler with // style comments, the expression ends up being equivalent to:

    puchar('/' 1)
    

    Which is simply an error.