Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How is it possible to use a constant in a variegate way?

Reply
Thread Tools

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

 
 
Zottel
Guest
Posts: n/a
 
      12-11-2007


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!
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-11-2007
Zottel wrote:
>
> 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() {


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?

--
Ian Collins.
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      12-11-2007
Zottel wrote:
>
> 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.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Zottel
Guest
Posts: n/a
 
      12-11-2007
> > #define KONST1 13 /*Konstanten festlegen*/
> > #define KONST2 7
> > #define KONST2 47

>
> illegal. KONST2 is already defined.
>


Sorry a mistake it should be
#define KONST1 13 /*Konstanten festlegen*/
#define KONST2 7
#define KONST3 47
 
Reply With Quote
 
Zottel
Guest
Posts: n/a
 
      12-11-2007
> > 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?
>


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.


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-11-2007
CBFalconer said:

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


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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      12-11-2007
Zottel wrote:
>>> 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?
>>

>
> 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!

--
Ian Collins.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-11-2007
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[i]);
c = getchar();
}
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Zottel
Guest
Posts: n/a
 
      12-11-2007
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[i]);
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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      12-11-2007
Zottel wrote:
> 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 };

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
"use constant X=>(1,2);" or "use constant X=>[1,2];"? Victor Porton Perl Misc 7 12-11-2007 09:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments