Register

R

raghu

#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
 
J

jaysome

#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.
 
R

Richard Heathfield

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top