Imposing Line Numbers automatically in the C code

This is a discussion on Imposing Line Numbers automatically in the C code within the C Programming forums, part of the General Programming Boards category; Hi folks, Is it possible to impose line numbers automatically while writing C Code in Linux using "vi editor"...my requirement ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    36

    Imposing Line Numbers automatically in the C code

    Hi folks,

    Is it possible to impose line numbers automatically while writing C Code in Linux using "vi editor"...my requirement is the whenver I put a DEBUG message, I can say where exactly the control has reached.

    Code:
    #define DEBUG(arg...) \
                     printf(arg)
    
    int main()
    {
               //at line number 20
               DEBUG("I am in Line number : %d\n", Line Number());  // and it gives me the output as "I am in Line number : 20
    
              //similarly at line number 30
              DEBUG("I am in Line number : %d\n", Line Number());  // and it gives me the output as "I am in Line number : 30
    }
    Please let me know the way to implement the same or else if Linux already supports any such command or function which can tell me exact line number whenever I call that.

    Thanks,
    Cavestine

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Most compilers support the __LINE__ macro

    Code:
    DEBUG("I am in Line number : %d\n", __LINE__);
    Why not just use a debugger though?

    Edit: FYI: There are a few more, __FILE__ & __DATE__ etc, see your compiler manual.
    Last edited by zacs7; 10-12-2007 at 05:31 AM.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    ya thats gr8...
    is there any other way out instead of using __LINE__ macro...

    also what are the other system supported macros for the Linux which can be used...


    ya sure...i would definitely refer the manual...thanx...

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    thanx...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cavestine View Post
    ya thats gr8...
    is there any other way out instead of using __LINE__ macro...

    also what are the other system supported macros for the Linux which can be used...
    Sure, you can count the lines yourself - or write a little preprocessing program that replaces "$LINE$" with the line number - but what's the benefit of that. The __LINE__ macro is there for this particular purpose.

    Many compilers also support __function__ (gcc for example) which gives the name of the function. I think MS has a different macro, __FUNC__ or some such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You mean a pre-pre-processing program

    If you don't want to use __LINE__ use a debugger or turn on line numbers in vi and add them yourself like matsp suggested.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    hi matsp...thanx for ur point...but when I tried to know the function I am in, the macro __function__ is not working as expected...

    Code:
    #include <stdio.h>
    
    #define DEBUG(arg...)   \
                        printf(arg)
    
    void secondfunc()
    {
             DEBUG("I am in %s\n", __function__);
    }
    
    int main()
    {
             DEBUG("I am in %s\n", __function__);
             secondfunc();
    }
    Please let me know, is this the right way of using the __function__ macro. If possible, provide with an example

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    According to the manual GCC provides 2 macros relating to functions

    __PRETTY_FUNCTION__
    __FUNCTION__

    * Note the uppercase, the first is the function, it's type and the types of it's parameters while the latter is just the function name.
    Code:
    DEBUG("I am in &#37;s\n", __FUNCTION__);
    DEBUG("I am in pretty %s\n", __PRETTY_FUNCTION__);

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    ya thats gr8...

    but when I try both the MACROS, I get the same result...

    Code:
    #include <stdio.h>
    
    #define DEBUG(arg...)   \
                    printf(arg)
    
    void secondfunc()
    {
             DEBUG("I am in %s\n", __PRETTY_FUNCTION__);
    }
    
    int main()
    {
             DEBUG("I am in %s\n", __FUNCTION__);
             secondfunc();
    }
    the output which I am getting is:

    Code:
    I am in main
    I am in secondfunc
    are both the macros same??

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    secondfunc is void (no type) and has no arguments I'd assume that's why, or your compiler doesn't support __PRETTY_FUNCTION__ and instead aliased __FUNCTION__

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    even if I change the return type to int and accept few arguments, the output remains same...

    Code:
    #include <stdio.h>
    
    #define DEBUG(arg...)   \
                    printf(arg)
    
    int secondfunc(int x, int y)
    {
             DEBUG("I am in %s\n", __PRETTY_FUNCTION__);
             return 0;
    }
    
    int main()
    {
             DEBUG("I am in %s\n", __FUNCTION__);
             secondfunc(2, 3);
    }

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    Now to be very clear, I tried this but the output remained same...

    Code:
    #include <stdio.h>
    
    #define DEBUG(arg...)   \
                    printf(arg)
    
    int secondfunc(int x, int y)
    {
             DEBUG("I am in %s\n", __PRETTY_FUNCTION__);
             DEBUG("I am in %s\n", __FUNCTION__);
             return 0;
    }
    
    int main()
    {
             DEBUG("I am in %s\n", __PRETTY_FUNCTION__);
             DEBUG("I am in %s\n", __FUNCTION__);
             secondfunc(2, 3);
    }
    the output I received:

    Code:
    I am in main
    I am in main
    I am in secondfunc
    I am in secondfunc
    any idea y is it happennig...any conceptual issue??

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The __PRETTY_FUNCTION__ macro seems to work differently in C++ (printing whole function prototype) but same as __FUNCTION__ in C programs (with MingW).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    yes anon u r correct, in C++, the macro __PRETTY_FUNCTION__ gives the complete information about the function, including
    1. name of function
    2. return type and
    3. data types of its arguments

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    Hmmm...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Backgammon
    By glo in forum Game Programming
    Replies: 5
    Last Post: 10-02-2006, 10:26 PM
  2. Need help with reading numbers from the command line
    By Nterpol in forum C Programming
    Replies: 4
    Last Post: 06-01-2006, 01:40 AM
  3. Need the missing line of code
    By mayhem in forum C Programming
    Replies: 3
    Last Post: 06-20-2005, 04:21 PM
  4. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 03:53 PM
  5. Hotmail Hacked In One Line of code
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-01-2001, 09:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21