What's wrong with this code?

Discussion in 'C Programming' started by Kenny McCormack, Feb 11, 2008.

  1. In article <>,
    Chris Thomasson <> wrote:
    >_________________________________________________
    >#include <stdio.h>
    >
    >void foo(int _this) {
    > printf("%d\n", _this);
    >}
    >
    >int main(void) {
    > foo(1);
    > getchar();
    > return 0;
    >}
    >
    >_________________________________________________
    >
    >
    >
    >Is this non-portable?
    >
    >
    >I am interested in the name of the variable...


    Sometime in the next 15 minutes, one of the "regulars" will come on and
    tell you that identifiers beginning with underscores are reserved for
    the implementation.
     
    Kenny McCormack, Feb 11, 2008
    #1
    1. Advertising

  2. _________________________________________________
    #include <stdio.h>

    void foo(int _this) {
    printf("%d\n", _this);
    }

    int main(void) {
    foo(1);
    getchar();
    return 0;
    }

    _________________________________________________



    Is this non-portable?


    I am interested in the name of the variable...


    --
    Chris M. Thomasson
    http://appcore.home.comcast.net
     
    Chris Thomasson, Feb 11, 2008
    #2
    1. Advertising

  3. "Kenny McCormack" <> wrote in message
    news:fooio0$baf$...
    > In article <>,
    > Chris Thomasson <> wrote:
    >>_________________________________________________
    >>#include <stdio.h>
    >>
    >>void foo(int _this) {
    >> printf("%d\n", _this);
    >>}
    >>
    >>int main(void) {
    >> foo(1);
    >> getchar();
    >> return 0;
    >>}
    >>
    >>_________________________________________________
    >>
    >>
    >>
    >>Is this non-portable?
    >>
    >>
    >>I am interested in the name of the variable...

    >
    > Sometime in the next 15 minutes, one of the "regulars" will come on and
    > tell you that identifiers beginning with underscores are reserved for
    > the implementation.


    Yeah. That's my problem.
     
    Chris Thomasson, Feb 11, 2008
    #3
  4. "Chris Thomasson" <> wrote in message
    news:...
    >
    > "Kenny McCormack" <> wrote in message
    > news:fooio0$baf$...
    >> In article <>,
    >> Chris Thomasson <> wrote:
    >>>_________________________________________________
    >>>#include <stdio.h>
    >>>
    >>>void foo(int _this) {
    >>> printf("%d\n", _this);
    >>>}
    >>>
    >>>int main(void) {
    >>> foo(1);
    >>> getchar();
    >>> return 0;
    >>>}
    >>>
    >>>_________________________________________________
    >>>
    >>>
    >>>
    >>>Is this non-portable?
    >>>
    >>>
    >>>I am interested in the name of the variable...

    >>
    >> Sometime in the next 15 minutes, one of the "regulars" will come on and
    >> tell you that identifiers beginning with underscores are reserved for
    >> the implementation.

    >
    > Yeah. That's my problem.



    In was under the impression that only a typename, function name, or #define
    could
    not use a leading underscore.

    YIKES!
     
    Chris Thomasson, Feb 11, 2008
    #4
  5. Kenny McCormack

    Ian Collins Guest

    Chris Thomasson wrote:

    >
    > In was under the impression that only a typename, function name, or
    > #define could
    > not use a leading underscore.
    >

    You should check the standard!

    All identifiers that begin with a double underscore or an underscore
    followed by a capital letter are reserved for any use.

    Identifiers that begin with a single underscore reserved for use as
    identifiers at file level.

    --
    Ian Collins.
     
    Ian Collins, Feb 11, 2008
    #5
  6. On Mon, 11 Feb 2008 19:08:25 +1300, Ian Collins wrote:
    > Chris Thomasson wrote:
    >> In was under the impression that only a typename, function name, or
    >> #define could
    >> not use a leading underscore.
    >>

    > You should check the standard!
    >
    > All identifiers that begin with a double underscore or an underscore
    > followed by a capital letter are reserved for any use.
    >
    > Identifiers that begin with a single underscore reserved for use as
    > identifiers at file level.


    Right, so a function parameter named _this does not break any of the
    rules, since a function parameter does not have file scope. So long as
    it's not renamed to __this, it's fine.
     
    Harald van Dijk, Feb 11, 2008
    #6
  7. "Ian Collins" <> wrote in message
    news:...
    > Chris Thomasson wrote:
    >
    >>
    >> In was under the impression that only a typename, function name, or
    >> #define could
    >> not use a leading underscore.
    >>

    > You should check the standard!
    >
    > All identifiers that begin with a double underscore or an underscore
    > followed by a capital letter are reserved for any use.





    >
    > Identifiers that begin with a single underscore reserved for use as
    > identifiers at file level.


    Yup! Thanks. SHI^%
     
    Chris Thomasson, Feb 11, 2008
    #7
  8. Kenny McCormack

    Ian Collins Guest

    Chris Thomasson wrote:
    > "Harald van Dijk" <> wrote in message
    > news:f3f9b$47afe8c6$541dfcd3$1.nb.home.nl...
    >> On Mon, 11 Feb 2008 19:08:25 +1300, Ian Collins wrote:
    >>> Chris Thomasson wrote:
    >>>> In was under the impression that only a typename, function name, or
    >>>> #define could
    >>>> not use a leading underscore.
    >>>>
    >>> You should check the standard!
    >>>
    >>> All identifiers that begin with a double underscore or an underscore
    >>> followed by a capital letter are reserved for any use.
    >>>
    >>> Identifiers that begin with a single underscore reserved for use as
    >>> identifiers at file level.

    >>
    >> Right, so a function parameter named _this does not break any of the
    >> rules, since a function parameter does not have file scope. So long as
    >> it's not renamed to __this, it's fine.

    >
    > A function parameter is contained within file scope right?


    Wrong!

    --
    Ian Collins.
     
    Ian Collins, Feb 11, 2008
    #8
  9. Kenny McCormack

    Ian Collins Guest

    Chris Thomasson wrote:
    >
    > "Ian Collins" <> wrote in message
    >> Chris Thomasson wrote:


    >>> A function parameter is contained within file scope right?

    >>
    >> Wrong!

    >
    > ARGHRHGHGHH#!@$H@#$H@#H$@#H$H@#$H@#$H@$#


    Having problems with the shift key :)

    --
    Ian Collins.
     
    Ian Collins, Feb 11, 2008
    #9
  10. On Sun, 10 Feb 2008 22:38:00 -0800, Chris Thomasson wrote:
    > A function parameter is contained within file scope right?


    Would you expect this to compile?

    int foo(int bar) { return bar; }
    int main(void) { return bar; }

    I wouldn't. The scope of bar should and does end when the definition of
    foo ends. That's not file scope.
     
    Harald van Dijk, Feb 11, 2008
    #10
  11. "Harald van Dijk" <> wrote in message
    news:f3f9b$47afe8c6$541dfcd3$1.nb.home.nl...
    > On Mon, 11 Feb 2008 19:08:25 +1300, Ian Collins wrote:
    >> Chris Thomasson wrote:
    >>> In was under the impression that only a typename, function name, or
    >>> #define could
    >>> not use a leading underscore.
    >>>

    >> You should check the standard!
    >>
    >> All identifiers that begin with a double underscore or an underscore
    >> followed by a capital letter are reserved for any use.
    >>
    >> Identifiers that begin with a single underscore reserved for use as
    >> identifiers at file level.

    >
    > Right, so a function parameter named _this does not break any of the
    > rules, since a function parameter does not have file scope. So long as
    > it's not renamed to __this, it's fine.


    A function parameter is contained within file scope right?
     
    Chris Thomasson, Feb 11, 2008
    #11
  12. "Ian Collins" <> wrote in message
    news:...
    > Chris Thomasson wrote:
    >> "Harald van Dijk" <> wrote in message
    >> news:f3f9b$47afe8c6$541dfcd3$1.nb.home.nl...
    >>> On Mon, 11 Feb 2008 19:08:25 +1300, Ian Collins wrote:
    >>>> Chris Thomasson wrote:
    >>>>> In was under the impression that only a typename, function name, or
    >>>>> #define could
    >>>>> not use a leading underscore.
    >>>>>
    >>>> You should check the standard!
    >>>>
    >>>> All identifiers that begin with a double underscore or an underscore
    >>>> followed by a capital letter are reserved for any use.
    >>>>
    >>>> Identifiers that begin with a single underscore reserved for use as
    >>>> identifiers at file level.
    >>>
    >>> Right, so a function parameter named _this does not break any of the
    >>> rules, since a function parameter does not have file scope. So long as
    >>> it's not renamed to __this, it's fine.

    >>
    >> A function parameter is contained within file scope right?

    >
    > Wrong!


    ARGHRHGHGHH#!@$H@#$H@#H$@#H$H@#$H@#$H@$#
     
    Chris Thomasson, Feb 11, 2008
    #12
  13. "Chris Thomasson" <> wrote in message
    news:...
    >
    > "Ian Collins" <> wrote in message
    > news:...
    >> Chris Thomasson wrote:
    >>> "Harald van Dijk" <> wrote in message
    >>> news:f3f9b$47afe8c6$541dfcd3$1.nb.home.nl...
    >>>> On Mon, 11 Feb 2008 19:08:25 +1300, Ian Collins wrote:
    >>>>> Chris Thomasson wrote:
    >>>>>> In was under the impression that only a typename, function name, or
    >>>>>> #define could
    >>>>>> not use a leading underscore.
    >>>>>>
    >>>>> You should check the standard!
    >>>>>
    >>>>> All identifiers that begin with a double underscore or an underscore
    >>>>> followed by a capital letter are reserved for any use.
    >>>>>
    >>>>> Identifiers that begin with a single underscore reserved for use as
    >>>>> identifiers at file level.
    >>>>
    >>>> Right, so a function parameter named _this does not break any of the
    >>>> rules, since a function parameter does not have file scope. So long as
    >>>> it's not renamed to __this, it's fine.
    >>>
    >>> A function parameter is contained within file scope right?

    >>
    >> Wrong!

    >
    > ARGHRHGHGHH#!@$H@#$H@#H$@#H$H@#$H@#$H@$#



    file-scope {
    #include <stdio.h>

    void foo(int _this) {
    printf("%d\n", _this);
    }

    int main(void) {
    foo(1);
    getchar();
    return 0;
    }
    }


    When I look at that, I see _this existing not only within the function
    parameter scope, but contained within the file-scope as well; this is source
    of my confusion.

    ;^(...
     
    Chris Thomasson, Feb 11, 2008
    #13
  14. On Sun, 10 Feb 2008 22:49:59 -0800, Chris Thomasson wrote:
    > "Harald van Dijk" <> wrote in message
    > news:cb0d1$47afecda$541dfcd3$1.nb.home.nl...
    >> On Sun, 10 Feb 2008 22:38:00 -0800, Chris Thomasson wrote:
    >>> A function parameter is contained within file scope right?

    >>
    >> Would you expect this to compile?
    >>
    >> int foo(int bar) { return bar; }
    >> int main(void) { return bar; }
    >>
    >> I wouldn't. The scope of bar should and does end when the definition of
    >> foo ends. That's not file scope.

    >
    > Thanks for not sticking me in your kill-file.


    I don't know why you think you would get put in anyone's killfile, but
    regardless, I don't use one. :)
     
    Harald van Dijk, Feb 11, 2008
    #14
  15. "Harald van Dijk" <> wrote in message
    news:cb0d1$47afecda$541dfcd3$1.nb.home.nl...
    > On Sun, 10 Feb 2008 22:38:00 -0800, Chris Thomasson wrote:
    >> A function parameter is contained within file scope right?

    >
    > Would you expect this to compile?
    >
    > int foo(int bar) { return bar; }
    > int main(void) { return bar; }
    >
    > I wouldn't. The scope of bar should and does end when the definition of
    > foo ends. That's not file scope.


    Thanks for not sticking me in your kill-file.
     
    Chris Thomasson, Feb 11, 2008
    #15
  16. "Ian Collins" <> wrote in message
    news:...
    > Chris Thomasson wrote:
    >>
    >> "Ian Collins" <> wrote in message
    >>> Chris Thomasson wrote:

    >
    >>>> A function parameter is contained within file scope right?
    >>>
    >>> Wrong!

    >>
    >> ARGHRHGHGHH#!@$H@#$H@#H$@#H$H@#$H@#$H@$#

    >
    > Having problems with the shift key :)


    Well, I don't like it when I made a massive screwed up like this. IMHO, I
    would rather it be in the algorithm, not something fundamental within the C
    Standard.

    Here is the origin of my confusion:

    http://groups.google.com/group/comp.programming.threads/msg/6ff53456a8e4dcfe

    I am lucky if I don't get fired over this... I know I am going to hear about
    it in the morning. LOL! I should go into work with egg already splattered
    all over my face to save them some precious time and energy!

    My threading skills keep me working.

    ;^)
     
    Chris Thomasson, Feb 11, 2008
    #16
  17. "Chris Thomasson" <> writes:
    > _________________________________________________
    > #include <stdio.h>
    >
    > void foo(int _this) {
    > printf("%d\n", _this);
    > }
    >
    > int main(void) {
    > foo(1);
    > getchar();
    > return 0;
    > }
    >
    > _________________________________________________
    >
    > Is this non-portable?
    >
    > I am interested in the name of the variable...


    Others have already described the rules regarding identifiers
    beginning with underscores. Briefly, there's all reserved in some
    contexts; some are reserved in more contexts than others.

    Your "_this", as it turns out, is used in a context in which that
    particular form of identifier is not reserved -- in other words, your
    code is ok as far as the standard is concerned.

    Personally, though, I find it easier to avoid using identifiers
    starting with underscores altogether than to remember the rules in
    detail. Looking at your code, I have to stop and think for a moment
    before remembering that it's ok in this case. If you had used an
    identifier *not* starting with an underscore ("this_", if you like),
    then your code would be just slightly easier to read.

    Part of this, I suppose, is that I've never understood the rationale
    for the rules as they exist, rather than just reserving all
    identifiers starting with underscores for all purposes.

    It's a matter of style, not of legality.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 11, 2008
    #17
  18. Kenny McCormack

    Default User Guest

    Chris Thomasson wrote:

    >
    > "Kenny McCormack" <> wrote in message
    > news:fooio0$baf$...


    > > Sometime in the next 15 minutes, one of the "regulars" will come on
    > > and tell you that identifiers beginning with underscores are
    > > reserved for the implementation.

    >
    > Yeah. That's my problem.



    One of the things you need to learn is that Kenny is troll. He will
    tell any lie he can think of to cause trouble. The best information I
    can give you on that score is to killfile or ignore him.




    Brian
     
    Default User, Feb 12, 2008
    #18
  19. "Chris Thomasson" <> wrote:
    > _________________________________________________
    > #include <stdio.h>
    >
    > void foo(int _this) {
    >   printf("%d\n", _this);
    > }
    >
    > int main(void) {
    >   foo(1);
    >   getchar();
    >   return 0;
    > }
    >
    > _________________________________________________
    >
    > Is this non-portable?
    >
    > I am interested in the name of the variable...


    Is that really the context? What do you hope to gain
    by using a leading underscore in a parameter name?
    Is it because you want to declare another 'this' in
    the function body?

    Other comments notwithstanding, note that _trailing_
    underscores are perfectly legitimate. So what about
    this_?

    If your real reason is you want something close to 'this',
    but don't want a C++ compiler to barf, then try 'thiz'.

    --
    Peter
     
    Peter Nilsson, Feb 12, 2008
    #19
  20. In article <>,
    Default User <> wrote:
    >Chris Thomasson wrote:
    >
    >>
    >> "Kenny McCormack" <> wrote in message
    >> news:fooio0$baf$...

    >
    >> > Sometime in the next 15 minutes, one of the "regulars" will come on
    >> > and tell you that identifiers beginning with underscores are
    >> > reserved for the implementation.

    >>
    >> Yeah. That's my problem.

    >
    >
    >One of the things you need to learn is that Kenny is troll. He will
    >tell any lie he can think of to cause trouble. The best information I
    >can give you on that score is to killfile or ignore him.


    One of the things you need to learn is that the default loser is tard.
    It will tell any lie it can think of to cause trouble. The best
    information I can give you on that score is to killfile or ignore it.
     
    Kenny McCormack, Feb 12, 2008
    #20
    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. walala
    Replies:
    3
    Views:
    2,206
    Ralf Hildebrandt
    Sep 10, 2003
  2. willem oosthuizen

    What is wrong with the following code?

    willem oosthuizen, Oct 10, 2003, in forum: VHDL
    Replies:
    9
    Views:
    1,284
  3. Matthew
    Replies:
    7
    Views:
    681
    Priscilla Walmsley
    Jan 7, 2005
  4. David. E. Goble
    Replies:
    9
    Views:
    484
    David. E. Goble
    Feb 2, 2005
  5. kiran
    Replies:
    12
    Views:
    1,133
    Scott Sauyet
    Dec 7, 2011
Loading...

Share This Page