C Comments

Learn how to use single-line and multi-line comments in C with clear examples.

C में single-line और multi-line comments का उपयोग करना सीखें।

Comments in C are used to make the code more readable and to add notes for developers. They are ignored by the compiler.

C में comments का उपयोग कोड को readable बनाने और programmer के लिए notes लिखने के लिए होता है। ये compiler द्वारा ignore किए जाते हैं।


Example 1: Single-line comment

#include <stdio.h>

int main() {
    // This is a single-line comment
    printf("Hello World!");
    return 0;
}

A single-line comment starts with `//` and explains a line of code.

`//` से शुरू होने वाला comment एक लाइन के लिए उपयोग होता है।

Output: Hello World!


Example 2: Multi-line comment

#include <stdio.h>

int main() {
    /* This is a
       multi-line comment */
    printf("Hello again!");
    return 0;
}

Multi-line comments start with `/*` and end with `*/`.

Multi-line comment `/*` से शुरू और `*/` से खत्म होता है।

Output: Hello again!


Example 3: Comment after code

#include <stdio.h>

int main() {
    int x = 5; // Declare variable
    printf("%d", x);
    return 0;
}

You can place comments after code on the same line.

आप comment को कोड के बाद एक ही लाइन में लिख सकते हैं।

Output: 5


Example 4: Comment to disable code

#include <stdio.h>

int main() {
    // printf("This is hidden!");
    printf("Only this prints.");
    return 0;
}

Use comments to temporarily disable code lines.

कोड को अस्थायी रूप से बंद करने के लिए comment करें।

Output: Only this prints.


Example 5: Multiline explanation for logic

#include <stdio.h>

int main() {
    /* Calculate area
       of rectangle */
    int length = 5, width = 3;
    int area = length * width;
    printf("Area: %d", area);
    return 0;
}

Use multi-line comments to explain blocks of logic.

Logic को समझाने के लिए multi-line comment का उपयोग करें।

Output: Area: 15


Example 6: Empty comments

#include <stdio.h>

int main() {
    // 
    printf("Blank comment above.");
    return 0;
}

Empty comments don’t affect output.

खाली comments आउटपुट को प्रभावित नहीं करते।

Output: Blank comment above.


Example 7: Nested comment (Invalid)

#include <stdio.h>

int main() {
    /* Outer comment
    /* Inner comment */
    printf("Invalid nesting!");
    return 0;
}

Nested comments are not allowed in C and cause error.

C में nested comments allowed नहीं हैं, error आएगा।

Output: Compiler error


Example 8: Comment before declaration

#include <stdio.h>

int main() {
    // Declaring a variable
    int x = 100;
    printf("%d", x);
    return 0;
}

Use comments before declaring important variables.

महत्वपूर्ण variables के पहले comments helpful होते हैं।

Output: 100


Example 9: Use comments for debugging

#include <stdio.h>

int main() {
    int a = 5, b = 0;
    // int c = a / b;
    printf("Debugging step");
    return 0;
}

Use comments to temporarily skip faulty logic.

गलत logic को skip करने के लिए comment करें।

Output: Debugging step


Example 10: Comment function purpose

#include <stdio.h>

// Main function starts here
int main() {
    printf("Function comment");
    return 0;
}

You can describe function purpose above its declaration.

Function के ऊपर उसका purpose comment में लिखा जा सकता है।

Output: Function comment