Thread: my teacher gave it to us....

  1. #1
    Registered User
    Join Date
    Oct 2009

    my teacher gave it to us....

    Hello. Our teacher gave us this piece of code:

    #include <stdio.h>
    #define PR(a,f) printf (#a "= %#f"\t", (int)(a) )
    #define PRINT(a,f) PR(a,f); putchar ('/n')
    #define PRINT2(a,b,f1,f2) PR(a,f1); PRINT(b,f2)
    #define PRINT3(a,b,c,f1,f2,f3)   \
                           PR(a,f1); PRINT2(b,c,f2,f3)
    #define MAX(a,b) (a<b ? b : a)
    #define TAB(c,i,oi,t) if(c=='\t')\
          for (t=8-(i-oi-1)%8, oi=i; t; t--)\
    void main (void)
            int x=19, y=20;
            PRINT3 ( MAX(x++,y), x, y ,d,d,d);
            PRINT3 ( MAX(x++,y), x, y ,d,d,d);
            static char input[] = "\tlight\tweight";
            char c;
            int i, oldi, temp;
            for (oldi=-1, i=0; (c=input[i])!='\0'; i++)
            TAB (c, i, oldi, temp);
            else putchar(c);
    The answer is:
    (x++<y ? y : x++)= 24   x= 24 y= 24
    (x++<y ? y : x++)= 25   x= 24 y= 24
    The problem is that we were given it very lately and he didn't even start explaining it. He said that we had very little time (which is true) and we should 'figure it out' by ourselves. I have no idea what's going on there and I know that majority of my classmates (and all of those who I asked) are in the same position as me. I don't know how it may learn us C, but we are given points by him and we must survive it.

    Could any of you post some instructions to this program? At least 15 people will benefit from this.

    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    What is the question? What do you mean by "instructions to this program"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    I mean how this program outputs that answer.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > I don't know how it may learn us C
    That's OK, neither do I.

    A lot of very bad teachers (yours seems to be one of them) seem to think throwing random bits of crap code about (which this is some) is in some way useful.

    For info, this should be filed under "stupid pre-processor tricks".

    If you want to find out what happens, then look at
    gcc -E prog.c
    which should IIRC create a prog.i file which is your .c file with all the #define stuff expanded into real code.

    This kind of meta-programming using all sorts of pre-processor tricks is occasionally useful and sometimes a necessary evil. But only after you've been programming for a long while, and are writing quite large programs.

    Trying to teach it to students is a waste of effort. You're not likely to use that information for a good while, and when/if you do, you'll have a lot better idea as to what it is you're trying to solve.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I like C and C++, i just dont like my teacher teaching it...
    By jazzglenn421 in forum C++ Programming
    Replies: 11
    Last Post: 09-09-2009, 11:37 AM
  2. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  3. cpp help, not even the teacher can figure this out!
    By Kynan_BeetleBug in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2003, 03:06 AM
  4. gave it gave me an error!
    By LonelyPlanderWa in forum C Programming
    Replies: 3
    Last Post: 07-12-2002, 01:26 AM