Question on predefine macros

This is a discussion on Question on predefine macros within the C++ Programming forums, part of the General Programming Boards category; Hi , it's me again. I just came across predfine macros and was wondering how to use it so, I ...

  1. #1
    Programming Newbie Kaminaga's Avatar
    Join Date
    Jun 2005
    Posts
    14

    Question on predefine macros

    Hi , it's me again. I just came across predfine macros and was wondering how to use it so, I played around with one of the macros.

    Code:
    #include <iostream>
    #define __TIMESTAMP__
    using namespace std;
    
    
    int main()
    {
    	cout << "Start time: \n" << __TIMESTAMP__ << endl;
    
    	for(int x = 0; x < 5; x++)
    		cout << x;
    
    	cout << "End time is: \n" << __TIMESTAMP__ << endl;
    
     
    	return 0;
    }
    The output of the time will always remain the same what ever code I put between them so, is there a way to update the time to see some effect, or am I doing something wrong with the code i.e. the time of the code executing is too fast to see anything?

    Another question is, am I using the macros right? I didn't know how to output it in the first place but thought what the hell...try and cout the thing
    -Kaminaga
    "Temporary constructs of the feeble human intellect, trying desperately to justify it's meaning or purpose.." -Agent Smith
    kaminaga.deviantart.com

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    1) The __TIMESTAMP__ macro is the time that the file was last modified. What a macro is is just a placeholder. When you compile your file, the compiler will begin by replacing every instance of __TIMESTAMP__ with the modification time (that time doesn't change; it is determined once at the start of compilation).
    2) That macro is not part of C++ so if you use it be aware that you won't be able to compile it with all compilers.
    3) To determine elapsed time, #include <ctime> and call clock() to store the time you start your calculation then call it again at the end of your calculation. The difference divided by built-in macro CLOCKS_PER_SEC is the elapsed time. Here's some code:
    Code:
    #include <ctime>
    // ...
    clock_t start, end;
    double elapsed;
    
    start = clock();
    // do what you want to do (in living color)
    end = clock();
    elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
    // ...
    Do note, however, that the precision of this time is good but you might not see much elapsed time for such little code because it executes so rapidly. There are high resolutions timers available (depending on your compiler) but this method should suffice for your needs.
    Last edited by LuckY; 08-07-2005 at 10:06 AM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Both of you should carefully read the definition of __TIMESTAMP__.

    You also need to figure out what a statement like this does:

    #define name replacement

    and then what a statement like this does:

    #define name
    Last edited by 7stud; 08-07-2005 at 04:37 AM.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    My copy of the standard contains no mention of __TIMESTAMP__, I do have __DATE__ and __TIME__ though.

    [cpp.predefined] 16.8 Predefined macro names 1 The following macro names shall be defined by the implementation: _ _LINE_ _The line number of the current source line (a decimal constant). _ _FILE_ _The presumed name of the source file (a character string literal). _ _DATE_ _ The date of translation of the source file (a character string literal of the form "Mmm dd yyyy", where the names of the months are the same as those generated by theasctime function, and the first character of ddis a space character if the value is less than 10). If the date of translation is not available, an implementation-defined valid date is supplied. _ _TIME_ _ The time of translation of the source file (a character string literal of the form"hh:mm:ss" as in the time generated by theasctimefunction). If the time of translation is not available, an implementation-defined valid time is supplied. _ _STDC_ _Whether_ _STDC_ _ is predefined and if so, what its value is, are implementation-defined. _ _cplusplus The name_ _cplusplusis defined to the value199711Lwhen compiling a C + + translation unit. 136)
    Forgive the paste, Preview does not handle pdf's well.

    Another useful quote from the same section

    If any of the pre-defined macro names in this subclause, or the identifier defined, is the subject of a #defineor a#undefpreprocessing directive, the behavior is undefined.
    Now, in your code, you #define __TIMESTAMP__. If __TIMESTAMP__ were a standard macro why would you have to define it? It is alreayd defined, Hence the name 'pre-defined'.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    [edit]
    I realized my blunder. Pardon my mistatement.
    Last edited by LuckY; 08-07-2005 at 10:05 AM.

  6. #6
    Programming Newbie Kaminaga's Avatar
    Join Date
    Jun 2005
    Posts
    14
    Sorry if this post is long but I just wanted to make sure I can understand this better.

    Lucky:
    Thanks, I'm reading about it now in the MSDN library to understand more.

    7stud:
    I've read some of the stuff mentioned in the MSDN library, wasn't sure if I understand it properly but I'll give it ago.

    1. I guess when you ask to read about the definition, I'm assuming this is the important part
    "The date and time of the "last modification" of the current source file"
    2. For #define name replacement:
    The general form is
    #define identifier token-string or #define macro-name character sequence.
    So, if I made a
    Code:
    #define UP 1
    #define Down 0
    
    // and when I cout 
    
    cout << UP << ' ' << UP << ' ' << DOWN << ' '<< DOWN << ' ';
    
    //The output would be 1 1 0 0
    /* So basically, the statements would cause the compiler to subsitute 1 and 0 everytime UP and DOWN is used */
    3. For #define name
    if I do something like #define BLAH_H. It will continue to be defined. I guess this one is pretty useful when I'm creating a seperate header file and also use #ifndef and #endif.
    Again, correct me if I'm wrong

    Orbitz
    Sorry about that, I didn't check that one out thoroughly.
    -Kaminaga
    "Temporary constructs of the feeble human intellect, trying desperately to justify it's meaning or purpose.." -Agent Smith
    kaminaga.deviantart.com

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    3. For #define name
    if I do something like #define BLAH_H. It will continue to be defined. I guess this one is pretty useful when I'm creating a seperate header file and also use #ifndef and #endif.
    Again, correct me if I'm wrong
    That's not all that's going on. The replacement still occurs--even when you don't specify a set of replacement characters. If you don't specify a set of replacement characters, the name is replaced by nothing, which serves to remove the name from the file.
    Last edited by 7stud; 08-07-2005 at 03:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about macros
    By lazyme in forum C++ Programming
    Replies: 19
    Last Post: 04-27-2009, 10:10 PM
  2. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Macros question
    By anson1999 in forum C Programming
    Replies: 12
    Last Post: 03-08-2003, 09:30 PM

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