C comments | c programming 4

 

Comments

Comments in C

In a C program, statements are used to add descriptive statements to the code without affecting the functionality of the program. Comments are ignored by the compiler, so they do not affect the behavior of the program, but they can be very helpful to other programmers who are reading the code.

 

C comments | c programming 4

 

C supports two types of statements: single-character statements and multi-character statements.

Single-line text begins with a leading colon (//) and continues to the end of the line. Example:
arduino.


1. Copy the code :-

// This is a single line of text
int x = 5; // This is another single line comment
Multiline text begins with a lead character followed by an asterisk (/*) and ends with an asterisk followed by an asterisk (*/). Example:
arduino.

2. Copy the code :-

/* This is information about multiple characters
spanning several lines */ .
int y = 10; /* This is another multi-line text */
It is important to use text sparingly, only for necessary explanations. Overstating comments can make code difficult to read and maintain, so it’s best to use comments only when necessary.
In addition to traditional statements, C also supports a special type of statement called the preprocessor directive. Preprocessor instructions start with a hash sign (#) and are used to provide instructions to the compiler before the code is compiled. Commonly used preprocessor directives include #include, used to include header files, and #define, used to define macros.
 
Comments can be singled-lined or multi-lined.

 


Single-line Comments

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the compiler (will not be executed).

This example uses a single-line comment before a line of code:

Example

// This is a comment
printf(“Hello World!”);

Try it Yourself »

This example uses a single-line comment at the end of a line of code:

Example

printf(“Hello World!”); // This is a comment

Try it Yourself »


C Multi-line Comments

Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by the compiler:

Example

/* The code below will print the words Hello World!
to the screen, and it is amazing */

printf(“Hello World!”);

Try it Yourself »

Single or multi-line comments?

It is up to you which you want to use. Normally, we use // for short comments, and /* */ for longer.

Good to know: Before version C99 (released in 1999), you could only use multi-line comments in C.


C Exercises

Test Yourself With Exercises

Exercise:

Comments in C are written with special characters. Insert the missing parts:

 This is a single-line comment
 This is a multi-line comment 


Start the Exercise

 

                                                       PREVIOUS NEXT

  C Comments Comments in C In a C program, statements are used to add descriptive statements to the code without affecting the functionality of the program. Comments are ignored by the compiler, so they do not affect the behavior of the program, but they can be very helpful to other programmers who are reading the…

  C Comments Comments in C In a C program, statements are used to add descriptive statements to the code without affecting the functionality of the program. Comments are ignored by the compiler, so they do not affect the behavior of the program, but they can be very helpful to other programmers who are reading the…

Leave a Reply

Your email address will not be published. Required fields are marked *