-3
//Printing something from comment
#include <stdio.h>

int main()
{
  //This is a comment line \r printf("I am printed from comment");
  return 0;
}

I tried to come up with solution by adding various escape sequences present in C but it does'nt worked.

  • 7
    A comment is not parsed as code. Why do you need to do this? – Retired Ninja Apr 28 '21 at 22:38
  • 4
    They didn't think of questions like this when adding the minimum-size requirement for Answers – M.M Apr 28 '21 at 22:38
  • 5
    This has got to be an XY-Problem. What's the issue behind the question ? – Richard Critten Apr 28 '21 at 22:39
  • I don't think it is possible. Comments are for preventing things from being executed. So are you trying to find bugs in your compilers? – MikeCAT Apr 28 '21 at 22:41
  • Are you expecting a special syntax inside a comment? – Thomas Matthews Apr 28 '21 at 22:43
  • BTW, you have a `;` after the `#include` which is not necessary. – Thomas Matthews Apr 28 '21 at 22:43
  • As we can execute a comment in java using \u000d in the comment line why not in c – Himanshu Shekhar Apr 28 '21 at 22:43
  • 1
    @HimanshuShekhar: Because C and Java and C++ are different languages. Skipping comment content makes compiler writing easier. – Thomas Matthews Apr 28 '21 at 22:45
  • 1
    You can escape a comment that starts with `/*` by using `*/` to escape it and `/*` to resume it. You can escape a comment that starts with `//` by using a new-line character to escape it and `//` to resume it. – Eric Postpischil Apr 28 '21 at 22:45
  • @ThomasMatthews No I am just trying to print something from the comment line in the output screen. – Himanshu Shekhar Apr 28 '21 at 22:46
  • @MikeCAT This might be an bug , presently just exploring the scope of comment statements. – Himanshu Shekhar Apr 28 '21 at 22:47
  • @EricPostpischil I am trying to execute through comment. – Himanshu Shekhar Apr 28 '21 at 22:50
  • *I am trying to execute through comment*. Yes, you've said that a few times. But you have still not answered - **why**? If it should be executed why is it in a comment? And "because java can" is not a good reason. Why would you need to do that even in java? – kaylum Apr 28 '21 at 22:51
  • IMHO, using conditional compilation is a lot better than commenting code. For example, using `#if NDEBUG` or `#if LOGGING`. – Thomas Matthews Apr 28 '21 at 22:54
  • In C and C++ your desire is usually accomplished using some form of *logging*. Many logging functions write a sting to a file with a timestamp. Others write to `stdout`. An advantage to logging is that you get a receipt of how the program behaves; sometimes you can't get this using a debugger in debug mode (vs. optimized release) mode. – Thomas Matthews Apr 28 '21 at 22:57
  • How to execute a comment: First you start by saying, "Michael Corleone says hello." – user4581301 Apr 28 '21 at 22:59
  • This was asked here before (in another context) but same main idea https://stackoverflow.com/questions/28950718/do-comments-get-translated-to-machine-code-c `Comments are normally stripped out during preprocessing, so the compiler itself never sees them at all.` – Saifeddine Ben Salem Apr 28 '21 at 23:11
  • @SaifeddineBenSalem yes – Himanshu Shekhar Apr 28 '21 at 23:14

3 Answers3

3

Can we execute comment in C/CPP?

No, we cannot - at least not in any standard way. Content of comments are removed in a phase of translation and as such there is no way to access that content from within the program.

C++ Standard says (latest draft):

[lex.phases]

The precedence among the syntax rules of translation is specified by the following phases.

...

  1. The source file is decomposed into preprocessing tokens ([lex.pptoken]) and sequences of whitespace characters (including comments). A source file shall not end in a partial preprocessing token or in a partial comment. Each comment is replaced by one space character. New-line characters are retained. Whether each nonempty sequence of whitespace characters other than new-line is retained or replaced by one space character is unspecified. The process of dividing a source file's characters into preprocessing tokens is context-dependent.

...

user4581301
  • 33,082
  • 7
  • 33
  • 54
eerorika
  • 232,697
  • 12
  • 197
  • 326
0

comment is just used for remembering what the function do and the compiler does not compile it.

coderx64
  • 185
  • 1
  • 11
0

As others said comments can not be executed, the pre processor removes the line after the double slash.

However, you can do this in a psudo way with the /* */ comment style

So, Instead of

// This is a comment line \r printf("I am printed from comment");

Do

/* This is a comment line */ printf("I am printed from comment");

It isnt literally printed from the comment but you CAN have the printf visually on the same line as the comment.