Page 3 of 5

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Tue Mar 03, 2015 9:36 pm
by frag.machine
Baker wrote:We think of comments as being ignored. This isn't actually true.

They are interpreted as whitespace.

int x/*hello*/y = 5; // Error. If the comments were ignored, this line would be int xy=5;
I suspect this is treated as a "\n" actually (which may be irrelevant at least for your example).

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Wed Mar 04, 2015 12:23 am
by Baker
frag.machine wrote:I suspect this is treated as a "\n" actually (which may be irrelevant at least for your example).
It can't be because this works and preprocessor directives are terminated by a newline, so this inline comment is bridging the newline.
#define name "dick" /* this works
hello world */ "jones"
This fails ...
#define name "dick" // Hello
"jones"
And this obviously fails too
#define name "dick"
"jones"
However, just to see what is really going on I did a compile using /i in Visual Studio, which generates preprocessed output and to my surprise the preprocessed file generates this.
int x/*hello*/y=5;
becomes ...
int xy=5;
However, it does not compile and generates error "syntax error : missing ';' before identifier y".
Which caused me to check CodeBlocks/mingw and it gets same error message when compiling.

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Wed Mar 04, 2015 1:32 am
by mankrip
Baker wrote:
mankrip wrote:When using strcmp, the coder is essentially asking "does those strings match?", and a no is a valid answer for such a question, so in cases like this it makes no sense to assign the return values as if they were error codes.
strcmp and memcmp's -1, 0, 1 return values are great for sorting.
:shock:
Baker wrote:An example is FitzQuake's cvar list. Your own engine feeds (sorting by distance) qsort --- which requires a comparison function be supplied, what is essentially memcmp (although I think you slightly wrap it).
:oops: I feel dumb. I can't remember if I ever knew about strcmp's return values behaving like that.

And from what I've read now, they're not limited to 1 and -1. strcmp("example asset", "example data") should return -3, because 'a' - 'd' == -3.
Baker wrote:This fails ...
#define name "dick" // Hello
"jones"
And this obviously fails too
#define name "dick"
"jones"
Now I know why the "old man" fired him.
Baker wrote:However, just to see what is really going on I did a compile using /i in Visual Studio, which generates preprocessed output and to my surprise the preprocessed file generates this.
int x/*hello*/y=5;
becomes ...
int xy=5;
However, it does not compile and generates error "syntax error : missing ';' before identifier y".
Which caused me to check CodeBlocks/mingw and it gets same error message when compiling.
Maybe there's a null character between x and y. Or the error checking is done before the preprocessing.

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Wed Mar 04, 2015 2:06 am
by Baker
mankrip wrote:Now I know why the "old man" fired him.
Hehe. :D Yeah, Robocop reference :D
Baker wrote:Maybe there's a null character between x and y.
Just checked with hex editor ---- nothing between x and y, not even null character.

Yes, this discrepancy does seem odd, but then again I've been coming up with absurd scenarios to see how the compiler reacts to them.

In part because I'm trying to pull the functions out of C source files to create the headers automatically. And also have a index of functions. CTags does this, but I don't like the source code nor the output format.

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Thu Mar 05, 2015 12:18 am
by jitspoe
I wonder if it's a rule that /* */ comments are treated as whitespace, or if that behavior could vary from compiler to compiler. Obvious approach is, "Don't do that!" :)

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Thu Mar 05, 2015 12:25 am
by Baker
jitspoe wrote:I wonder if it's a rule that /* */ comments are treated as whitespace, or if that behavior could vary from compiler to compiler. Obvious approach is, "Don't do that!" :)
Found an answer. https://msdn.microsoft.com/en-us/library/wfwda74e.aspx

Probably does not vary from compiler to compiler and most likely ANSI C behavior. On the MSDN page, it does not say "Microsoft specific" for that section.
MSDN wrote:A "comment" is a sequence of characters beginning with a forward slash/asterisk combination (/*) that is treated as a single white-space character by the compiler and is otherwise ignored.

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Sat Mar 14, 2015 5:13 am
by Baker
Shortcircuit operators don't yield the same result as non-shortcircuit operators.

if (2 & 1) // Non-shortcircuiting. Will evaluate to false because 2 AND 1 = 0 (bit operation)

if (2 && 1) // Shortcircuiting. 2 will pass. Then 1 will pass. Will evaluate to true.

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Tue Mar 17, 2015 2:39 pm
by mankrip

Code: Select all

do
{
stuff (); // this works
} while (true);

Code: Select all

do
{
stuff (); // this doesn't, even though it comes before the conditional check
} while (false);
And I figured out that the need for ( ) in sizeof is because of pointers, like
i = sizeof(cvar_t *) - 1;
instead of
i = sizeof cvar_t * - 1;

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Wed Mar 18, 2015 3:19 am
by Baker
I'm don't think that is true. ... I just tried this.

Code: Select all

do
	{
		printf ("Hello\n");
	} while (0);
> Hello

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Wed Mar 18, 2015 7:48 am
by r00k
everything is false until it's true.

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Wed Mar 18, 2015 10:37 pm
by mankrip
Baker wrote:I'm don't think that is true. ... I just tried this.

Code: Select all

do
	{
		printf ("Hello\n");
	} while (0);
> Hello
Still, it happened in my code. My guess is that some compiler (MSVC ToolKit 2003) optimization may be the cause.

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Thu Mar 19, 2015 8:12 am
by Baker

Code: Select all

	int ____ = 5;
	printf ("My value %i \n", ____ );
>My value is 5
Probably not recommended way of naming variables. :D

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Thu Mar 19, 2015 3:30 pm
by Spike
one leading underscore is reserved for the libc.
two leading underscores are reserved for the compiler.
I have no idea who four leading underscores is reserved for...

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Sun Mar 22, 2015 8:02 am
by revelator
One particularly funny one i seen in C is tag names
#define thistag L# or LL# for 64 bit long long names.
Took me a while to figure out that one and some compilers dont like tags.

Re: Funny C Rules (And Low-Level Languages in general)

Posted: Sat Apr 04, 2015 1:59 am
by Baker
I've been thinking of calling conventions lately.

Then I realized something.

WinMain and the message handling function (tends to be named something like MainWndProc or such in Quake) are both WINAPI which is stdcall, not cdecl.

I've never really paid too much attention to how those 2 specific functions must use a different calling convention than the rest of the code.

No doubt because it is "interfacing" with the operating system, just like how Windows API calls are WINAPI calling convention.