why it is printing 7instead of 10

This is a discussion on why it is printing 7instead of 10 within the C Programming forums, part of the General Programming Boards category; Code: #define PR (a) printf("a = %d\t",(int) (a)); #define PRINT(a) PR (a); putchar('\n'); #define FUDGE(k) k + 3.14 main() { ...

  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
    21,717
    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)
    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
    Sep 2006
    Posts
    8,868
    Your two line program eloquently shows the wayward path leading to code hell, of using damnable defines.

  4. #4
    msh
    msh is offline
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 06:30 PM
  5. working out
    By ZakkWylde969 in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 11-29-2003, 12:17 PM

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