problem : x1


#include <stdio.h>
#define loop( i, from, to ) \
     for( i = from; i <= to; i++ )

int main ()
{
    int i;
    loop ( i, 0, 3 )
        printf ("%d\n", i);
}

// what will be the output ? [Do not execute / run]
// Author : Gultu
Advertisements

11 thoughts on “problem : x1

  1. “#define” is always replaced in the code by its content.
    for example,

    #include <stdio.h>
    #define PI 3.1416
    
    int main ()
    {
        printf ("%lf\n", 4 * PI);
        return 0;
    }
    

    here, PI = 3.1416
    and output is : 4 * 3.1416

    if our code comprises with too many loops then we can use this technique.
    we defined ‘loop’ at line 2 & 3
    u may treat this as a function, [ though its not a function rather C pre-processor ]
    we call the ‘loop’ at line 8
    so, argument ‘from’ at line 3, gets the value = 0
    and ‘to’ = 3
    line 8 can be rewritten as,
    for ( int i = 0; i <= 3; i++ )

    for more information about C pre-processor u can follow this link :
    http://www.cprogramming.com/tutorial/cpreprocessor.html

    Correct Output :
    0
    1
    2
    3

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