Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
seriypshick said:
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

macrumors member
Original poster
Apr 4, 2005
76
0
Everywhere
HiRez said:
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).
Code:
#define ; {printf("Line # is %i", line_number_function()}
 

NewbieNerd

macrumors 6502a
Sep 22, 2005
512
0
Chicago, IL
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

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
seriypshick said:
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).
Code:
#define ; {printf("Line # is %i", line_number_function()}
You could do something like:
Code:
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:
Code:
nl test.c
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
NewbieNerd said:
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

macrumors 6502a
Jun 17, 2003
873
3
seriypshick said:
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).
Code:
#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)

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

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

Code:
/*
 * 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.
 

seriypshick

macrumors member
Original poster
Apr 4, 2005
76
0
Everywhere
AlmostThere said:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.