A C/C++ macro counter 
Author Message
 A C/C++ macro counter

I'm looking for a way to implement a counter using preprocessor
statements (macro) in C/C++, but so far I've failed.

What I need is to define a 'constant' (well, I don't want it to be constant
across the file) that when each time is included in the source code is
incremented by 1. It should begin at 1 (or 0) at the top of the file and
increase for each time it is used.

Example:

#ifdef OPTION1
  printf( "A - The counter is now %d\n", COUNTER );
#endif

/* Other code here */

#ifdef OPTION2
  printf( "B - The counter is now %d\n", COUNTER );
#endif

/* Other code here */

#ifdef OPTION3
  printf( "C - The counter is now %d\n", COUNTER );
#endif

Provided OPTION1 , OPTION2 and OPTION3 are defined, the last
printf would yield:

  C - The counter is now 3

If only OPTION3 is defined, we would get:

  C - The counter is now 1

Some notes:
1 - We cannot use some global variable and increase it, since
the macro COUNTER is used in different functions.

2 - I tried this:

#define COUNTER 0
/* other code */
#define COUNTER_TMP (COUNTER+1)
#undef COUNTER
#define COUNTER COUNTER_TMP
#undef COUNTER_TMP
printf( "The counter is now %d\n", COUNTER );

It did not work. The problem I think is that I want the compiler to resolve
COUNTER_TMP when used on the 3rd line from the end above, while it
does not attempt to resolve it until COUNTER is subsequently used.

I think the problem is general enough, and that someone probably has a
nice solution to it.


Thanks in advance !

Best Regards,
Arne Steinarson, Z-Soft
----------------------------------------------------
FileProbe - Ultimate File Management for Win32
http://www.*-*-*.com/  - Information and Downloads

--



Fri, 29 Mar 2002 03:00:00 GMT  
 A C/C++ macro counter


Quote:
>I'm looking for a way to implement a counter using preprocessor
>statements (macro) in C/C++, but so far I've failed.

>What I need is to define a 'constant' (well, I don't want it to be constant
>across the file) that when each time is included in the source code is
>incremented by 1. It should begin at 1 (or 0) at the top of the file and
>increase for each time it is used.

>Example:

>#ifdef OPTION1
>  printf( "A - The counter is now %d\n", COUNTER );
>#endif

>/* Other code here */

>#ifdef OPTION2
>  printf( "B - The counter is now %d\n", COUNTER );
>#endif

>/* Other code here */

>#ifdef OPTION3
>  printf( "C - The counter is now %d\n", COUNTER );
>#endif

>Provided OPTION1 , OPTION2 and OPTION3 are defined, the last
>printf would yield:

>  C - The counter is now 3

>If only OPTION3 is defined, we would get:

>  C - The counter is now 1

>Some notes:
>1 - We cannot use some global variable and increase it, since
>the macro COUNTER is used in different functions.

>2 - I tried this:

>#define COUNTER 0
>/* other code */
>#define COUNTER_TMP (COUNTER+1)
>#undef COUNTER
>#define COUNTER COUNTER_TMP
>#undef COUNTER_TMP
>printf( "The counter is now %d\n", COUNTER );

>It did not work. The problem I think is that I want the compiler to resolve
>COUNTER_TMP when used on the 3rd line from the end above, while it
>does not attempt to resolve it until COUNTER is subsequently used.

>I think the problem is general enough, and that someone probably has a
>nice solution to it.

How about this:
-------- counter.h -----
extern int count;
#define COUNTER count++

-------- counter.c -----
int count = 0;

-------- main_program.c -----
#include <stdio.h>
#include "counter.h"

int main()
{
  int i;
  for (i=0; i<3; i++)
    printf("Counter is %d\n", COUNTER);
  return 0;

Quote:
}

NOTE: Code is untested.
Quote:



Sorry, post here, read here.

Quote:

>Thanks in advance !

>Best Regards,
>Arne Steinarson, Z-Soft
>--


Bart v Ingen Schenau
--



Sat, 30 Mar 2002 03:00:00 GMT  
 A C/C++ macro counter

Quote:

> I'm looking for a way to implement a counter using preprocessor
> statements (macro) in C/C++, but so far I've failed.

There is no satisfactory way.

You can resort to kludges such as:

#if cond1
#define X1 1
#endif

#if cond2
#define X2 1
#endif

#if cond3
#define X3 1
#endif

#define COUNT (X1+X2+X3)

but as I said, that's not really satisfactory.

There are general-purpose macro processors such as M4 that can be
used for such tasks, or you could write a small C program that
generates the source code.
--



Sat, 30 Mar 2002 03:00:00 GMT  
 A C/C++ macro counter

Quote:


>>I'm looking for a way to implement a counter using preprocessor
>>statements (macro) in C/C++, but so far I've failed.

>>What I need is to define a 'constant' (well, I don't want it to be constant
>>across the file) that when each time is included in the source code is
>>incremented by 1. It should begin at 1 (or 0) at the top of the file and
>>increase for each time it is used.

>>Example:

>>#ifdef OPTION1
>>  printf( "A - The counter is now %d\n", COUNTER );
>>#endif

>>/* Other code here */

>>#ifdef OPTION2
>>  printf( "B - The counter is now %d\n", COUNTER );
>>#endif

>>/* Other code here */

>>#ifdef OPTION3
>>  printf( "C - The counter is now %d\n", COUNTER );
>>#endif

>>Provided OPTION1 , OPTION2 and OPTION3 are defined, the last
>>printf would yield:

>>  C - The counter is now 3

>>If only OPTION3 is defined, we would get:

>>  C - The counter is now 1

>>Some notes:
>>1 - We cannot use some global variable and increase it, since
>>the macro COUNTER is used in different functions.

>>2 - I tried this:

>>#define COUNTER 0
>>/* other code */
>>#define COUNTER_TMP (COUNTER+1)
>>#undef COUNTER
>>#define COUNTER COUNTER_TMP
>>#undef COUNTER_TMP
>>printf( "The counter is now %d\n", COUNTER );

>>It did not work. The problem I think is that I want the compiler to resolve
>>COUNTER_TMP when used on the 3rd line from the end above, while it
>>does not attempt to resolve it until COUNTER is subsequently used.

>>I think the problem is general enough, and that someone probably has a
>>nice solution to it.

>How about this:
>-------- counter.h -----
>extern int count;
>#define COUNTER count++

>-------- counter.c -----
>int count = 0;

>-------- main_program.c -----
>#include <stdio.h>
>#include "counter.h"

>int main()
>{
>  int i;
>  for (i=0; i<3; i++)
>    printf("Counter is %d\n", COUNTER);
>  return 0;
>}

>NOTE: Code is untested.

How is the above any better than just using count++?

But in any event, as he mentioned, he "cannot use some global variable
and increase it".

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- NOTE 4.2.42 BETAS NOW AVAILABLE

                *** WEB: http://www.comeaucomputing.com ***
--



Sun, 31 Mar 2002 03:00:00 GMT  
 A C/C++ macro counter


Quote:



>>>I'm looking for a way to implement a counter using preprocessor
>>>statements (macro) in C/C++, but so far I've failed.

>>>What I need is to define a 'constant' (well, I don't want it to be constant
>>>across the file) that when each time is included in the source code is
>>>incremented by 1. It should begin at 1 (or 0) at the top of the file and
>>>increase for each time it is used.

>>>Example:

>>>#ifdef OPTION1
>>>  printf( "A - The counter is now %d\n", COUNTER );
>>>#endif

>>>/* Other code here */

>>>#ifdef OPTION2
>>>  printf( "B - The counter is now %d\n", COUNTER );
>>>#endif

>>>/* Other code here */

>>>#ifdef OPTION3
>>>  printf( "C - The counter is now %d\n", COUNTER );
>>>#endif

>>>Provided OPTION1 , OPTION2 and OPTION3 are defined, the last
>>>printf would yield:

>>>  C - The counter is now 3

>>>If only OPTION3 is defined, we would get:

>>>  C - The counter is now 1

>>>Some notes:
>>>1 - We cannot use some global variable and increase it, since
>>>the macro COUNTER is used in different functions.

>>>2 - I tried this:

>>>#define COUNTER 0
>>>/* other code */
>>>#define COUNTER_TMP (COUNTER+1)
>>>#undef COUNTER
>>>#define COUNTER COUNTER_TMP
>>>#undef COUNTER_TMP
>>>printf( "The counter is now %d\n", COUNTER );

>>>It did not work. The problem I think is that I want the compiler to resolve
>>>COUNTER_TMP when used on the 3rd line from the end above, while it
>>>does not attempt to resolve it until COUNTER is subsequently used.

>>>I think the problem is general enough, and that someone probably has a
>>>nice solution to it.

>>How about this:
>>-------- counter.h -----
>>extern int count;
>>#define COUNTER count++

>>-------- counter.c -----
>>int count = 0;

>>-------- main_program.c -----
>>#include <stdio.h>
>>#include "counter.h"

>>int main()
>>{
>>  int i;
>>  for (i=0; i<3; i++)
>>    printf("Counter is %d\n", COUNTER);
>>  return 0;
>>}

>>NOTE: Code is untested.

>How is the above any better than just using count++?

>But in any event, as he mentioned, he "cannot use some global variable
>and increase it".

The OP wanted a global counter, but could not use a global variable.
The only reason for this I can think of, is because the functions are
in different source files.
This is how you would get a counter that is valid in multiple source
files.

I agree that it is not better than plain 'count++'.

Quote:
>- Greg

Bart v Ingen Schenau
--



Sun, 31 Mar 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. A C/C++ macro counter

3. counter macro

4. Macro counter problem

5. Need C++ text for non cs major course

6. How to show/call Form2.cs from Form1.cs ?

7. C line counter (bean counter)

8. Include code in other Cs files

9. Reuse of cs files, namespace, arch advice pls

10. word - automatic numbering/bold/underline/italics

11. How to Generate .cs file at Runtime

12. newbe/cs student, need help w/ code

 

 
Powered by phpBB® Forum Software