You're online now.

Hurray! you are online now.

A C program for checking whether a given line is a comment

Code comments are non-executable instruction that programmers can use provide documentation. Single-line comments describe a single line in code.

Program comments can be placed anywhere.

  1. A single line comment can place anywhere in the program.
  2. Comments lines start with ‘//’.
  3. Compilers ignore symbols written after ‘//’.

Multi-line comment

  1. Anywhere can be used for a multi-line comment
  2. Multi-line comments starts with ‘/*’ and ends with ‘*/’.
  3. Any symbols written between ‘/*’ and ‘*/’ are ignored by compiler.
  4. Multiple lines can be use for comments.
// Method 1: To detect the comment
    #include <stdio.h>
    int main() {
        
        char comment[50];
        int i = 2, a = 0;
        
        printf("Enter Comment : ");
        gets(comment);
        if(comment[0] == '/'){
            if(comment[1] == '/'){
                printf("This is Single Line Comment.");
            }
            else if(comment[1] == '*'){
                for(i = 2; i <= 50; i++){
                    if(comment[i]=='*' && comment[i + 1] == '/'){
                        printf("This is Multi Line Comment.");
                        a = 1;
                        break;
                    }
                    else{
                        continue;
                    }
                }
                if(a == 0){
                    printf("It is not a comment");
                }
            }
            else{
                printf("It is not a comment");
            }
        }
        else{
            printf("It is not a comment");
        }
        
        return 0;
    }
    
// Method 2: To check the comment
    #include<stdio.h>
    #include<string.h>
    
    void main(){
        char com[30];
        int len;
        printf("\nEnter Comment : ");
        gets(com);
        len = strlen(com);
        if(com[0] == '/' && com[1] == '/'){
            printf("It is a Single Line Comment");
        }
        else if(com[0] == '/' && com[1] == '*' && com[len - 1] == '/' && com[len - 2] == '*' ){
            printf("It is a Multi Line Comment");
        }
        else{
            printf("It is not a comment");
        }
    }
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...