my teacher gave it to us....

This is a discussion on my teacher gave it to us.... within the C Programming forums, part of the General Programming Boards category; Hello. Our teacher gave us this piece of code: Code: #include <stdio.h> #define PR (a,f) printf (#a "= %#f"\t", (int)(a) ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    32

    my teacher gave it to us....

    Hello. Our teacher gave us this piece of code:

    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--)\
          putchar('*')
    
    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);
            putchar('\n');
        }
    }
    The answer is:
    Code:
    (x++<y ? y : x++)= 24   x= 24 y= 24
    (x++<y ? y : x++)= 25   x= 24 y= 24
    ********black***night
    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
    Location
    Singapore
    Posts
    21,632
    What is the question? What do you mean by "instructions to this program"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02:06 AM
  4. gave it shot...it gave me an error!
    By LonelyPlanderWa in forum C Programming
    Replies: 3
    Last Post: 07-12-2002, 01:26 AM

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