Register

Discussion in 'C Programming' started by raghu, Nov 16, 2006.

  1. raghu

    raghu Guest

    #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
     
    raghu, Nov 16, 2006
    #1
    1. Advertising

  2. raghu

    jaysome Guest

    On 15 Nov 2006 23:22:25 -0800, "raghu" <> wrote:

    >#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
     
    jaysome, Nov 16, 2006
    #2
    1. Advertising

  3. 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.
     
    Richard Heathfield, Nov 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jay
    Replies:
    0
    Views:
    484
  2. Max
    Replies:
    1
    Views:
    2,561
    eadgbe
    Sep 4, 2003
  3. yoga
    Replies:
    1
    Views:
    501
    Ray Andraka
    Oct 11, 2003
  4. Pierre-Louis
    Replies:
    8
    Views:
    1,017
    Bob Efram
    Oct 17, 2003
  5. Morfeusz

    Register problem(long)

    Morfeusz, Dec 1, 2003, in forum: VHDL
    Replies:
    4
    Views:
    584
    Patrick Erich
    Dec 3, 2003
Loading...

Share This Page