Skip to main content

Section 3.6 Putting Comments in your Code

It is important to put comments throughout your code, both for purposes of readability of your code and so that you yourself can remember what you were thinking when you wrote the code. There are two ways to do so, both of which are demonstrated in the code below:

Compile and run the code below in your own C environment.

#include <stdio.h>

int main(void) {
    /* It is important to add comments to your code. */
    /* There are two types of comments. */

    /* This type of comment
       an go over multiple lines if you wish. */
       
    /* Or just one line - up to you! */
    
    // This type of comment only affects the remainder of a line.
    // Let's look at some examples.

    /* This program prints a greeting to the screen.
       It also tells you the day of the week. */
       
    printf("Hello, World!\n"); // print greeting.
    printf("Today is Monday!\n"); // print the day of the week
        
    /* Note that comments show up in your source code in a different color. */
    
    return(0);
}

Activity 3.6.

Try putting a comment inside another comment, so something like /*This is /*comment inside a */ comment */. What happens when you nest a comment inside another comment?