Aug 12, 2019

Comments in C

Every programming language has comments .In python tutorial we used # for indicating a comment.

So take a minute and think why we need commenting ? Thee are several reasons.

1) make clear what we do.

We can't remember everything we do. Actually in some times I can't remember what I eat for breakfast. So when we think about programs we write, after some days we can't figure out how you build your tools .You may think why I used a malloc() , why I used a for loop and why didn't choose a while loop etc.

So the solution is comments. Yes you may write a short line about explaining what you did. Also we can use comments for divide different parts of your code specifically if it's too long .Not only comments helpful to you for remember things. When you are contributing to an open source community they will admire you more and more if everything is clear and well commented.

2) Debug your code.

In sometimes you need to temporarily disable some parts of your code.It is needed when testing new features or code gives you errors. So the smartest way is commenting the unwanted part of your code. So when your work is over you can in comment that part and continue your work.

Let' see how we can make a comment in C. The first example is about clearing the code for understand.

int main() { 
  char buff[20]; /*hear we declared a character buffer that length is 20 bytes*/
  /*next we use gets function 
  for fill data into the buffer. 
  We did not any bound checking hear. 
  It'll lead to a buffer overflow*/
  puts(buff); /*finally we out put the data.*/

You can see that even a beginner can get a raw idea about what's going hear.

Above example is coming from exploitation tutorials so we used gets() function for getting input. It is not safe to use that function on your real life codes.

Next see how can we use commenting for debugging purposes.

Assume that I have wrote following code. I want to find out the actual memory address of the variable x. Hear I have used a pointer and pleas don't worry about it for now

#include <stdio.h>
int x= 1;  
int *ptr; 
int main() {  
  ptr = &x;     
  printf("address of x variable is %d\n" ptr);     
  return 0; 

Now when I compiling this I get an error.

So I think error is coming from printf function. So I want to disable it for a while and check if error is gone or not.

I do it like this.

#include <stdio.h>
int x;  
int *ptr; 
int main() {  
  ptr = &x;     
  printf("address of x variable is %d\n" ptr);     
  return 0; 

OK I hope it is clear now.

(Yes error is in printf, we did not used format string correctly)

Taking the advantage of C++ comments.

In C++ you can comment in code using two slashes. While modern compilers are support both of C and C++ you can comment using thus method also.Let's see quick example.

int main(){
printf("It's going to rain\n") ; // a  quote inside string but no error.

So guys I hope you understand this tutorial well. C you again in next post.

May 04
C programming strings

In our "Manipulating data with C" article we saw how we can store data in memory. We used the....

Oct 17
Command line arguments in C

Hi guys, In this document we are going to see how we can use command line argumentsin C programs .....

Aug 12
Outputs in C

In C there are number of ways to getting output to the screen.You can use them according to your....

Replying to 's comment Cancel reply
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.