Macro for declaration

S

SRR

Can anyone tell me if it is really possible to do declare 100
variables without "explicitly typing it"?
Consider the following:

int var1=0;
int var2=0;
...........
............
int var99=0;
int var100=0;

The job is to write a "short" macro which when used, pastes the above
declarations in the code.
 
A

ais523

Can anyone tell me if it is really possible to do declare 100
variables without "explicitly typing it"?
Consider the following:

int var1=0;
int var2=0;
..........
...........
int var99=0;
int var100=0;

The job is to write a "short" macro which when used, pastes the above
declarations in the code.

The easiest way to do it is probably
int var[100]={0};
where you declare an array rather than 100 variables. Whenever you
would use one of your 100 variables, you can just write an extra pair
of square brackets to get the same effect; and you get the added
benefit of being able to do loops over the variables without having to
write everything out by hand. (I don't thing it's possible to do what
you originally with a '"short"' macro, although it's definitely
possible to do it with a set of macros shorter than what it would take
to do the whole thing by hand.)
 
F

Flash Gordon

SRR wrote, On 26/02/07 15:00:
Can anyone tell me if it is really possible to do declare 100
variables without "explicitly typing it"?
Consider the following:

int var1=0;
int var2=0;
..........
...........
int var99=0;
int var100=0;

The job is to write a "short" macro which when used, pastes the above
declarations in the code.

Why do you need 100 variable rather than 1 array or 100 elements? It
seems like a stupid requirement to me.
 
E

Eric Sosman

SRR wrote On 02/26/07 10:00,:
Can anyone tell me if it is really possible to do declare 100
variables without "explicitly typing it"?
Consider the following:

int var1=0;
int var2=0;
..........
...........
int var99=0;
int var100=0;

The job is to write a "short" macro which when used, pastes the above
declarations in the code.

It's certainly possible, given a suitably lenient
definition of "short."

Under any definition, though, it's a stupid thing
to do. Given those declarations, how will you use the
hundred declared variables? Answer: For each operation
you want to perform you will need to write a hundred
versions of the statement that performs it, plus some
more code to choose which of the hundred statements you
want to execute. You will type your little fingers to
the bone. You will make at least two typographical
errors, at least one of which will escape the notice of
the compiler, e.g.

switch (which) {
case 1: var1 = x; break;
case 2: var2 = x; break;
...
case 85: var85 = x; break;
case 86: var85 = x; break;
...
case 99: var99 = x; break;
case100: var100 = x; break;
}

.... and you will have made far more trouble for yourself
than I would wish on anyone.
 
C

Chris Dollin

SRR said:
Can anyone tell me if it is really possible to do declare 100
variables without "explicitly typing it"?
Consider the following:

int var1=0;
int var2=0;
..........
...........
int var99=0;
int var100=0;

The job is to write a "short" macro which when used, pastes the above
declarations in the code.

Suppose the answer was "yes". What would you really have gained?

Suppose the answer was "no". What would you have lost?

Suppose the answer was "yes, for some value of 'short', but the
result is an unmaintainable and pointless mess." What would you
have learned?
 
S

santosh

SRR said:
Can anyone tell me if it is really possible to do declare 100
variables without "explicitly typing it"?
Consider the following:

int var1=0;
int var2=0;
..........
...........
int var99=0;
int var100=0;

The job is to write a "short" macro which when used, pastes the above
declarations in the code.

How are you going to use them then? Write another set of macros?

Forget such nonsense and use arrays. That's what they're there for.
 
M

mark_bluemel

Can anyone tell me if it is really possible to do declare 100
variables without "explicitly typing it"?
Consider the following:

int var1=0;
int var2=0;
..........
...........
int var99=0;
int var100=0;

The job is to write a "short" macro which when used, pastes the above
declarations in the code.

Why would you want to do that, when it seems fairly clear that what
you need here is an array?

Are you also looking for a "short" macro to generate the accesses to
these variables?
 
F

Fred Kleinschmidt

SRR said:
Can anyone tell me if it is really possible to do declare 100
variables without "explicitly typing it"?
Consider the following:

int var1=0;
int var2=0;
..........
...........
int var99=0;
int var100=0;

The job is to write a "short" macro which when used, pastes the above
declarations in the code.

Your job is to find a different instructor. The one who assigned this
needs to be fired, unless he/she can say why such nonsense could
really be useful.
 
S

Sjouke Burry

SRR said:
Can anyone tell me if it is really possible to do declare 100
variables without "explicitly typing it"?
Consider the following:

int var1=0;
int var2=0;
..........
...........
int var99=0;
int var100=0;

The job is to write a "short" macro which when used, pastes the above
declarations in the code.
int var[100]={0};
Easy to extend to any number of vars you want.
And all set to zero.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top