Thread: why it is printing 7instead of 10

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    8

    why it is printing 7instead of 10

    Code:
    #define PR(a)   printf("a = %d\t",(int) (a));
    #define PRINT(a)  PR(a); putchar('\n');
    #define FUDGE(k) 	k + 3.14
    
    main()
    {
    	int x = 2;
    	PRINT( x * FUDGE(2));
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because (int)(2 * 2 + 3.14) == 7. Expand the macros and see it for yourself.

    This is one reason why you will often see "extra" parentheses used to define macros, e.g.,
    Code:
    #define FUDGE(k) ((k) + 3.14)
    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
    Sep 2006
    Posts
    8,868
    Your two line program eloquently shows the wayward path leading to code hell, of using damnable defines.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Adak View Post
    Your two line program eloquently shows the wayward path leading to code hell, of using damnable defines.
    Spacial place in hell for people that do that.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    For a certainty I would not want to come back in 5 years and have to read that code or update it.

    Defines are not for math!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Defines are not really for anything complex. If at all, they should be avoided.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Did I mention the lack of effort?
    what is the program output please explain
    I think I did.
    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.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Elysia View Post
    Defines are not really for anything complex. If at all, they should be avoided.
    Yep... about the only place I use them is to centralize constants...

    In a header I might use...

    #define MAX_TEXTBUFFER 256

    But that's about the extent of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Nearly finished, but a single problem remains.
    By jaja009 in forum C Programming
    Replies: 4
    Last Post: 03-26-2010, 04:41 PM
  3. Array help
    By Oklaskull in forum C Programming
    Replies: 19
    Last Post: 03-11-2008, 04:15 PM
  4. Printing an array in lines of 10.
    By furiousferret in forum C++ Programming
    Replies: 10
    Last Post: 11-16-2004, 07:30 PM
  5. working out
    By ZakkWylde969 in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 11-29-2003, 01:17 PM