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

Register

P: n/a
#include<stdio.h>
register int i=9;
int main(void)
{
register int i=8;
int *p;
p=&i;
printf("%d",*p);
return 0;
}

I compiled this code in Turbo C/C++.It gave two errors as: Register int
cant be used as global and pointer cant be applied to the register int.

When I compiled the same in Miracle C compiler it compiled succesfully
and gave output as 8.

I think Turbo C/C++ compiler is correct as I have read in books that
pointer cant be applied variables of register storage class.

Why this differentiation in compilers?

Dont they follow the same rules of C?

Please help.

Regards,
Raghu

Nov 16 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 15 Nov 2006 23:22:25 -0800, "raghu" <ra*********@gmail.comwrote:
>#include<stdio.h>
register int i=9;
int main(void)
{
register int i=8;
int *p;
p=&i;
printf("%d",*p);
return 0;
}

I compiled this code in Turbo C/C++.It gave two errors as: Register int
cant be used as global and pointer cant be applied to the register int.

When I compiled the same in Miracle C compiler it compiled succesfully
and gave output as 8.

I think Turbo C/C++ compiler is correct as I have read in books that
pointer cant be applied variables of register storage class.

Why this differentiation in compilers?

Dont they follow the same rules of C?

Please help.

Regards,
Raghu
This code should not compile. This line:

register int i=9;

uses an inappropriate storage class (register) for an object with
external linkage (or, for that matter, any object that is not an auto
declaration).

Even if you remove the above line, I don't think you can take the
address of a "register".

The "register" keyword should be considered obsolete and off-limits,
unless you really, really know what you're doing and have a really
good argument to justify its usage. A good rule of thumb is to never,
ever use it.

--
jay
Nov 16 '06 #2

P: n/a
raghu said:
#include<stdio.h>
register int i=9;
int main(void)
{
register int i=8;
int *p;
p=&i;
printf("%d",*p);
return 0;
}

I compiled this code in Turbo C/C++.It gave two errors as: Register int
cant be used as global and pointer cant be applied to the register int.
That's correct.
When I compiled the same in Miracle C compiler it compiled succesfully
and gave output as 8.
If you invoke it in conforming mode, it *must* issue a diagnostic message
for the above code, but it is allowed to continue to translate the code and
even produce an executable program. (If you don't invoke it in conforming
mode, it isn't a C compiler as far as comp.lang.c is concerned.)

If you're not getting a diagnostic message from Miracle C for taking the
address of a register, you're not invoking it in conforming mode. (That
doesn't amount to a claim that Miracle C /has/ a conforming mode, on which
I have no opinion either way, since AFAIK I've never used it.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.