Thread: C Preprocessing

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64

    C Preprocessing

    Hello
    Is there a program that can remove the functions in a program by preprocessing
    Code:
    #include<stdio.h>
    int is_even(int number);
    int main(void)
    {
    	if(is_even(number))
    	{
    		printf("%d is even", number);
    	}
    }
    
    
    int is_even(int number)
    {
    	return number%2 ==0
    }

    To something like


    Code:
    #include<stdio.h>
    int is_even(int number);
    int main(void)
    {
    	if( number%2 ==0 )
    	{
    		printf("%d is even", number);
    	}
    }

    I use the "-E" option when compiling with gcc on linux but its output still contains funcitons. Is there a preprocessing program that can remove functions from a program?
    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So to clarify, you want a program that will go through your source files and delete all function definitions with the exception of main? And it should leave all the prototypes, global and file scope variables, preprocessor directives (#include, #define, etc) alone? I find that a strange request, so if you could explain why you want to do that, perhaps we could suggest a better alternative.

    That being said, I don't know of anything that does exactly what you want, but perhaps if you look for some refactoring tools for C (or maybe even C++), you could find one that will do what you want. I personally have never used such tools, so I can't suggest any. Perhaps others can, or perhaps Google will help you.

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Code:
    #define PROVIDE_ISEVEN
    ...
    #ifdef PROVIDE_ISEVEN
    int is_even(int number)
    {
        return number%2 ==0
    }
    #endif
    EDIT: Wait, you want to change the code inside of main as well? In that case you can just define a function-like macro and use that instead:
    #define is_even(n) ((n) % 2 == 0)
    Last edited by Barney McGrew; 06-16-2013 at 07:29 PM.

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    There might not be something that specifically optimizes out the function in C, but when your compiler generates the machine code, it probably would be optimized out there.

    The compiler might see that instead of 2 jumps, it can just relocate that bit of logic. Also if you have code that will generate a numerical output in a deterministic way, you may see your function won't even make it into the machine code and your compiler has hard coded a value in the ultimate program.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by anduril462 View Post
    So to clarify, you want a program that will go through your source files and delete all function definitions with the exception of main? And it should leave all the prototypes, global and file scope variables, preprocessor directives (#include, #define, etc) alone? I find that a strange request, so if you could explain why you want to do that, perhaps we could suggest a better alternative.
    The reason i want such a program is that when reading through a source code, i have having to scroll up and down every time a function is called so i am looking for a program that can go through a source code and instead of putting function calls, it can replace it with the actual function itself

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,407
    Quote Originally Posted by acho.arnold
    The reason i want such a program is that when reading through a source code, i have having to scroll up and down every time a function is called so i am looking for a program that can go through a source code and instead of putting function calls, it can replace it with the actual function itself
    That is a very lousy reason. Functions provide some abstraction. Having the details exposed to you at each and every function call point will negate a significant advantage of having functions, i.e., once you understand what the function does, you can understand what the code does just by noting the function call, the arguments passed and the return value, without having to examine the implementation of the function.

    Furthermore, some text editors/IDEs may allow you to quickly navigate to a function definition and back again, so if you find this "having to scroll up and down every time" to be a problem, maybe you just need to use better tools or make better use of your current tools rather than drastically change your code with preprocessor or whatever other kind of magic.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Preprocessing #if
    By AnishaKaul in forum C Programming
    Replies: 2
    Last Post: 09-09-2010, 06:24 AM
  2. preprocessing
    By ganesh bala in forum C Programming
    Replies: 3
    Last Post: 02-18-2009, 03:24 AM
  3. C preprocessing
    By deepakwar in forum C Programming
    Replies: 7
    Last Post: 11-13-2007, 02:57 AM
  4. Windows GUI Preprocessing
    By Jaken Veina in forum Windows Programming
    Replies: 5
    Last Post: 07-22-2005, 01:22 PM
  5. Preprocessing C
    By trem in forum C Programming
    Replies: 3
    Last Post: 04-08-2002, 01:42 PM