Posted on June 29, 2010 by robbychen
I have not written a post since last Tuesday because I’m busy with other things which are not related to the IT. During that time, I’ve been thinking about change the posting schedule for my blog to twice a day instead of once a day in order to changing my procrastination habit. I will try to write a post in the morning and one in the afternoon beginning this Thursday. This post is talking about how to use comment in C. Let’s begin!
Most of you reading this post probably already experienced with comments in other programming languages. If you didn’t, comments are some human-readable characters that placed in the source code to make it more understandable to other humans. Comments can be any languages as long as it’s the same language as the target audience. They are ignored by the compilers / interpreters so comments can be a reference or documentation to the program.
Comments in C begin with /* and end with */ for multi-line comments, and begin with // for single-line comments. For example:
/*
* This is a multi-line comment
*/
// This is a single-line comment
Note that the single-line comment (//) is not available in the old C compilers. This type of comment is integrated from C++, therefore only the new C compilers that come after the existence of C++ supports the single-line comment.
The following source code shows the example of comments usage:
/*
* Comment.c
*
* A program used to demonstrate the use of comments
* by showing how to calculate the volume of a sphere
*
* by Robby Chen, 2010.
*
*/
#include <stdio.h>
main() {
double PI = 3.1415926536;Â Â Â Â Â Â // We need a lot of precision
int radius;
double volume;
printf("Please enter the radius: ");
scanf("%d", &radius);
/*
The volume of a sphere is:
four thirds times PI times the radius cubed
*/
volume = 4 / 3 * PI * radius * radius * radius;
printf("The volume of the sphere would be %fn", volume);
fflush(stdin);
getchar();
}
The above source code originally was to calculate the area of a circle. I modified it to calculate the volume of a sphere in order to avoid plagiarism.
I have not written a post since last Tuesday because I’m busy with other things which are not related to the IT. During that time, I’ve been thinking about change the posting schedule for my blog to twice a day instead of once a day in order to changing my procrastination habit. I will try to write a post in the morning and one in the afternoon beginning this Thursday. This post is talking about how to use comment in C. Let’s begin!
Most of you reading this post probably already experienced with comments in other programming languages. If you didn’t, comments are some human-readable characters that placed in the source code to make it more understandable to other humans. Comments can be any languages as long as it’s the same language as the target audience. They are ignored by the compilers / interpreters so comments can be a reference or documentation to the program.
Comments in C begin with /* and end with */ for multi-line comments, and begin with // for single-line comments. For example:
Note that the single-line comment (//) is not available in the old C compilers. This type of comment is integrated from C++, therefore only the new C compilers that come after the existence of C++ supports the single-line comment.
The following source code shows the example of comments usage:
/* * Comment.c * * A program used to demonstrate the use of comments * by showing how to calculate the volume of a sphere * * by Robby Chen, 2010. * */ #include <stdio.h> main() { double PI = 3.1415926536;Â Â Â Â Â Â // We need a lot of precision int radius; double volume; printf("Please enter the radius: "); scanf("%d", &radius); /* The volume of a sphere is: four thirds times PI times the radius cubed */ volume = 4 / 3 * PI * radius * radius * radius; printf("The volume of the sphere would be %fn", volume); fflush(stdin); getchar(); }The above source code originally was to calculate the area of a circle. I modified it to calculate the volume of a sphere in order to avoid plagiarism.