Thread: Preprocessor Functions

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Preprocessor Functions

    i just started a new c book and it mentioned using preprocessor functions, but it gave it a whole 2 pages. could i take something like this and make it a preprocessor function?

    Code:
    char* htmlstart(char title[]) {
    	static char start[] = "<HTML>\n\n<HEAD>\n\t<TITLE>";
    
    	strcat(start, title);
    	strcat(start, "</TITLE>\n</HEAD>\n\n</BODY>\n");
    
    	return start; 
    }

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    >>but it gave it a whole 2 pages

    Well I'd suggest you read those 2 pages first

    Even if you could, you wouldn't want to make that a preprocessor function, anyhow. If you really want to have the feeling of a preprocessor function (called Macros), then use an inline. It's faster than a normal function call, but it bloats up your program...

    I'm not a big fan of inline functions, though - I'd stick with what you have now.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    The basic idea is, if you want to program effectively, is to avoid macros unless you absolutely need them and only inline functions that are small (less than 4 lines).

    If you "inline" a function, you're suggesting the compiler should inline it. The compiler will make a logical choice, and usually won't inline functions that have a lot of instructions, loops, etc.

    As what KEN said what you have now is optimal, atleast in terms of function type (macro/inline/etc).

Popular pages Recent additions subscribe to a feed