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

Discussion in 'C Programming' started by Zottel, Dec 11, 2007.

  1. Zottel

    Zottel Guest

    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!
    Zottel, Dec 11, 2007
    #1
    1. Advertising

  2. Zottel

    Ian Collins Guest

    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.
    Ian Collins, Dec 11, 2007
    #2
    1. Advertising

  3. Zottel

    CBFalconer Guest

    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
    CBFalconer, Dec 11, 2007
    #3
  4. Zottel

    Zottel Guest

    > > #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
    Zottel, Dec 11, 2007
    #4
  5. Zottel

    Zottel Guest

    > > 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.
    Zottel, Dec 11, 2007
    #5
  6. 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
    Richard Heathfield, Dec 11, 2007
    #6
  7. Zottel

    Ian Collins Guest

    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.
    Ian Collins, Dec 11, 2007
    #7
  8. 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;
    }

    --
    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
    Richard Heathfield, Dec 11, 2007
    #8
  9. Zottel

    Zottel Guest

    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.
    Zottel, Dec 11, 2007
    #9
  10. Zottel

    Ian Collins Guest

    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.
    Ian Collins, Dec 11, 2007
    #10
  11. Zottel

    Zottel Guest

    > Why? An array is the appropriate solution.
    >
    > You could initialise the array thus:
    >
    > int konst[] = { KONST1, KONST2, KONST3 };
    >
    > --
    > Ian Collins.


    For me Ian, of course it is an academic quest ... have my special thanks
    @ll for your helpfully response.
    Zottel, Dec 11, 2007
    #11
  12. On Mon, 10 Dec 2007, Zottel wrote:

    > 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: WWW: http://www.phys.uu.nl/~bergmann/
    ** Life is complex: it has real and imaginary parts **
    Wouter Bergmann Tiest, Dec 11, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,074
  2. Martin Magnusson
    Replies:
    2
    Views:
    492
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    545
    Tim Roberts
    Nov 23, 2006
  4. H. Wade Minter
    Replies:
    8
    Views:
    274
    Robin
    Apr 25, 2004
  5. Victor Porton
    Replies:
    7
    Views:
    107
    TonyV
    Dec 11, 2007
Loading...

Share This Page