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

Discuss programming topics for the various GPL'd game engine sources.
frag.machine
Posts: 2126
Joined: Sat Nov 25, 2006 1:49 pm

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

Post 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).
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

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

Post 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.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
mankrip
Posts: 924
Joined: Fri Jul 04, 2008 3:02 am

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

Post 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.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

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

Post 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.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
jitspoe
Posts: 217
Joined: Mon Jan 17, 2005 5:27 am

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

Post 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!" :)
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

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

Post 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.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

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

Post 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.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
mankrip
Posts: 924
Joined: Fri Jul 04, 2008 3:02 am

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

Post 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;
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

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

Post by Baker »

I'm don't think that is true. ... I just tried this.

Code: Select all

do
	{
		printf ("Hello\n");
	} while (0);
> Hello
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
r00k
Posts: 1111
Joined: Sat Nov 13, 2004 10:39 pm

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

Post by r00k »

everything is false until it's true.
mankrip
Posts: 924
Joined: Fri Jul 04, 2008 3:02 am

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

Post 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.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

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

Post by Baker »

Code: Select all

	int ____ = 5;
	printf ("My value %i \n", ____ );
>My value is 5
Probably not recommended way of naming variables. :D
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

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

Post 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...
revelator
Posts: 2621
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

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

Post 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.
Productivity is a state of mind.
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

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

Post 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.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Post Reply