What's wrong with this code?

K

Kenny McCormack

_________________________________________________
#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.
 
C

Chris Thomasson

_________________________________________________
#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...
 
C

Chris Thomasson

Kenny McCormack said:
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.
 
C

Chris Thomasson

Chris Thomasson said:
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!
 
I

Ian Collins

Chris said:
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.
 
H

Harald van Dijk

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.
 
C

Chris Thomasson

Ian Collins said:
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^%
 
H

Harald van Dijk

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.
 
C

Chris Thomasson

Harald van Dijk said:
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?
 
C

Chris Thomasson

Chris Thomasson said:
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.

;^(...
 
C

Chris Thomasson

Harald van Dijk said:
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.
 
C

Chris Thomasson

Ian Collins said:
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.

;^)
 
K

Keith Thompson

Chris Thomasson said:
_________________________________________________
#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.
 
D

Default User

Chris said:
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
 
P

Peter Nilsson

Chris Thomasson said:
_________________________________________________
#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'.
 
K

Kenny McCormack

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top