PDA

View Full Version : Line count function?




seriypshick
Oct 20, 2005, 06:49 PM
Is there a function in C that would display the line count?

Thanks, Serg.



HiRez
Oct 20, 2005, 07:36 PM
Is there a function in C that would display the line count?The line count of what? A file? An image? User input? If text you could just count the number of newline characters (which can be /r, /n, or /r/n).

seriypshick
Oct 20, 2005, 10:49 PM
The line count of what? A file? An image? User input? If text you could just count the number of newline characters (which can be /r, /n, or /r/n).
The souce file (Ex. test.c)

Basically what i want to do is to print line number plus some other info for every line, or after each ; (semicolon).

#define ; {printf("Line # is %i", line_number_function()}

NewbieNerd
Oct 21, 2005, 12:06 AM
Do many people do much Obj-C programming just because they own a Mac? Seems like a nice language though I haven't used it much.

HiRez
Oct 21, 2005, 12:51 AM
The souce file (Ex. test.c)

Basically what i want to do is to print line number plus some other info for every line, or after each ; (semicolon).

#define ; {printf("Line # is %i", line_number_function()}
You could do something like:int line_number_function() {
static int linenum;
return linenum++;
}...although if you are replacing semicolons with #defines then you've got the problem of having them substituted in that function as well so you need to be careful.

It sounds like you want your program to print out line numbers as it runs? Or am I misunderstanding what you're trying to do? If you just want to print out the file with line numbers prepended you could do it very easily in Terminal with:nl test.c

caveman_uk
Oct 21, 2005, 02:51 AM
Do many people do much Obj-C programming just because they own a Mac? Seems like a nice language though I haven't used it much.
I wouldn't do it if I didn't own a mac. But owning a mac doesn't mean you have to do programming in objective-C. There's plenty of other languages you could use and not all mac users are programmers (obviously). It's is a very nice development platform and the tools are free.

mrichmon
Oct 21, 2005, 04:07 AM
The souce file (Ex. test.c)

Basically what i want to do is to print line number plus some other info for every line, or after each ; (semicolon).

#define ; {printf("Line # is %i", line_number_function()}


This will not work since ";" is not a valid identifier for a #define.

Also, ";" is not a line delimiter it is a statement seperator so you will find that it is difficult to follow which particular line is outputing this statement.

I'd like to echo another posters comment that this is a very odd thing to be trying. I suspect you are hoping that this will make it easier to do debugging since your code would output line comments with every line. Unfortunately this stort of thing would be confusing to follow and output too much information. You are much better off trying to reason about what your program is doing, if necessary starting from the first line of the main method and stepping forward slowly. Getting familiar with a good debugger is also helpful. DDD although an X11 application is an excellent C debugger.

Having said all that here are two files that do almost what you want: (compile the line_number.c file first)


/*
* File: line_number.c
* Compile this file with gcc -c line_number.c
*/
int linenum = 0;

extern int line_number() {
return linenum++;
}



/*
* File: test.c
* Compile this file with gcc -o linenum test.c line_number.o
*/
#define LINNUM printf("Line # is %i\n", line_number());

extern int line_number();

main() {
int foo=0; LINNUM
while(foo<10) { LINNUM
foo++; LINNUM
}
}


Notice how normally you would not get output from the while statement line since there is no semi-colon used on the while statement. This is one example of how altering the meaning of ";" would not help you, even if it was a valid #define identifier.

robbieduncan
Oct 21, 2005, 04:28 AM
If you can guarantee you are on a sensibly set up unix box why not shell out to 'wc -l'?

AlmostThere
Oct 21, 2005, 06:25 AM
I don't know what the original poster is trying to achieve but no-one seems to have mentioned the standard C macro __LINE__, which just expands to the current line number.

seriypshick
Oct 21, 2005, 06:39 PM
I don't know what the original poster is trying to achieve but no-one seems to have mentioned the standard C macro __LINE__, which just expands to the current line number.
Thank's I think that's what I need.