locked
#define symbolic RRS feed

  • Question

  • Here's my question I cannot find it in my book so please help me thanks
    Define symbolic constant ALWAYS to have the value 1000.

    choices are /define
    define
    #define ALWAYS 1000
    #define ALWAYS <1000>
    please help me
    Sunday, April 26, 2009 11:19 PM

Answers

  • Circle 1 on your test paper.  It is the answer that accurately indicates your knowledge.

    Hans Passant.
    • Marked as answer by dougshell Monday, April 27, 2009 12:56 AM
    Monday, April 27, 2009 12:52 AM
  • Ermm, that wouldn't be a "symbolic constant", that's a preprocessor macro.  The 1st syntax is correct.  In C++, you'd use a declaration like this:

    const int Always = 100;

    Hans Passant.
    • Marked as answer by dougshell Monday, April 27, 2009 12:58 AM
    Sunday, April 26, 2009 11:49 PM
  • my choices are as followed

    1. /define ALWAYS "1000"

    2 define ALWAYS 1000

    3 #define ALWAYS 1000

    4 #define ALWAYS <1000>

    my question is What is the correct preprocessor directive for the following: Define symbolic
    constant ALWAYS to have the value 1000.
    • Marked as answer by dougshell Monday, April 27, 2009 12:59 AM
    Monday, April 27, 2009 12:45 AM

All replies

  • what is your question exactly ?

    Do you want to know what a define is ? what it does? and why you should use "static const int" instead ?

    GRZ A

    Sunday, April 26, 2009 11:49 PM
  • Ermm, that wouldn't be a "symbolic constant", that's a preprocessor macro.  The 1st syntax is correct.  In C++, you'd use a declaration like this:

    const int Always = 100;

    Hans Passant.
    • Marked as answer by dougshell Monday, April 27, 2009 12:58 AM
    Sunday, April 26, 2009 11:49 PM
  • my choices are as followed

    1. /define ALWAYS "1000"

    2 define ALWAYS 1000

    3 #define ALWAYS 1000

    4 #define ALWAYS <1000>

    my question is What is the correct preprocessor directive for the following: Define symbolic
    constant ALWAYS to have the value 1000.
    • Marked as answer by dougshell Monday, April 27, 2009 12:59 AM
    Monday, April 27, 2009 12:45 AM
  • Circle 1 on your test paper.  It is the answer that accurately indicates your knowledge.

    Hans Passant.
    • Marked as answer by dougshell Monday, April 27, 2009 12:56 AM
    Monday, April 27, 2009 12:52 AM
  • the #defigne is ment for the precompiler.
    so when you compile de precompiler will replace everwhere the Keword ALWAYS with the value nexst to it.

    Example:

    #define ALWAYS 1000
    
    int 2TimesALWAYS ()
    {
         return ALWAYS+ALWAYS;
    }

    when you compile the precompiler wil change the source code to this:

    int 2TimesALWAYS ()
    {
      return 1000+1000;
    }
    so when you sould definge ALWAYS diferent it would look like this:


    #define ALWAYS (1000)
    /* this is still correct becouse it wil become
    *  return (1000)+(1000);
    */
    
    #define ALWAYS "1000"
    /* this is a string and wil become this:
    *  return "1000"+"1000";
    *  And you cant return a string if it expecs to return a int
    */
    
    #define ALWAYS <1000>
    /*
    *  this can be uses(if you use Templates) but its not recomeced to 
    *  do it.
    *  in our simple function it wil become
    *  return <1000>+<1000>;
    *  And you probebly see that isn right and it wil give compile errors
    */
    

    i Suggest "Do not use define's"
    use a static const int instead it wil have the same effect

    /*in The .h File (header) */
    
    class Test
    {
    public:
    	Test();
    	virtual ~Test();
    
    private:
    
    static const int ALWAYS=1000;
    
    
    	 /*Disabling default copy constructor and assignment operator.*/
    	Test(const Test& t);
    	Test& operator=(const Test& t);
    };
    
    so nexst time consider using "static const int" for simple number defignes


    GRZ A.
    Monday, April 27, 2009 6:56 AM