Can we write comments within variable names?

Vinita
:
int main()
{
     i/*nt*/a = 10;
     return 0;
}

If I have the above code and I want to count the tokens, will it be 14 or 13 tokens?

Is it valid to write a comment within a variable name? You can assume that the int i, int a, int ia are globally defined.

chqrlie
:

The comments are removed during phase 3 of program translation1: each comment is replaced by one space character. so the comment /*nt*/ is definitely not a token.

If none of int, main, i, a or return are defined as preprocessing macros, parsing the program produces 14 tokens (not 13):

int main ( ) { i a = 10 ; return 0 ; }

Unless i is defined as a type with a typedef statement, there is a syntax error as i a does not match a rule in the C grammar.

So you cannot write comments inside variable names, the comment splits the identifier into 2 separate tokens. This is true for any preprocessing and C language token2.

Note however that you can insert comments in unusual places such as between unary operators and their operand or between the # and the preprocessing directive and its arguments:

/**/#/**/include/**/<stdio.h>/**///////////////////////
/**/#/**/define/**/STAT/**/(/**/a/**/)/**/-/**/1/**////
/**/#/**/ifdef/**/STAT/**//////////////////////////////
/**/int/**/main/**/(/**/)/**/{/**//////////////////////
/**/int/**/a/**/=/**/+/**/1/**/;/**////////////////////
/**/printf/**/(/**/"Hello "/**/"world!\n"/**/)/**/;/**/
/**/return/**/STAT/**/;/**/////////////////////////////
/**/}/**///////////////////////////////////////////////
/**/#/**/endif/**//////////////////////////////////////

But the above macro definition does not define a function-like macro but a regular macro STAT that expands to ( a ) - 1.

Variable names, like any other token can be split by escaped newlines. Escaped newlines are sequences or \ immediately followed by a newline. These sequences are removed from the source code during phase 2 of program translation. Their main purpose is to break long macro definitions on multiple lines.

Below is a code fragment3 that produces the same 14 tokens:

\
i\
nt\
 ma\
in()
{\
i/\
*nt\
*/a \
= 10;
r\
et\
urn\
 0;}

Notice how the code colorizer missed the sliced and diced keywords and comment :)


1) This behavior was specified in ANSI-C aka C89. Some ancient compilers had subtly different behavior resulting in token pasting, but such peculiarities are of historical interest only.

2) You can almost insert a comment inside a string constant by taking advantage of the fact that adjacent string constants are concatenated in phase 6 of program translation: printf("Hello "/* my name is Luca */"world!\n");

3) This Christmas Tree presentation style is not meant to be used in real programs, it illustrates how to abuse C's input handling capabilities. More elaborate tricks have won The International Obfuscated C Code Contest

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can we set cursor as a session variable?

From Dev

can we undefine/unset a variable in makefile

From Dev

Can we write extension method for all methods?

From Dev

Why we can not define a variable twice in switch?

From Dev

How can we control if a variable exists or not in Java?

From Dev

Can we write functional tests using junit?

From Dev

Can we get variable that in Try from catch?

From Dev

Why is it that we can write outside of bounds in C?

From Dev

Can we write extensions for Visual Studio Code?

From Dev

Java - Can we declare object variable in constructor?

From Dev

Can we add php comments before declaring namespace

From Dev

Can we store echoed text in a variable in PHP?

From Dev

Can we write multiple queries in a MySQL event?

From Dev

jQuery: Can't we assign jQuery(this) to a variable?

From Dev

Can we initialise static variable inside constructor?

From Dev

Can we add comments on index in oracle?

From Dev

Can we write extension method for all methods?

From Dev

can we write to a word file using c?

From Dev

Can't write in global variable

From Dev

Can we assign names to buttons like we have in icons on a desktop?

From Dev

Bash - expanding variables names within variable names

From Dev

Can we write IF statement without else in javascript

From Dev

How can we add comments in flex/bison source code?

From Dev

How can write two statement within then statement?

From Dev

Can we use regex within sed

From Dev

Why can I edit object by calling their methods but not referencing their variable names from within a smaller scope

From Dev

In Java can we write "SuperClassObject instanceof SubClass"?

From Dev

How can we write a unit test for printing?

From Dev

Can we use ternary operator within an if statement?

Related Related

  1. 1

    Can we set cursor as a session variable?

  2. 2

    can we undefine/unset a variable in makefile

  3. 3

    Can we write extension method for all methods?

  4. 4

    Why we can not define a variable twice in switch?

  5. 5

    How can we control if a variable exists or not in Java?

  6. 6

    Can we write functional tests using junit?

  7. 7

    Can we get variable that in Try from catch?

  8. 8

    Why is it that we can write outside of bounds in C?

  9. 9

    Can we write extensions for Visual Studio Code?

  10. 10

    Java - Can we declare object variable in constructor?

  11. 11

    Can we add php comments before declaring namespace

  12. 12

    Can we store echoed text in a variable in PHP?

  13. 13

    Can we write multiple queries in a MySQL event?

  14. 14

    jQuery: Can't we assign jQuery(this) to a variable?

  15. 15

    Can we initialise static variable inside constructor?

  16. 16

    Can we add comments on index in oracle?

  17. 17

    Can we write extension method for all methods?

  18. 18

    can we write to a word file using c?

  19. 19

    Can't write in global variable

  20. 20

    Can we assign names to buttons like we have in icons on a desktop?

  21. 21

    Bash - expanding variables names within variable names

  22. 22

    Can we write IF statement without else in javascript

  23. 23

    How can we add comments in flex/bison source code?

  24. 24

    How can write two statement within then statement?

  25. 25

    Can we use regex within sed

  26. 26

    Why can I edit object by calling their methods but not referencing their variable names from within a smaller scope

  27. 27

    In Java can we write "SuperClassObject instanceof SubClass"?

  28. 28

    How can we write a unit test for printing?

  29. 29

    Can we use ternary operator within an if statement?

HotTag

Archive