How to use correctly the comments in C/++
Posted
by
Lucio
on Programmers
See other posts from Programmers
or by Lucio
Published on 2012-07-02T00:31:06Z
Indexed on
2012/07/02
3:23 UTC
Read the original article
Hit count: 324
I'm learning to program in C and in my stage, the best form to use correctly the comments is writing good comments from the beginning.
As the comments are not just for that one understands better the code but others too, I want to know the views of all of you to reach a consensus.
So what I want is that the most experienced users edit the following code as you please. (If it's unnecessary, delete it; If it's wrong, correct it; If needed, add more)
Thus there'll be multiple answers with different syntax and the responses with the most votes will be taken as referring when commenting.
The code to copy, paste and edit to your pleasure is:
(And I remark again, just import the comments, not the code)
/*
This programs find 1 number in 1 file.
The file is binary type and has integers in series.
The number is integer type and it's entered from the keyboard.
When finished the program, a poster will show the results:
Saying if the number is in the file or not.
*/
#include <stdio.h>
//FUNCTION 1
//Open file 'path' and closes it.
void openf(char path[]) {
int num;
//Read from Keyboard a Number and it save it into 'num' var
printf("Ready for read number.\n\nNumber --> ");
fflush(stdin);
scanf("%d",&num);
//Open file 'path' in READ mode
FILE *fvar;
fvar=fopen(path,"rb");
//IF error happens when open file, exit of function
if (fvar==NULL) {
printf("ERROR while open file %s in read mode.",path);
exit(1);
}
/*Verify the result of 'funct' function
IF TRUE, 'num' it's in the file*/
if (funct(path,fvar,num))
printf("The number %d it is in the file %s.",num,path);
else
printf("The number %d it is not in the file %s.",num,path);
fclose(fvar);
}
/*FUNCTION 2
It is a recursive function.
Reads number by number until the file is empty or the number is found.
Parameters received:
'path' -> Directory file
'fvar' -> Pointer file
'num' -> Number to compare
*/
int funct(char path[],FILE *fvar,int num) {
int compare;
//FALSE condition when the pointer reaches the end
if (fread(&compare,sizeof(int),1,fvar)>0)
/*TRUE condition when the number readed is iqual that 'num'
ELSE will go to the function itself*/
if (compare!=num) funct(path,fvar,num); else return 1;
else
return 0;
}
int main(int argc, char **argv)
{
char path[30]="file.bin"; //Direction of the file to process
openf(path); //Function with algorithm
return 0;
}
© Programmers or respective owner