C program to Find the Function Name of a C File

This C Program displays the function names defined in c source file.

Here is source code of the C Program to display the function names defined in c source file. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to Display the Function Names defined in C Source File
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. void check(char *c,int p1, int p2);
  8. void display(char *c, int p1);
  9.  
  10. void main(int argc, char **argv)
  11. {
  12.     FILE *fp;
  13.     char ch[100];
  14.     char *pos1, *pos2, *pos3;
  15.  
  16.     fp=fopen(argv[1], "r");
  17.     if (fp == NULL)
  18.     {
  19.         printf("\nFile unable to open");
  20.         return;
  21.     }
  22.     else
  23.         printf("\nFile Opened to display function names :\n");
  24.     while (1)
  25.     {
  26.         if ((fgets(ch, 100, fp)) != NULL)
  27.         {
  28.             if ((strstr(ch, "/*")) == NULL)
  29.             {
  30.                 pos1 = strchr(ch, '(');                /* check opening brace */
  31.                 if (pos1)
  32.                 {
  33.                     pos2 = strchr(ch,')');            /* check oclosing brace */
  34.                     if (pos2)
  35.                     {
  36.                         pos3 = strchr(ch,';');        /* check for semicolon */
  37.                         if ((pos1 < pos2) && (pos3 == NULL) || (pos3 < pos1))
  38.                         {
  39.                             check(ch, pos1 - ch, pos2 - ch);
  40.                         }
  41.                         else    continue;
  42.                     }
  43.                     else    continue;
  44.                 }
  45.                 else    continue;
  46.             }
  47.             else    continue;
  48.         }
  49.         else    break;
  50.     }
  51.     fclose(fp);
  52. }
  53.  
  54. /* To check if it is a function */
  55. void check(char *c, int p1, int p2)
  56. {
  57.     int i, flag = 0, temp = p1;
  58.  
  59.     if ((c[p1 + 1] == ')'))
  60.     {
  61.         display(c, p1);
  62.         return;
  63.     }
  64.     for (i = p1 + 1; i < p2; i++)
  65.     {
  66.         if ((c[i] != ' ') || (c[i] == ')'))
  67.         {
  68.             flag = 1;
  69.  
  70.         }
  71.         if (flag == 0)
  72.         {
  73.             display(c, p1);
  74.             return;
  75.         }
  76.         else
  77.         {
  78.             flag = 0;
  79.             while (c[--temp] != ' ');
  80.             for (i = 0; i < temp; i++)
  81.                 if (c[i]==' ')
  82.                 {
  83.                     flag = 1;
  84.                 }
  85.                 if (flag == 0)
  86.                 {
  87.                     display(c, p1);
  88.                     return;
  89.                  }
  90.                  else
  91.                      return;
  92.           }
  93.     }
  94. }
  95.  
  96. /* To display function name */
  97. void display(char *c,int p1)
  98. {
  99.     int temp = p1, i;
  100.  
  101.     while (c[--temp] != ' ');
  102.     for (i = temp + 1; i < p1; i++)            /* Print name of function character by character */
  103.         printf("%c", c[i]);
  104.     printf("\n");
  105.     return;
  106.  
  107. }

$ cc file9.c
$ a.out 
 
File Opened to display function names :
main
check
display

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.