Thread: Defined constants questions

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    18

    Defined constants questions

    Why does this code = 27 instead of 21

    7*7*7=21

    Code:
    #include <stdio.h>
    #define x 5+2
    main(){
    	int i;
    	i=x*x*x;
    	printf("%d",i);
    	getchar();
    	getchar();
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    5 + 2 * 5 + 2 * 5 + 2.
    This is what you said your code to do.
    Read more here.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Oh and also, 7 * 7 * 7 is not 7*3, but 7³ = 343, that's why you need to wrap your value in parenthesis like this,

    Code:
    #define x (5+2)
    This was a good question you made
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    18
    Haha, can't believe i didn't see the little math operations in that lol. Thank for the help

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are not the first one who did not, neither the last one

    You are welcome
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two questions on string constants
    By Niels_M in forum C Programming
    Replies: 3
    Last Post: 08-08-2010, 08:52 AM
  2. Struct with pre-defined constants, possible?
    By Subsonics in forum C Programming
    Replies: 6
    Last Post: 02-06-2009, 05:18 AM
  3. More questions about constants
    By R.Stiltskin in forum C++ Programming
    Replies: 31
    Last Post: 01-03-2008, 03:05 PM
  4. Displaying all #defined constants?
    By cpjust in forum C Programming
    Replies: 5
    Last Post: 10-30-2007, 10:15 AM
  5. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM