Comments in C

HacksLand | The computer science playground

Posted by Thilan Dissanayaka on Aug 12, 2019

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.

Hi, I'm Thilan. An engineering student from SriLanka. I love to code with Python, JavaScript PHP and C.

Also read

Apr 16
Wordpress nulled theme checker

We all love free stuff. So many people try to install premium themes and plugins on there WordPress....

Nov 19
cookie consent banner javascript

Accordion to GDPR (General Data Protection Regulation) If we collect or save any kind of website....

Aug 12
SQL injection login bypass

SQL injection, The classical example of web application vulnerabilities. Actually the term SQL....