Thread: suppressing the compilation of the block of code

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    suppressing the compilation of the block of code

    Given something like the following

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
        FILE *fp;
        int c1,
        char c2;
    
        if ( ( fp = fopen( "test.txt", "r" ) == NULL )
        {
            fprintf( stderr, "Failed to open file\n" );
            return EXIT_FAILURE;
        }
    
        /* Read until nothing can be read anymore */
    
        while ( ( c1 = fgetc( fp ) ) != EOF )
            putchar( c1 );
    
    #if 0
        /* or, when using fscanf() */
    
        while ( fscanf( fp, "%c", &c2 ) == 1 )
            putc( c2 )
    
        /* or, when using fread() */
    
        while ( fread( &c2, 1, 1, fp ) == 1 )
            putc( c2 )
    #endif
    
        /* Check why the last read failed */
    
        if ( feof( fp ) )
            fprintf( stderr, "End of file detected\n" );
        else
            fprintf( stderr, "Read error\n" );
    
    #if 0
        /* or using ferror() */
    
        if ( ferror( fp ) )
            fprintf( stderr, "Read error\n" );
        else
            fprintf( stderr, "End of file detected\n" );
    #endif
    
        close( fp );
        return 0;
    }
    Is there a particular advantage to suppressing the compilation a block of code vs suppressing the execution of the block ?

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Yes, if the size of the executable is to be reduced. Also, some piece of code might require linking of other files which can also be removed by conditional compilation.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Is there a particular advantage to suppressing the compilation a block of code vs suppressing the execution of the block ?
    There is also perforamnce issue. If you leave a condition in the code - that is checked in runtime - it still takes some CPU cycles... Moreover - if you compiler is not too good - you will have possible branch misprediction on this condition check - that can greately decrease performance of the code...

    If this part of the code lies in some very frequently called function - it can result in big performance difference of the resulting code comaring to the code that has removed that part on the compilation stage
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM