Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Assigning a value using #define ?

11 views
Skip to first unread message

Scott Kelley

unread,
Sep 29, 2008, 12:08:49 PM9/29/08
to
Forgive me if this is a no-brainer - I'm pretty rusty - haven't been
able to find anything in my C books...

I have a number of #define commands in my current code. I now need to
provide for two or more possible definitions, depending upon the value
of a particular eeprom byte.

For example,
if eeprom byte 0x22 =0, define "Hysteresis" as 15
if eeprom byte 0x22 =1, define "Hysteresis" as 11

It seems to me that I have seen a one line function that would work
for this where the "selection byte" would refer to the data in a
location in a following list of numbers, something kinda like this:

temp = I2C_EEPROM_read(0x00, 0x22);
#define Hysteresis (temp: 15,11,9)

Isn't there something like this?

The other part I'm not sure of here is whether I can use a #define
command in the body of the code (where I am able to access the eeprom
memory). Maybe I just need to use a variable array that I can define
in the code depending upon the value of my eeprom byte . . . ?

It's been too long since I worked with this stuff (and I was a
neophyte when I wrote it in the first place)... Thanks for any help.
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Gordon Burditt

unread,
Oct 3, 2008, 5:03:38 PM10/3/08
to
>Forgive me if this is a no-brainer - I'm pretty rusty - haven't been
>able to find anything in my C books...
>
>I have a number of #define commands in my current code. I now need to
>provide for two or more possible definitions, depending upon the value
>of a particular eeprom byte.

You can't do that at compile time. You get one definition at compile
time in effect at any particular place. However that definition
can determine at run time what you want.

>For example,
>if eeprom byte 0x22 =0, define "Hysteresis" as 15
>if eeprom byte 0x22 =1, define "Hysteresis" as 11

Note: you failed to specify what happens if it's something else.

Try something like:

#define Hysteresis ((I2C_EEPROM_read(0x00, 0x22) == 0) ? 15 : \
((I2C_EEPROM_read(0x00, 0x22) == 1) ? 11 : abort()))

>It seems to me that I have seen a one line function that would work
>for this where the "selection byte" would refer to the data in a
>location in a following list of numbers, something kinda like this:
>
>temp = I2C_EEPROM_read(0x00, 0x22);
>#define Hysteresis (temp: 15,11,9)
>
>Isn't there something like this?
>
>The other part I'm not sure of here is whether I can use a #define
>command in the body of the code (where I am able to access the eeprom
>memory).

It is not necessary that the DEFINITION occur in the body of the
code (although it can be). The USE has to be inside the body of
a function. Remember, a #define is just textual substitution.

I have to wonder whether this #define should be a variable instead.

Hans-Bernhard Bröker

unread,
Oct 3, 2008, 5:04:12 PM10/3/08
to
Scott Kelley wrote:
> For example,
> if eeprom byte 0x22 =0, define "Hysteresis" as 15
> if eeprom byte 0x22 =1, define "Hysteresis" as 11

That can't be done. You can #define HYSTERESIS as a value that depends
on that eeprom byte:

/* Side note: recommended practice has all macro names in CAPITALS */
#define HYSTERESIS ((I2C_EEPROM_read(0x00,0x22) ? 11 : 15)

But you probably shouldn't do that, because it means you would call that
EEPROM read function every single time this macro value is used.
Generally one would define a variable "Hysteresis" whose value you write
once, at the start of the program, depending on that EEPROM cell's content.

Jack Klein

unread,
Oct 3, 2008, 5:04:38 PM10/3/08
to
On Mon, 29 Sep 2008 11:08:49 -0500 (CDT), Scott Kelley
<sco...@iccom.com> wrote in comp.lang.c.moderated:

> Forgive me if this is a no-brainer - I'm pretty rusty - haven't been
> able to find anything in my C books...
>
> I have a number of #define commands in my current code. I now need to
> provide for two or more possible definitions, depending upon the value
> of a particular eeprom byte.
>
> For example,
> if eeprom byte 0x22 =0, define "Hysteresis" as 15
> if eeprom byte 0x22 =1, define "Hysteresis" as 11
>
> It seems to me that I have seen a one line function that would work
> for this where the "selection byte" would refer to the data in a
> location in a following list of numbers, something kinda like this:
>
> temp = I2C_EEPROM_read(0x00, 0x22);
> #define Hysteresis (temp: 15,11,9)
>
> Isn't there something like this?
>
> The other part I'm not sure of here is whether I can use a #define
> command in the body of the code (where I am able to access the eeprom
> memory). Maybe I just need to use a variable array that I can define
> in the code depending upon the value of my eeprom byte . . . ?
>
> It's been too long since I worked with this stuff (and I was a
> neophyte when I wrote it in the first place)... Thanks for any help.

You can't do this with a macro. The arguments to a macro must be
known at compile time, they can't be based on, or use in any way,
values that do not exist until run-time.

What you can do is something like this:

int Hysteresis; /* or whatever type it is, inside or outside of
any function */

....then at run-time:

temp = /* whatever function you want to call */

....and if there are only two possible values:

Hysteresis = (temp == 0) ? 15 : 11;

....of course, if there are more than two values you can use an
if...else or a switch.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

James Kuyper

unread,
Oct 3, 2008, 5:04:51 PM10/3/08
to
Scott Kelley wrote:
> Forgive me if this is a no-brainer - I'm pretty rusty - haven't been
> able to find anything in my C books...
>
> I have a number of #define commands in my current code. I now need to
> provide for two or more possible definitions, depending upon the value
> of a particular eeprom byte.
>
> For example,
> if eeprom byte 0x22 =0, define "Hysteresis" as 15
> if eeprom byte 0x22 =1, define "Hysteresis" as 11
>
> It seems to me that I have seen a one line function that would work
> for this where the "selection byte" would refer to the data in a
> location in a following list of numbers, something kinda like this:
>
> temp = I2C_EEPROM_read(0x00, 0x22);
> #define Hysteresis (temp: 15,11,9)

By convention, macro names should be in all caps: HYSTERESIS. This isn't
a requirement of the language, but following this convention makes
things easier for the next person to read your code.

> Isn't there something like this?

Not within the preprocessor. Preprocessing occurs during compilation,
before the byte you're referring to is even available for inspection.

What you can do is

#define HYSTERESIS (I2C_EEPROM_read(0x00, 0x22) ? 11 : 15)

However, that means that I2C_EEPROM_read() will be called in every
location where you use this macro. If this isn't a problem, then this is
the approach I would recommend.

If calling I2C_EEPROM_read() many times is a problem, then I recommend
using a global and an initialization process:

hysteresis.h:
extern int hysteresis;
extern void init_hyst(void);

hysteresis.c:
#include "hysteresis.h"

int hysteresis = 9;
void init_hyst(void)
{
switch(I2C_EEPROM_read(0x00, 0x22)
{
case 0: hysteresis = 15; break;
case 1: hysteresis = 11; break;
default: break;
}
}

Have main() call init_hyst() before any other part of the program uses
hysteresis.

> The other part I'm not sure of here is whether I can use a #define
> command in the body of the code (where I am able to access the eeprom

> memory). ...

Preprocessing is done when your program is translated; putting the
#define into a function body doesn't change that.

Jasen Betts

unread,
Oct 3, 2008, 5:05:07 PM10/3/08
to
On 2008-09-29, Scott Kelley <sco...@iccom.com> wrote:
> Forgive me if this is a no-brainer - I'm pretty rusty - haven't been
> able to find anything in my C books...
>
> I have a number of #define commands in my current code. I now need to
> provide for two or more possible definitions, depending upon the value
> of a particular eeprom byte.
>
> For example,
> if eeprom byte 0x22 =0, define "Hysteresis" as 15
> if eeprom byte 0x22 =1, define "Hysteresis" as 11
>
> It seems to me that I have seen a one line function that would work
> for this where the "selection byte" would refer to the data in a
> location in a following list of numbers, something kinda like this:
>
> temp = I2C_EEPROM_read(0x00, 0x22);
> #define Hysteresis (temp: 15,11,9)
>
> Isn't there something like this?
>
> The other part I'm not sure of here is whether I can use a #define
> command in the body of the code (where I am able to access the eeprom
> memory). Maybe I just need to use a variable array that I can define
> in the code depending upon the value of my eeprom byte . . . ?
>
> It's been too long since I worked with this stuff (and I was a
> neophyte when I wrote it in the first place)... Thanks for any help.

#define Hysterisis ( ((int[]){15,11,9})[I2C_EEPROM_read(0x00, 0x22)])

this doesn't not make Hysterisis behave like a constant, but like an
expression, (you can't used it define the size of arrays etc., but
you can use it pretty much anywhere else a number is needed)

when Hysterisis is used the compiler substitutes in the expression
which calls I2C_EEPROM_read((0x00, 0x22) and then refers to that place
in the anonymous array.

(if you're targeting an 8-bit microcontroller you should probably use
char instead of int above.)

if your compiler supports it using an inline function (instead of the
#define) with a named const array would probably be superior.

inline int Hysterisis(){
const int hysts[]={15,11,9};
return hysts I2C_EEPROM_read(0x00, 0x22);
}

if it's known that the program will be restarted before the after the
eeprom value is changed the inline can be optimised at the cost of
room to store an int by storing the value resulting much greater speed
on subsequent calls (I2C is much slower than rerencing ram)

inline int Hysterisis(){
static int knownhist=0;
const int hysts[]={15,11,9};
if(knownhist) return knownhist;
return knownhist=hysts[I2C_EEPROM_read(0x00, 0x22)];
}

Bye.
Jasen

0 new messages