What's the differences between REGISTER and AUTO?

J

James Kuyper

JOYCE said:
Look the subject,that's my problem!

Putting the question in the subject line is a problem. You should state
the full question in the body of your message; the subject line should,
at most, contain a summary of the question, not the question itself.
I hope someone can help me, thanks

Before asking us, what have you figured out for yourself from reading
your textbook? If you can't understand your text book, you're not likely
to understand the responses given in this newsgroup any better. However,
if you explain what you're having difficulty understanding from your
textbook's explanation, we can help correct your understanding.

Final note: C is a case-sensitive language. Make sure, whenever you
write down C identifiers, that you always write them in the correct
case. The identifier "register" is a keyword with a very specific
meaning defined by the standard. On the other hand "REGISTER" is a
completely unrelated identifier, which can only be declared or defined
in user code.
 
F

Flash Gordon

JOYCE wrote, On 04/11/08 11:58:
Look the subject,that's my problem!

It is better to put the entire question in the body of the post.
I hope someone can help me, thanks

What does your text book say? What is the difference between a car and a
road? Anyway, register is a hint to the compiler (which normally is not
needed and has not been needed for many years) which affects what you
can do with a variable, auto has not been needed as part of the language
for even longer since where it is legal the variable would have
automatic storage duration without using it.

Another hint is that many processors have registers but I'm not aware of
any that have autos.
 
N

Nick Keighley

always leave the subject in the body of your message:

Subject: What's the differences between REGISTER and AUTO?

Look the subject,that's my problem!
I hope someone can help me, thanks

C is case sensitive so you mean auto and register.
They are both somewhat obselete and hardly ever used.

auto is used with variables to indicate they are not static
or external. That they are local to a function and are
(conceptually) created on entry and (conceptually) destroyed
on exit. A stack is often used to implement this.

int fred (void)
{
auto int i;
auto j
}

the above are both legal. j is implicitly int (at least in the 1989
standard for C).

Note this is completly unecessary. Just use

int i;
int j;

register indicates to the C compiler that the indicated variable
is heavily used (in the programmer's opinion) and should be placed
in a hardware register for faster code. You cannot take the address
of a register variable.

int bill (void)
{
register int i;
int *pi;
pi = &i; /* ILLEGAL *?
}

register is only a request to the compiler and the compiler is
permitted
to ignore it. For instance there may not actually be any spare
registers.
Programmers are often not good at correctly identifying the important
variables and modern compilers are good at it. Hence modern compilers
used ignore the "register" hint (except for disallowing taking the
address
of a register variable).

In <large-num> years of C programming I have never used either
register or auto. I've worked with code with register in it.
I've no idea if the register keyword did any good (or any harm).
I have only seen in auto in old books (I think K&R-one might use
it).
 
N

Nick Keighley

JOYCE wrote, On 04/11/08 11:58:

[what is the difference between register and auto]
register is a hint to the compiler (which normally is not
needed and has not been needed for many years) which affects what you
can do with a variable, auto has not been needed as part of the language
for even longer since where it is legal the variable would have
automatic storage duration without using it.

auto was still present in the 89 standard
 
C

Chris Dollin

Nick said:
JOYCE wrote, On 04/11/08 11:58:

[what is the difference between register and auto]
register is a hint to the compiler (which normally is not
needed and has not been needed for many years) which affects what you
can do with a variable, auto has not been needed as part of the language
for even longer since where it is legal the variable would have
automatic storage duration without using it.

auto was still present in the 89 standard

Present but not needed.

--
'Don't be afraid: /Electra City/
there will be minimal destruction.' - Panic Room

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
 
S

Stephen Sprunk

JOYCE said:
Look the subject,that's my problem!

What subject? You didn't ask a question in your message. The question
you asked in your Subject line (which many people can't see when they
open your post) refers to REGISTER and AUTO, which are not defined by
ISO C; remember, C is case sensitive. I can see why you're having problems.
I hope someone can help me, thanks

If you meant to ask what the difference is between "register" and
"auto", the only guaranteed difference is that you cannot take the
address of a variable of "register" storage class with the unary & operator.

In ancient compilers, there was a chance that "register" variables might
have been faster to use, but modern optimizing compilers will
automatically put variables in registers without needing a hint from the
programmer.

S
 
K

Keith Thompson

Nick Keighley said:
JOYCE wrote, On 04/11/08 11:58:

[what is the difference between register and auto]
register is a hint to the compiler (which normally is not
needed and has not been needed for many years) which affects what you
can do with a variable, auto has not been needed as part of the language
for even longer since where it is legal the variable would have
automatic storage duration without using it.

auto was still present in the 89 standard

auto is still in C99 as well; it's in the language, but either illegal
or redundant in all contexts. There's a proposal to drop it.
 
N

Nate Eldredge

Keith Thompson said:
Nick Keighley said:
JOYCE wrote, On 04/11/08 11:58:

[what is the difference between register and auto]
register is a hint to the compiler (which normally is not
needed and has not been needed for many years) which affects what you
can do with a variable, auto has not been needed as part of the language
for even longer since where it is legal the variable would have
automatic storage duration without using it.

auto was still present in the 89 standard

auto is still in C99 as well; it's in the language, but either illegal
or redundant in all contexts. There's a proposal to drop it.

Was there ever a time when `auto' did something useful?

It seems to have been redundant even in K&R I. Was it just included for
consistency, so that every storage class had a corresponding keyword?
It could maybe be useful to emphasize that a particular variable was
auto, where the context might suggest something else, but I can't really
think of a good example, and in any case a comment would probably be
just as good.
 
K

Keith Thompson

Nate Eldredge said:
Was there ever a time when `auto' did something useful?

Yes. Pre-ANSI C allowed implicit int for object declarations, so
"auto i;" was equivalent to "auto int i;", but "i;" was illegal (if it
appeared before a declaration in the same block) or a reference to a
variable ``i''.

In K&R C, one might write:

foo(x, y)
{
auto i;
extern j;
/* ... */
}

I *think* that "auto i;" is illegal in C90.

[...]
 
B

Ben Bacarisse

Nate Eldredge said:
Keith Thompson said:
Nick Keighley said:
JOYCE wrote, On 04/11/08 11:58:

[what is the difference between register and auto]

register is a hint to the compiler (which normally is not
needed and has not been needed for many years) which affects what you
can do with a variable, auto has not been needed as part of the language
for even longer since where it is legal the variable would have
automatic storage duration without using it.

auto was still present in the 89 standard

auto is still in C99 as well; it's in the language, but either illegal
or redundant in all contexts. There's a proposal to drop it.

Was there ever a time when `auto' did something useful?

It seems to have been redundant even in K&R I.

Well, it is more a case of not wanting to remove it, I imagine. auto
comes from B, which is typeless, so the storage class must always be
there. As C evolved, the shadow of B remained in the guise of
implicit int:

auto x;

and

register y;

were ints just like

f() {...}

was an int-returning function. I imagine that for a while (pre-K&R I)
a lot of very early C looked a lot like B, except where the type
really mattered and then, after a while, it just seemed churlish to
remove the keyword. That last bit is a guess of course!
 
N

Nate Eldredge

Keith Thompson said:
Yes. Pre-ANSI C allowed implicit int for object declarations, so
"auto i;" was equivalent to "auto int i;", but "i;" was illegal (if it
appeared before a declaration in the same block) or a reference to a
variable ``i''.

In K&R C, one might write:

foo(x, y)
{
auto i;
extern j;
/* ... */
}

I see.

In a sense, it's still redundant, because writing `int i;' would be
equivalent. But I can see how that wouldn't fit the idiom.
 
F

Flash Gordon

Keith Thompson wrote, On 04/11/08 16:58:
Yes. Pre-ANSI C allowed implicit int for object declarations, so
"auto i;" was equivalent to "auto int i;", but "i;" was illegal (if it
appeared before a declaration in the same block) or a reference to a
variable ``i''.

In K&R C, one might write:

foo(x, y)
{
auto i;
extern j;
/* ... */
}

I *think* that "auto i;" is illegal in C90.

C90 still had implicit int so I think that "auto i;" is legal in C90.
For what it's worth, gcc agrees with me and accepts it without complaint
with "-ansi -pedantic", you have to enable additional warnings or
(partial) C99 to get a warning.

I still don't think auto was useful in K&R C since one could always have
used "int i;" instead of "auto i;" with the same effect. Perhaps pre-K&R
there was a situation where nothing other than auto would do?
 
F

Flash Gordon

Nick Keighley wrote, On 04/11/08 13:08:

auto is used with variables to indicate they are not static
or external. That they are local to a function and are

Local to the block, which can be a smaller scope than the entire function.
(conceptually) created on entry and (conceptually) destroyed
on exit. A stack is often used to implement this.

int fred (void)
{
auto int i;
auto j

Missing semi-colon ;-)
}

the above are both legal. j is implicitly int (at least in the 1989
standard for C).

Illegal in C99 of course.

<snip good stuff>
 
S

Stephen Sprunk

Flash said:
I still don't think auto was useful in K&R C since one could always have
used "int i;" instead of "auto i;" with the same effect. Perhaps pre-K&R
there was a situation where nothing other than auto would do?

Remember that it was a very gradual evolution from B to NB to C. When
language features were added (on a weekly basis, as K&R found something
new that would make coding easier), it was desirable that old code still
compile correctly so that they wouldn't have to constantly rewrite
existing programs that worked just fine; only new programs that needed
those new features (like types) had to use them.

B was typeless and so "auto i;" was the only way to declare a local
variable. For B programs to be valid C, it was necessary that C have
implicit int, meaning that "auto i;" was legal in C as well.

C99 started removing some of the old compatibility features because the
amount of code that still relied on it had become insignificant and had
become a source of bugs, programmer confusion, and unnecessary compiler
complexity. B is finally dead.

S
 
I

Ian Collins

Jack said:
The programmer can often know things from the design of the code that
a compiler cannot discern without emulation or profiling of the entire
program.
Modern hosted compilers can use profile feedback to tune optimisations.
I haven't come across an embedded compilers that do this, but I expect
there are those that do.
 
P

Phil Carmody

Ian Collins said:
Modern hosted compilers can use profile feedback to tune optimisations.
I haven't come across an embedded compilers that do this, but I expect
there are those that do.

Haven't you come across GCC as an embedded compiler? I know at Freescale
all ARM- and POWER- targetted C was compiled using GCC. (OK, the DSPs and
PICs had other compilers, obviously.)

Phil
 
T

Thad Smith

Nick said:
On 4 Nov, 12:51, Flash Gordon <[email protected]> wrote:
auto was still present in the 89 standard

As well as the current C standard.

The thing that is interesting to me is that the specifier auto appears to
have no semantics in the Standard, except for preventing a declaration from
also having a typedef, static, extern, or register storage class specifier.

There is mention of automatic storage duration, but no explicit connection
to the auto storage class specifier.
 
I

Ian Collins

Phil said:
Haven't you come across GCC as an embedded compiler? I know at Freescale
all ARM- and POWER- targetted C was compiled using GCC. (OK, the DSPs and
PICs had other compilers, obviously.)
Is profile feedback available on those embedded targets?
 
P

Phil Carmody

Ian Collins said:
Is profile feedback available on those embedded targets?

Well, I know there was GCC 4.1, and that at least supports -fprofile-arcs
and -fbranch-probabilities, etc., so yup, AFAICR it is.

Phil
 

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