a good idea

This is a discussion on a good idea within the C Programming forums, part of the General Programming Boards category; this is a very simple programe,but the last line "printf" is very clever. Code: main() { int a,b,c,count=0; printf("There are ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17

    Smile a good idea

    this is a very simple programe,but the last line "printf" is very clever.
    Code:
    main()
    {
        int a,b,c,count=0;
        printf("There are different methods for XM to distribute books to 3 readers:\n");
        for(a=1;a<=5;a++)
           for(b=1;b<=5;b++)
               for(c=1;a!=b&&c<=5;c++)
                   if(c!=a&&c!=b)
                      printf(count%4?"%2d:%d,%d,%d  " : "%2d:%d,%d,%d\n",++count,a,b,c);}
    do you think so?

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I believe that causes undefined behaviour. You don't know if
    Code:
    count%4?"%2d:%d,%d,%d  " : "%2d:%d,%d,%d\n"
    will be evaluated before
    Code:
    ++count
    In order to avoid undefined behaviour you could simply (starting count at 1)
    Code:
    printf("%2d:%d,%d,%d", count, a, b, c);
    printf(count++ % 4 ? "\n" : "   ");}
    Of course, the best way is for your code to be readable, not save lines. If you want to save lines in main() just make a function with the desired behaviour. I once made newl() that prints("\n"). You could make a newl(int) to simulate the above behaviour.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17

    Smile

    Quote Originally Posted by C_ntua View Post
    I believe that causes undefined behaviour. You don't know if
    Code:
    printf("%2d:%d,%d,%d", count, a, b, c);
    printf(count++ % 4 ? "\n" : "   ");}
    you are right
    thank you ! but it should in this way
    Code:
    printf("%2d:%d,%d,%d", count, a, b, c);
                printf(count++%4?"  " :"\n");

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by luoyangke View Post
    you are right
    thank you ! but it should in this way
    Code:
    printf("%2d:%d,%d,%d", count, a, b, c);
                printf(count++%4?"  " :"\n");
    Obfuscating your code will get you fired from most jobs.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by abachler View Post
    Obfuscating your code will get you fired from most jobs.
    You get paid to post here? I've been getting ripped off for all these years!


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I agree it is generally not a good idea. But, as always, it depends. For example this:
    Code:
    printf(count++%4?"  " :"\n");
    is less clear than this
    Code:
    if (count++ % 4) 
       printf("   "); 
    else 
       printf("\n");
    but the second needs 4 lines, which might make things worse.

    For example imagine a piece of code with a lots of ifs and elses. Adding one more which does a really minor thing might be worse than adding everything in one line. Because if you wanted to review your code for errors you would just read "prints something", since printf() is far less likely to cause errors. But if you add another if-then-else branch then you would read "if this print sth else print sth"

    So it depends from the overal code...

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    I think that the worst thing in that code isn't the printf line, it's the complete and utter lack of whitespace in lines such as:
    Code:
    for(c=1;a!=b&&c<=5;c++)
    Actually, the a != b part doesn't need to be evaluated each time through the loop either, so:
    Code:
    if (a != b)
        for (c = 1; c <= 5; c++)
    Fixed!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opinion on GOOD digicam
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-22-2003, 05:37 PM
  2. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  3. IDEA: Text Summarization
    By Perspective in forum Contests Board
    Replies: 2
    Last Post: 07-27-2003, 12:35 PM
  4. good or bad game idea you vote?
    By L_I_Programmer in forum Game Programming
    Replies: 3
    Last Post: 03-15-2003, 06:14 AM
  5. Good site to learn about Prog. language concept?
    By Extrovert in forum Tech Board
    Replies: 5
    Last Post: 03-13-2003, 01:46 AM

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