473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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



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!
Dec 11 '07 #1
11 1402
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.
Dec 11 '07 #2
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

Dec 11 '07 #3
#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
Dec 11 '07 #4
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
Dec 11 '07 #5
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.
Dec 11 '07 #6
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
Dec 11 '07 #7
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.
Dec 11 '07 #8
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.
Dec 11 '07 #9
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.
Dec 11 '07 #10
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.
Dec 11 '07 #11
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: W.***************@phys.uu.nl WWW: http://www.phys.uu.nl/~bergmann/
** Life is complex: it has real and imaginary parts **
Dec 11 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
5
by: Veit Wiessner | last post by:
I wrote a program that handles the buildcount of projects (gets called every time I compile a project, it writes a header file which is #include-ed in the project). My question is this, is it...
25
by: tsaar2003 | last post by:
Hi Pythonians, To begin with I'd like to apologize that I am not very experienced Python programmer so please forgive me if the following text does not make any sense. I have been missing...
5
by: =?Utf-8?B?UnlhbiBBbmRydXM=?= | last post by:
Is there a way to configure\create a datatable (strongly typed or untyped) that can have a row accessed in constant time if you are searching on the primary key? If there isn't a way, then what...
2
by: Alex Weber | last post by:
weird issue i ran into today... its possible to define case-insensitive constants outside the scope of a class using: define('name', 'value', false); however, you cannot use define() for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.