How is it possible to use a constant in a variegate way?

Z

Zottel

I like to use couple of constant in comination with an variable to
change the name of the constant.

The further text show my vainly try:

#define KONST1 13 /*Konstanten festlegen*/
#define KONST2 7
#define KONST2 47

main() {

int c = getchar(), i; /*Variablen definieren und
initialisieren*/
for (i = 1, i <= 3, i++) { /*Einfügen von ASCii Zeichen*/
putchar(c+KONSTi);
c = getchar();
}
}

I am a newbie in C and I hope this question is not to nasty.

Have thanks for your kindly support!
 
I

Ian Collins

Zottel said:
I like to use couple of constant in comination with an variable to
change the name of the constant.

The further text show my vainly try:

#define KONST1 13 /*Konstanten festlegen*/
#define KONST2 7
#define KONST2 47
You shouldn't redefine a macro without undefining it first.

main returns int.
int c = getchar(), i; /*Variablen definieren und
initialisieren*/

Bad style, don't mix variable assignment and declarations on the same
line. It's often recommended to keep to one declaration per line.
for (i = 1, i <= 3, i++) { /*Einfügen von ASCii Zeichen*/

The commas should be semicolons.
putchar(c+KONSTi);

No, you can't do this. Maybe you want to define KONST as an array?
 
C

CBFalconer

Zottel said:
I like to use couple of constant in comination with an variable to
change the name of the constant.

The further text show my vainly try:

#define KONST1 13 /*Konstanten festlegen*/
#define KONST2 7
#define KONST2 47

illegal. KONST2 is already defined.
 
Z

Zottel

#define KONST1 13 /*Konstanten festlegen*/
illegal. KONST2 is already defined.

Sorry a mistake it should be
#define KONST1 13 /*Konstanten festlegen*/
#define KONST2 7
#define KONST3 47
 
Z

Zottel

for (i = 1; i <= 3; i++) { /*Einfügen von ASCii Zeichen*/
The commas should be semicolons.


No, you can't do this. Maybe you want to define KONST as an array?

Hi Ian,
Have thanks for your comment.

How I have to wrote it, if I like to define the KONST as an array?
It should have 2 to 255 fields and the value of each field should be
determined.
 
R

Richard Heathfield

CBFalconer said:
illegal. KONST2 is already defined.

That isn't why it's illegal. It's illegal because KONST2 is already defined
*differently*. See 3.8.3 (Macro replacement) or the C99 equivalent.

Incidentally, by "illegal" at this point, I simply mean that it's a
constraint violation.
 
I

Ian Collins

Zottel said:
Hi Ian,
Have thanks for your comment.

How I have to wrote it, if I like to define the KONST as an array?
It should have 2 to 255 fields and the value of each field should be
determined.
Richard Heathfield just posted what I was typing!
 
R

Richard Heathfield

Zottel said:
I like to use couple of constant in comination with an variable to
change the name of the constant.

The further text show my vainly try:

#define KONST1 13 /*Konstanten festlegen*/
#define KONST2 7
#define KONST2 47

main() {

int c = getchar(), i; /*Variablen definieren und
initialisieren*/
for (i = 1, i <= 3, i++) { /*Einfügen von ASCii Zeichen*/

You meant <, not <=, and you meant semicolons ; ; not commas , ,
putchar(c+KONSTi);
c = getchar();
}
}

I am a newbie in C and I hope this question is not to nasty.

For C to be able to do this, the implementation would have to be able to
maintain a symbol table at runtime, and few implementations do that (I
would guess that a C interpreter might, but most C implementations are
compilers).

Instead, I recommend that you use an array. For example:

#include <stdio.h>

int main(void)
{
int konst[] = { 13, 7, 47 };
int c = getchar();
int i;
for(i = 0; i < 3; i++)
{
putchar(c + konst);
c = getchar();
}
return 0;
}
 
Z

Zottel

Hi Richard,

have thank for your solution by using a vector.

#include <stdio.h>
int main(void)
{
int konst[] = { 13, 7, 47 };
int c = getchar();
int i;
for(i = 0; i < 3; i++)
{
putchar(c + konst);
c = getchar();
}
return 0;
}


I have thought about this solution too.

But I like to find a solution with a constant like ..

#define KONST1
.....
#define KONST255

If there is now possibility, I would do it like your suggestion.
 
I

Ian Collins

Zottel said:
Hi Richard,

have thank for your solution by using a vector.


I have thought about this solution too.

But I like to find a solution with a constant like ..

#define KONST1
.....
#define KONST255

If there is now possibility, I would do it like your suggestion.

Why? An array is the appropriate solution.

You could initialise the array thus:

int konst[] = { KONST1, KONST2, KONST3 };
 
Z

Zottel

Why? An array is the appropriate solution.
You could initialise the array thus:

int konst[] = { KONST1, KONST2, KONST3 };

For me Ian, of course it is an academic quest ... have my special thanks
@ll for your helpfully response.
 
W

Wouter Bergmann Tiest

I like to use couple of constant in comination with an variable to
change the name of the constant.

If you don't want to use an array to save on dynamic memory allocation,
you could use an all-constant approach:

#define KONST1 13
#define KONST2 7
#define KONST3 47
#define KONST(i) (i==1?KONST1:(i==2?KONST2:KONST3))

main() {
int c = getchar()-48;
printf("c = %d, KONST = %d\n",c,KONST(c));
return 0;
}

Regards,

Wouter Bergmann Tiest

E-mail: (e-mail address removed) WWW: http://www.phys.uu.nl/~bergmann/
** Life is complex: it has real and imaginary parts **
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top