By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,668 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,668 IT Pros & Developers. It's quick & easy.

What's the differences between REGISTER and AUTO?

P: n/a
Look the subject,that's my problem!
I hope someone can help me, thanks
Nov 4 '08 #1
Share this Question
Share on Google+
21 Replies


P: n/a
JOYCE wrote:
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.
Nov 4 '08 #2

P: n/a
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.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 4 '08 #3

P: n/a
always leave the subject in the body of your message:

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

On 4 Nov, 11:58, JOYCE <zzzzzz90...@126.comwrote:
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-numyears 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).
--
Nick Keighley


Nov 4 '08 #4

P: n/a
On 4 Nov, 12:51, Flash Gordon <s...@spam.causeway.comwrote:
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

--
Nick Keighley
Nov 4 '08 #5

P: n/a
Nick Keighley wrote:
On 4 Nov, 12:51, Flash Gordon <s...@spam.causeway.comwrote:
>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

Nov 4 '08 #6

P: n/a
JOYCE wrote:
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
Nov 4 '08 #7

P: n/a
Nick Keighley <ni******************@hotmail.comwrites:
On 4 Nov, 12:51, Flash Gordon <s...@spam.causeway.comwrote:
>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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 4 '08 #8

P: n/a
Keith Thompson <ks***@mib.orgwrites:
Nick Keighley <ni******************@hotmail.comwrites:
>On 4 Nov, 12:51, Flash Gordon <s...@spam.causeway.comwrote:
>>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.
Nov 4 '08 #9

P: n/a
Nate Eldredge <na**@vulcan.lanwrites:
Keith Thompson <ks***@mib.orgwrites:
[...]
>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?
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.

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 4 '08 #10

P: n/a
Nate Eldredge <na**@vulcan.lanwrites:
Keith Thompson <ks***@mib.orgwrites:
>Nick Keighley <ni******************@hotmail.comwrites:
>>On 4 Nov, 12:51, Flash Gordon <s...@spam.causeway.comwrote:
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!

--
Ben.
Nov 4 '08 #11

P: n/a
Keith Thompson <ks***@mib.orgwrites:
Nate Eldredge <na**@vulcan.lanwrites:
>Keith Thompson <ks***@mib.orgwrites:
[...]
>>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?

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.
Nov 4 '08 #12

P: n/a
Keith Thompson wrote, On 04/11/08 16:58:
Nate Eldredge <na**@vulcan.lanwrites:
>Keith Thompson <ks***@mib.orgwrites:
[...]
>>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?

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?
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 4 '08 #13

P: n/a
Nick Keighley wrote, On 04/11/08 13:08:

<snip>
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>
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 4 '08 #14

P: n/a
Flash Gordon wrote:
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
Nov 4 '08 #15

P: n/a
On Tue, 04 Nov 2008 08:38:25 -0600, Stephen Sprunk
<st*****@sprunk.orgwrote in comp.lang.c:
JOYCE wrote:
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.
Of course your generalization above is not universally true.

That is the case for all generalizations, including this one.

I have yet to see a compiler whose output I could not improve with
judicious use of the register keyword when I needed to make the
effort, generally in time-critical real time code.

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.

--
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
Nov 5 '08 #16

P: n/a
"Nick Keighley" <ni******************@hotmail.comwrote in message
news:84961d20-3c81-4bd0-b5e6-
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.
I've worked with compilers where "register" was interpreted, as you
indicated, that this variable will have heavy use and, thus, it optimized
access to it even when it wasn't stored in a register.
In <large-numyears of C programming I have never used either
register or auto. I've worked with code with register in it.
It's still pretty popular in embedded programming, and many compilers used
for those targets will even spell out how many register variables will be
honored.
I've no idea if the register keyword did any good (or any harm).
Depends on the machine, of course. And yeah, I've seen places where it did
do harm. Usually because (as you also indicated) the programmer could be
wrong about what's really heavily used. In the cases I have in mind, he
specified several register variables, the compiler honored them, but as a
result it cut it's pool of registers for it's own optimizations and the code
got both fatter and slower.

- Bill

Nov 5 '08 #17

P: n/a
Jack Klein wrote:
>
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.

--
Ian Collins
Nov 5 '08 #18

P: n/a
Ian Collins <ia******@hotmail.comwrites:
Jack Klein wrote:
>>
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.
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
--
We must respect the other fellow's religion, but only in the sense and to the
extent that we respect his theory that his wife is beautiful and his children
smart. -- Henry Louis Mencken (1880-1956), American editor and critic
Nov 5 '08 #19

P: n/a
Nick Keighley wrote:
On 4 Nov, 12:51, Flash Gordon <s...@spam.causeway.comwrote:
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.

--
Thad
Nov 6 '08 #20

P: n/a
Phil Carmody wrote:
Ian Collins <ia******@hotmail.comwrites:
>Jack Klein wrote:
>>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.

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?

--
Ian Collins
Nov 6 '08 #21

P: n/a
Ian Collins <ia******@hotmail.comwrites:
Phil Carmody wrote:
>Ian Collins <ia******@hotmail.comwrites:
>>Jack Klein wrote:
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.

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?
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
--
We must respect the other fellow's religion, but only in the sense and to the
extent that we respect his theory that his wife is beautiful and his children
smart. -- Henry Louis Mencken (1880-1956), American editor and critic
Nov 6 '08 #22

This discussion thread is closed

Replies have been disabled for this discussion.