suppressing the compilation of the block of code

This is a discussion on suppressing the compilation of the block of code within the C Programming forums, part of the General Programming Boards category; Given something like the following Code: #include <stdio.h> #include <stdlib.h> int main( void ) { FILE *fp; int c1, char ...

  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
    India
    Posts
    515
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 06: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, 12: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-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21