Friday, January 11, 2013

How to use comments in C

C Language: Comments

In the C Programming Language, you can place comments in your code that are not executed as part of the program. A comment starts with /* symbol and ends with */ and can be anywhere in your program. Comments can span several lines within your C program.

Syntax

The syntax for a comment is:
/* comment goes here */
or
// comment goes here

Example - Single Line

You can create an comment on a single line. For example:
/* Author: TechOnTheNet.com */
or
// Author: TechOnTheNet.com

Example - Spans Multiple Lines

You can create a comment that spans multiple lines. For example:
/*
 * Author: TechOnTheNet.com
 * Purpose: To show a comment that spans multiple lines.
 * Language:  C
 */
The compiler will assume that everything after the /* symbol is a comment until it reaches the */ symbol, even if it spans multiple lines within the C program.

Example - End of Code Line

You can create a comment that displays at the end of a line of code. For example:
#define AGE 6    /* This constant is called AGE */
or
#define AGE 6    // This constant is called AGE
In this example, the compiler will define a constant called AGE that contains the value of 6. Then it interprets everything after the /* symbol as a comment until it reaches the */ symbol.

No comments:

Post a Comment