By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Macro for declaration

P: n/a
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.

Feb 26 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
You'll probably be better off using an array.

Rachael
Feb 26 '07 #2

P: n/a
On Feb 26, 3:00 pm, "SRR" <SRRajesh1...@gmail.comwrote:
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.)
--
ais523

Feb 26 '07 #3

P: n/a
SRR <SR**********@gmail.comwrote:
Can anyone tell me if it is really possible to do declare 100
variables without "explicitly typing it"?
I can tell you that this exact question was asked less than two weeks
ago:

http://groups.google.com/group/comp....0f7e5841c47817

It's typically a good idea to do at least a cursory search of the
group's archives before asking a question.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Feb 26 '07 #4

P: n/a
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.

--
Er*********@sun.com
Feb 26 '07 #5

P: n/a
SRR wrote:
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?

--
Chris "electric hedgehog" Dollin
"If there is a problem, you must confess it, Mr Chaplin."
Mr Carter, /The Beiderbeck Affair/

Feb 26 '07 #6

P: n/a
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.
--
Flash Gordon
Feb 26 '07 #7

P: n/a

SRR wrote:
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.

Feb 26 '07 #8

P: n/a
On 26 Feb, 15:00, "SRR" <SRRajesh1...@gmail.comwrote:
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?

Feb 26 '07 #9

P: n/a

"SRR" <SR**********@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
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.
--
Fred L. Kleinschmidt
Feb 26 '07 #10

P: n/a
SRR wrote:
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.
Feb 26 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.