Problem : 6


#include <stdio.h>
#if something == 0
int some = 0;
#endif

int main ()
{
    int thing = 0;
    printf("%d %d\n", some, thing);
    return 0;
}

// What's the output ? [Do not execute / run]
// Source : http://www.youngprogrammer.com/
Advertisements

5 thoughts on “Problem : 6

  1. The C preprocessor modifies a source code file before handing it over to the compiler.
    There are a whole set of options that can be used to determine whether the preprocessor will remove lines of code before handing the file to the compiler. They include #if, #elif, #else, #ifdef, and #ifndef. An #if or #if/#elif/#else block or a #ifdef or #ifndef block must be terminated with a closing #endif.

    The #if directive takes a numerical argument that evaluates to true if it’s non-zero. If its argument is false, then code until the closing #else, #elif, of #endif will be excluded.

    ~ http://www.cprogramming.com/tutorial/cpreprocessor.html

  2. @anubhav jain
    I have explained the theory already .. here is an example

    #include <stdio.h>
    #if something != 0
    int some = 0;
    #endif
    
    int main ()
    {
        int thing = 0;
        printf("%d %d\n", some, thing);
        return 0;
    }
    

    this code will not compile

    because, if “something” not equals to zero then variable [int some] won’t get declared! (and assigned)

    simply saying, “something” is a global variable and assigned to zero by default
    and we are checking if something == 0 then declare int same = 0;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s