Search code examples
cfunctionctagsline-numbers

How to get the range (i.e., the line number) of all functions in a file in C?


I want to get both the beginning and ending line numbers of all functions in a file in C. Does any one know that whether there is a easy-to-use tool in Linux for this purpose?


Solution

  • $ ctags -x --c-kinds=f filename.c
    

    This only gives the starting line of each function, but perhaps that is good enough.

    If the code was written using fairly common conventions, the function should end with a single line containing } in the first column, so it is fairly easy to get the last line given the first:

    awk 'NR > first && /^}$/ { print NR; exit }' first=$FIRST_LINE filename.c