Line count function?

Discussion in 'Mac Programming' started by seriypshick, Oct 20, 2005.

  1. macrumors member


    Is there a function in C that would display the line count?

    Thanks, Serg.
  2. macrumors 601


    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).
  3. macrumors member


    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()}
  4. macrumors 6502a


    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.
  5. macrumors 601


    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
  6. Guest


    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.
  7. macrumors 6502a

    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.
  8. Moderator


    Staff Member

    If you can guarantee you are on a sensibly set up unix box why not shell out to 'wc -l'?
  9. macrumors 6502a

    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.
  10. macrumors member


    Thank's I think that's what I need.

Share This Page